@@ -370,9 +370,11 @@ static StrScanFmt strscan_bin(const uint8_t *p, TValue *o,
370370}
371371
372372/* Scan string containing a number. Returns format. Returns value in o. */
373- StrScanFmt lj_strscan_scan (const uint8_t * p , TValue * o , uint32_t opt )
373+ StrScanFmt lj_strscan_scan (const uint8_t * p , MSize len , TValue * o ,
374+ uint32_t opt )
374375{
375376 int32_t neg = 0 ;
377+ const uint8_t * pe = p + len ;
376378
377379 /* Remove leading space, parse sign and non-numbers. */
378380 if (LJ_UNLIKELY (!lj_char_isdigit (* p ))) {
@@ -390,7 +392,7 @@ StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt)
390392 p += 3 ;
391393 }
392394 while (lj_char_isspace (* p )) p ++ ;
393- if (* p ) return STRSCAN_ERROR ;
395+ if (* p || p < pe ) return STRSCAN_ERROR ;
394396 o -> u64 = tmp .u64 ;
395397 return STRSCAN_NUM ;
396398 }
@@ -488,6 +490,7 @@ StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt)
488490 while (lj_char_isspace (* p )) p ++ ;
489491 if (* p ) return STRSCAN_ERROR ;
490492 }
493+ if (p < pe ) return STRSCAN_ERROR ;
491494
492495 /* Fast path for decimal 32 bit integers. */
493496 if (fmt == STRSCAN_INT && base == 10 &&
@@ -523,7 +526,7 @@ StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt)
523526
524527int LJ_FASTCALL lj_strscan_num (GCstr * str , TValue * o )
525528{
526- StrScanFmt fmt = lj_strscan_scan ((const uint8_t * )strdata (str ), o ,
529+ StrScanFmt fmt = lj_strscan_scan ((const uint8_t * )strdata (str ), str -> len , o ,
527530 STRSCAN_OPT_TONUM );
528531 lua_assert (fmt == STRSCAN_ERROR || fmt == STRSCAN_NUM );
529532 return (fmt != STRSCAN_ERROR );
@@ -532,7 +535,7 @@ int LJ_FASTCALL lj_strscan_num(GCstr *str, TValue *o)
532535#if LJ_DUALNUM
533536int LJ_FASTCALL lj_strscan_number (GCstr * str , TValue * o )
534537{
535- StrScanFmt fmt = lj_strscan_scan ((const uint8_t * )strdata (str ), o ,
538+ StrScanFmt fmt = lj_strscan_scan ((const uint8_t * )strdata (str ), str -> len , o ,
536539 STRSCAN_OPT_TOINT );
537540 lua_assert (fmt == STRSCAN_ERROR || fmt == STRSCAN_NUM || fmt == STRSCAN_INT );
538541 if (fmt == STRSCAN_INT ) setitype (o , LJ_TISNUM );
0 commit comments