Skip to content

Commit 7938cd1

Browse files
committed
Merge commit '6fb1dd339bb385b6b30c20464fcabe318e1c80d6'
2 parents 0e4d211 + 6fb1dd3 commit 7938cd1

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

libs/ujson4c/3rdparty/ultrajson.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ typedef struct __JSONObjectDecoder
294294
JSOBJ (*newArray)(void *prv);
295295
JSOBJ (*newInt)(void *prv, JSINT32 value);
296296
JSOBJ (*newLong)(void *prv, JSINT64 value);
297+
JSOBJ (*newUnsignedLong)(void *prv, JSUINT64 value);
297298
JSOBJ (*newDouble)(void *prv, double value);
298299
void (*releaseObject)(void *prv, JSOBJ obj);
299300
JSPFN_MALLOC malloc;

libs/ujson4c/3rdparty/ultrajsondec.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds)
102102
{
103103
int intNeg = 1;
104104
int mantSize = 0;
105-
JSUINT64 intValue;
105+
JSUINT64 intValue, chrValue;
106106
int chr;
107107
int decimalCount = 0;
108108
double frcValue = 0.0;
@@ -139,14 +139,13 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds)
139139
case '8':
140140
case '9':
141141
{
142-
//FIXME: Check for arithemtic overflow here
143-
//PERF: Don't do 64-bit arithmetic here unless we know we have to
144-
intValue = intValue * 10ULL + (JSLONG) (chr - 48);
145-
146-
if (intValue > overflowLimit)
142+
chrValue = chr - '0';
143+
if (intValue > (ULLONG_MAX - chrValue) / 10ULL)
147144
{
148-
return SetError(ds, -1, overflowLimit == LLONG_MAX ? "Value is too big" : "Value is too small");
145+
return SetError(ds, -1, intNeg > 0 ? "Value is too big" :
146+
"Value is too small");
149147
}
148+
intValue = intValue * 10ULL + chrValue;
150149

151150
offset ++;
152151
mantSize ++;
@@ -181,7 +180,17 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric (struct DecoderState *ds)
181180

182181
if ((intValue >> 31))
183182
{
184-
return ds->dec->newLong(ds->prv, (JSINT64) (intValue * (JSINT64) intNeg));
183+
if (intNeg > 0)
184+
{
185+
return (intValue > LLONG_MAX)
186+
? ds->dec->newUnsignedLong(ds->prv, intValue)
187+
: ds->dec->newLong(ds->prv, (JSINT64) intValue);
188+
}
189+
else if (intValue > LLONG_MIN)
190+
{
191+
return SetError(ds, -1, "Value is too small");
192+
}
193+
return ds->dec->newLong(ds->prv, -(JSINT64)intValue);
185194
}
186195
else
187196
{

libs/ujson4c/src/ujdecode.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ typedef struct __LongLongValue
9090
long long value;
9191
} LongLongValue;
9292

93+
typedef struct __UnsignedLongLongValue
94+
{
95+
Item item;
96+
unsigned long long value;
97+
} UnsignedLongLongValue;
98+
9399
typedef struct __DoubleValue
94100
{
95101
Item item;
@@ -143,6 +149,9 @@ static void *alloc(struct DecoderState *ds, size_t cbSize)
143149
newSize *= 2;
144150

145151
newSlab = (HeapSlab *) ds->malloc(newSize);
152+
if (! newSlab) {
153+
return NULL;
154+
}
146155
newSlab->start = (unsigned char *) (newSlab + 1);
147156
newSlab->end = (unsigned char *) newSlab + newSize;
148157
newSlab->size = newSize;
@@ -297,6 +306,18 @@ static JSOBJ newLong(void *context, JSINT64 value)
297306
return (JSOBJ) llv;
298307
}
299308

309+
static JSOBJ newUnsignedLong(void *context, JSUINT64 value)
310+
{
311+
struct DecoderState *ds = context;
312+
UnsignedLongLongValue *llv =
313+
(UnsignedLongLongValue *) alloc(ds, sizeof(UnsignedLongLongValue));
314+
if (llv) {
315+
llv->item.type = UJT_UnsignedLongLong;
316+
llv->value = (long long unsigned) value;
317+
}
318+
return (JSOBJ) llv;
319+
}
320+
300321
static JSOBJ newDouble(void *context, double value)
301322
{
302323
struct DecoderState *ds = context;
@@ -329,6 +350,11 @@ static long long GetLongLong(UJObject obj)
329350
return ((LongLongValue *) obj)->value;
330351
}
331352

353+
static unsigned long long GetUnsignedLongLong(UJObject obj)
354+
{
355+
return ((UnsignedLongLongValue *) obj)->value;
356+
}
357+
332358
void UJFree(void *state)
333359
{
334360
struct DecoderState *ds = (struct DecoderState *) state;
@@ -518,12 +544,27 @@ int UJIterObject(void **iter, UJString *outKey, UJObject *outValue)
518544
return 1;
519545
}
520546

547+
unsigned long long UJNumericUnsignedLongLong(UJObject obj)
548+
{
549+
switch ( ((Item *) obj)->type)
550+
{
551+
case UJT_Long: return (unsigned long long) GetLong(obj);
552+
case UJT_LongLong: return (unsigned long long) GetLongLong(obj);
553+
case UJT_UnsignedLongLong: return GetUnsignedLongLong(obj);
554+
case UJT_Double: return (unsigned long long) GetDouble(obj);
555+
default: break;
556+
}
557+
558+
return 0;
559+
}
560+
521561
long long UJNumericLongLong(UJObject obj)
522562
{
523563
switch ( ((Item *) obj)->type)
524564
{
525565
case UJT_Long: return (long long) GetLong(obj);
526566
case UJT_LongLong: return (long long) GetLongLong(obj);
567+
case UJT_UnsignedLongLong: return (long long) GetUnsignedLongLong(obj);
527568
case UJT_Double: return (long long) GetDouble(obj);
528569
default: break;
529570
}
@@ -537,6 +578,7 @@ int UJNumericInt(UJObject obj)
537578
{
538579
case UJT_Long: return (int) GetLong(obj);
539580
case UJT_LongLong: return (int) GetLongLong(obj);
581+
case UJT_UnsignedLongLong: return (int) GetUnsignedLongLong(obj);
540582
case UJT_Double: return (int) GetDouble(obj);
541583
default: break;
542584
}
@@ -550,6 +592,7 @@ double UJNumericFloat(UJObject obj)
550592
{
551593
case UJT_Long: return (double) GetLong(obj);
552594
case UJT_LongLong: return (double) GetLongLong(obj);
595+
case UJT_UnsignedLongLong: return (double) GetUnsignedLongLong(obj);
553596
case UJT_Double: return (double) GetDouble(obj);
554597
default: break;
555598
}
@@ -781,6 +824,7 @@ UJObject UJDecode(const char *input, size_t cbInput, UJHeapFuncs *hf, void **out
781824
newArray,
782825
newInt,
783826
newLong,
827+
newUnsignedLong,
784828
newDouble,
785829
releaseObject,
786830
NULL,

libs/ujson4c/src/ujdecode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern "C" {
4545
UJT_False,
4646
UJT_Long,
4747
UJT_LongLong,
48+
UJT_UnsignedLongLong,
4849
UJT_Double,
4950
UJT_String,
5051
UJT_Array,
@@ -241,6 +242,7 @@ extern "C" {
241242
converting doubles to integers.
242243
===============================================================================
243244
*/
245+
unsigned long long UJNumericUnsignedLongLong(UJObject obj);
244246
long long UJNumericLongLong(UJObject obj);
245247
int UJNumericInt(UJObject obj);
246248

0 commit comments

Comments
 (0)