Skip to content

Commit 709bd6a

Browse files
fix(tinyxml2): CVE-2024-50614/50615 - fix integer overflow in char refs
Fix integer overflow vulnerability in XML character reference parsing. The code could overflow when parsing large numeric character references. - Use uint32_t instead of unsigned long for UCS values - Add MAX_CODE_POINT check (0x10FFFF) to prevent overflow - Remove unnecessary assertion checks that don't prevent overflow Upstream: leethomason/tinyxml2@494735de30c9 Generated-By: glm-5.1 Co-Authored-By: hudeng <hudeng@deepin.org>
1 parent fa4041e commit 709bd6a

1 file changed

Lines changed: 67 additions & 70 deletions

File tree

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,72 @@
1-
Index: tinyxml2/tinyxml2.cpp
2-
===================================================================
3-
--- tinyxml2.orig/tinyxml2.cpp
4-
+++ tinyxml2/tinyxml2.cpp
5-
@@ -472,11 +472,11 @@ const char* XMLUtil::GetCharacterRef( co
6-
// Presume an entity, and pull it out.
7-
*length = 0;
8-
1+
--- a/tinyxml2.cpp
2+
+++ b/tinyxml2.cpp
3+
@@ -471,12 +471,13 @@
4+
{
5+
// Presume an entity, and pull it out.
6+
*length = 0;
97
+ static const uint32_t MAX_CODE_POINT = 0x10FFFF;
10-
+
11-
if ( *(p+1) == '#' && *(p+2) ) {
12-
- unsigned long ucs = 0;
13-
- TIXMLASSERT( sizeof( ucs ) >= 4 );
14-
- ptrdiff_t delta = 0;
15-
- unsigned mult = 1;
16-
+ uint32_t ucs = 0;
17-
+ uint32_t mult = 1;
18-
static const char SEMICOLON = ';';
19-
20-
if ( *(p+2) == 'x' ) {
21-
@@ -497,7 +497,7 @@ const char* XMLUtil::GetCharacterRef( co
22-
--q;
23-
24-
while ( *q != 'x' ) {
25-
- unsigned int digit = 0;
26-
+ uint32_t digit = 0;
27-
28-
if ( *q >= '0' && *q <= '9' ) {
29-
digit = *q - '0';
30-
@@ -512,11 +512,12 @@ const char* XMLUtil::GetCharacterRef( co
31-
return 0;
32-
}
33-
TIXMLASSERT( digit < 16 );
34-
- TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
35-
- const unsigned int digitScaled = mult * digit;
36-
- TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
37-
+ const uint32_t digitScaled = mult * digit;
38-
ucs += digitScaled;
39-
- TIXMLASSERT( mult <= UINT_MAX / 16 );
8+
9+
if ( *(p+1) == '#' && *(p+2) ) {
10+
- unsigned long ucs = 0;
11+
+ uint32_t ucs = 0;
12+
TIXMLASSERT( sizeof( ucs ) >= 4 );
13+
ptrdiff_t delta = 0;
14+
- unsigned mult = 1;
15+
+ uint32_t mult = 1;
16+
static const char SEMICOLON = ';';
17+
18+
if ( *(p+2) == 'x' ) {
19+
@@ -497,7 +498,7 @@
20+
--q;
21+
22+
while ( *q != 'x' ) {
23+
- unsigned int digit = 0;
24+
+ uint32_t digit = 0;
25+
26+
if ( *q >= '0' && *q <= '9' ) {
27+
digit = *q - '0';
28+
@@ -512,11 +513,11 @@
29+
return 0;
30+
}
31+
TIXMLASSERT( digit < 16 );
32+
- TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
33+
- const unsigned int digitScaled = mult * digit;
34+
- TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
35+
+ const uint32_t digitScaled = mult * digit;
36+
ucs += digitScaled;
37+
- TIXMLASSERT( mult <= UINT_MAX / 16 );
4038
+ if (ucs > MAX_CODE_POINT) {
4139
+ return 0;
4240
+ }
43-
+
44-
mult *= 16;
45-
--q;
46-
}
47-
@@ -540,22 +541,23 @@ const char* XMLUtil::GetCharacterRef( co
48-
49-
while ( *q != '#' ) {
50-
if ( *q >= '0' && *q <= '9' ) {
51-
- const unsigned int digit = *q - '0';
52-
+ const uint32_t digit = *q - '0';
53-
TIXMLASSERT( digit < 10 );
54-
- TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
55-
- const unsigned int digitScaled = mult * digit;
56-
- TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
57-
+ const uint32_t digitScaled = mult * digit;
58-
ucs += digitScaled;
59-
+ if (ucs > MAX_CODE_POINT) {
60-
+ return 0;
61-
+ }
62-
}
63-
else {
64-
return 0;
65-
}
66-
- TIXMLASSERT( mult <= UINT_MAX / 10 );
67-
mult *= 10;
68-
--q;
69-
}
70-
}
71-
// convert the UCS to UTF-8
41+
mult *= 16;
42+
--q;
43+
}
44+
@@ -540,22 +541,23 @@
45+
46+
while ( *q != '#' ) {
47+
if ( *q >= '0' && *q <= '9' ) {
48+
- const unsigned int digit = *q - '0';
49+
+ const uint32_t digit = *q - '0';
50+
TIXMLASSERT( digit < 10 );
51+
- TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
52+
- const unsigned int digitScaled = mult * digit;
53+
- TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
54+
+ const uint32_t digitScaled = mult * digit;
55+
ucs += digitScaled;
56+
+ if (ucs > MAX_CODE_POINT) {
57+
+ return 0;
58+
+ }
59+
}
60+
else {
61+
return 0;
62+
}
63+
- TIXMLASSERT( mult <= UINT_MAX / 10 );
64+
mult *= 10;
65+
--q;
66+
}
67+
}
68+
// convert the UCS to UTF-8
7269
+ TIXMLASSERT(ucs <= MAX_CODE_POINT);
73-
ConvertUTF32ToUTF8( ucs, value, length );
74-
return p + delta + 1;
75-
}
70+
ConvertUTF32ToUTF8( ucs, value, length );
71+
return p + delta + 1;
72+
}

0 commit comments

Comments
 (0)