Skip to content

Commit 1377517

Browse files
committed
use function safe_addition_for_params to check integer overflow
1 parent e17c076 commit 1377517

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

src/fromsixel.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <stdio.h>
5353
#include <ctype.h> /* isdigit */
5454
#include <string.h> /* memcpy */
55+
#include <limits.h>
5556

5657
#if defined(HAVE_INTTYPES_H)
5758
# include <inttypes.h>
@@ -367,6 +368,16 @@ parser_context_init(parser_context_t *context)
367368
return status;
368369
}
369370

371+
SIXELSTATUS safe_addition_for_params(parser_context_t *context, unsigned char *p){
372+
int x;
373+
374+
x = *p - '0'; /* 0 <= x <= 9 */
375+
if ((context->param > INT_MAX / 10) || (x > INT_MAX - context->param * 10)) {
376+
return SIXEL_BAD_INTEGER_OVERFLOW;
377+
}
378+
context->param = context->param * 10 + x;
379+
return SIXEL_OK;
380+
}
370381

371382
/* convert sixel data into indexed pixel bytes and palette data */
372383
SIXELAPI SIXELSTATUS
@@ -446,7 +457,10 @@ sixel_decode_raw_impl(
446457
if (context->param < 0) {
447458
context->param = 0;
448459
}
449-
context->param = context->param * 10 + *p - '0';
460+
status = safe_addition_for_params(context, p);
461+
if (SIXEL_FAILED(status)) {
462+
goto end;
463+
}
450464
p++;
451465
break;
452466
case ';':
@@ -647,7 +661,10 @@ sixel_decode_raw_impl(
647661
case '7':
648662
case '8':
649663
case '9':
650-
context->param = context->param * 10 + *p - '0';
664+
status = safe_addition_for_params(context, p);
665+
if (SIXEL_FAILED(status)) {
666+
goto end;
667+
}
651668
p++;
652669
break;
653670
case ';':
@@ -721,7 +738,10 @@ sixel_decode_raw_impl(
721738
case '7':
722739
case '8':
723740
case '9':
724-
context->param = context->param * 10 + *p - '0';
741+
status = safe_addition_for_params(context, p);
742+
if (SIXEL_FAILED(status)) {
743+
goto end;
744+
}
725745
p++;
726746
break;
727747
default:
@@ -753,7 +773,10 @@ sixel_decode_raw_impl(
753773
case '7':
754774
case '8':
755775
case '9':
756-
context->param = context->param * 10 + *p - '0';
776+
status = safe_addition_for_params(context, p);
777+
if (SIXEL_FAILED(status)) {
778+
goto end;
779+
}
757780
p++;
758781
break;
759782
case ';':

0 commit comments

Comments
 (0)