@@ -347,7 +347,7 @@ static void errorOut(const char* msg)
347347
348348/*! readU32FromCharChecked() :
349349 * @return 0 if success, and store the result in *value.
350- * allows and interprets K, KB, KiB, M, MB and MiB suffix.
350+ * allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
351351 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
352352 * @return 1 if an overflow error occurs */
353353static int readU32FromCharChecked (const char * * stringPtr , unsigned * value )
@@ -362,15 +362,22 @@ static int readU32FromCharChecked(const char** stringPtr, unsigned* value)
362362 if (result < last ) return 1 ; /* overflow error */
363363 (* stringPtr )++ ;
364364 }
365- if ((* * stringPtr == 'K' ) || (* * stringPtr == 'M' )) {
366- unsigned const maxK = ((unsigned )(-1 )) >> 10 ;
367- if (result > maxK ) return 1 ; /* overflow error */
368- result <<= 10 ;
369- if (* * stringPtr == 'M' ) {
370- if (result > maxK ) return 1 ; /* overflow error */
371- result <<= 10 ;
365+ if ((* * stringPtr == 'K' ) || (* * stringPtr == 'M' ) || (* * stringPtr == 'G' )) {
366+ switch (* * stringPtr ) {
367+ case 'K' :
368+ if (result > (((unsigned )-1 ) >> 10 )) return 1 ; /* overflow error */
369+ result <<= 10 ;
370+ break ;
371+ case 'M' :
372+ if (result > (((unsigned )-1 ) >> 20 )) return 1 ; /* overflow error */
373+ result <<= 20 ;
374+ break ;
375+ case 'G' :
376+ if (result > (((unsigned )-1 ) >> 30 )) return 1 ; /* overflow error */
377+ result <<= 30 ;
378+ break ;
372379 }
373- (* stringPtr )++ ; /* skip `K` or `M ` */
380+ (* stringPtr )++ ; /* skip `K`, `M` or `G ` */
374381 if (* * stringPtr == 'i' ) (* stringPtr )++ ;
375382 if (* * stringPtr == 'B' ) (* stringPtr )++ ;
376383 }
@@ -380,7 +387,7 @@ static int readU32FromCharChecked(const char** stringPtr, unsigned* value)
380387
381388/*! readU32FromChar() :
382389 * @return : unsigned integer value read from input in `char` format.
383- * allows and interprets K, KB, KiB, M, MB and MiB suffix.
390+ * allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
384391 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
385392 * Note : function will exit() program if digit sequence overflows */
386393static unsigned readU32FromChar (const char * * stringPtr ) {
@@ -392,7 +399,7 @@ static unsigned readU32FromChar(const char** stringPtr) {
392399
393400/*! readIntFromChar() :
394401 * @return : signed integer value read from input in `char` format.
395- * allows and interprets K, KB, KiB, M, MB and MiB suffix.
402+ * allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
396403 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
397404 * Note : function will exit() program if digit sequence overflows */
398405static int readIntFromChar (const char * * stringPtr ) {
@@ -409,7 +416,7 @@ static int readIntFromChar(const char** stringPtr) {
409416
410417/*! readSizeTFromCharChecked() :
411418 * @return 0 if success, and store the result in *value.
412- * allows and interprets K, KB, KiB, M, MB and MiB suffix.
419+ * allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
413420 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
414421 * @return 1 if an overflow error occurs */
415422static int readSizeTFromCharChecked (const char * * stringPtr , size_t * value )
@@ -424,15 +431,22 @@ static int readSizeTFromCharChecked(const char** stringPtr, size_t* value)
424431 if (result < last ) return 1 ; /* overflow error */
425432 (* stringPtr )++ ;
426433 }
427- if ((* * stringPtr == 'K' ) || (* * stringPtr == 'M' )) {
428- size_t const maxK = ((size_t )(-1 )) >> 10 ;
429- if (result > maxK ) return 1 ; /* overflow error */
430- result <<= 10 ;
431- if (* * stringPtr == 'M' ) {
432- if (result > maxK ) return 1 ; /* overflow error */
433- result <<= 10 ;
434+ if ((* * stringPtr == 'K' ) || (* * stringPtr == 'M' ) || (* * stringPtr == 'G' )) {
435+ switch (* * stringPtr ) {
436+ case 'K' :
437+ if (result > (((size_t )-1 ) >> 10 )) return 1 ; /* overflow error */
438+ result <<= 10 ;
439+ break ;
440+ case 'M' :
441+ if (result > (((size_t )-1 ) >> 20 )) return 1 ; /* overflow error */
442+ result <<= 20 ;
443+ break ;
444+ case 'G' :
445+ if (result > (((size_t )-1 ) >> 30 )) return 1 ; /* overflow error */
446+ result <<= 30 ;
447+ break ;
434448 }
435- (* stringPtr )++ ; /* skip `K` or `M ` */
449+ (* stringPtr )++ ; /* skip `K`, `M` or `G ` */
436450 if (* * stringPtr == 'i' ) (* stringPtr )++ ;
437451 if (* * stringPtr == 'B' ) (* stringPtr )++ ;
438452 }
@@ -442,7 +456,7 @@ static int readSizeTFromCharChecked(const char** stringPtr, size_t* value)
442456
443457/*! readSizeTFromChar() :
444458 * @return : size_t value read from input in `char` format.
445- * allows and interprets K, KB, KiB, M, MB and MiB suffix.
459+ * allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
446460 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
447461 * Note : function will exit() program if digit sequence overflows */
448462static size_t readSizeTFromChar (const char * * stringPtr ) {
@@ -830,7 +844,7 @@ static unsigned init_nbWorkers(unsigned defaultNbWorkers) {
830844 NEXT_FIELD(__nb); \
831845 _varu32 = readU32FromChar(&__nb); \
832846 if(*__nb != 0) { \
833- errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB are allowed"); \
847+ errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB, G, GB, GiB are allowed"); \
834848 } \
835849}
836850
@@ -839,7 +853,7 @@ static unsigned init_nbWorkers(unsigned defaultNbWorkers) {
839853 NEXT_FIELD(__nb); \
840854 _varTsize = readSizeTFromChar(&__nb); \
841855 if(*__nb != 0) { \
842- errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB are allowed"); \
856+ errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB, G, GB, GiB are allowed"); \
843857 } \
844858}
845859
0 commit comments