|
52 | 52 | #include <stdio.h> |
53 | 53 | #include <ctype.h> /* isdigit */ |
54 | 54 | #include <string.h> /* memcpy */ |
| 55 | +#include <limits.h> |
55 | 56 |
|
56 | 57 | #if defined(HAVE_INTTYPES_H) |
57 | 58 | # include <inttypes.h> |
@@ -367,6 +368,16 @@ parser_context_init(parser_context_t *context) |
367 | 368 | return status; |
368 | 369 | } |
369 | 370 |
|
| 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 | +} |
370 | 381 |
|
371 | 382 | /* convert sixel data into indexed pixel bytes and palette data */ |
372 | 383 | SIXELAPI SIXELSTATUS |
@@ -446,7 +457,10 @@ sixel_decode_raw_impl( |
446 | 457 | if (context->param < 0) { |
447 | 458 | context->param = 0; |
448 | 459 | } |
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 | + } |
450 | 464 | p++; |
451 | 465 | break; |
452 | 466 | case ';': |
@@ -647,7 +661,10 @@ sixel_decode_raw_impl( |
647 | 661 | case '7': |
648 | 662 | case '8': |
649 | 663 | 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 | + } |
651 | 668 | p++; |
652 | 669 | break; |
653 | 670 | case ';': |
@@ -721,7 +738,10 @@ sixel_decode_raw_impl( |
721 | 738 | case '7': |
722 | 739 | case '8': |
723 | 740 | 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 | + } |
725 | 745 | p++; |
726 | 746 | break; |
727 | 747 | default: |
@@ -753,7 +773,10 @@ sixel_decode_raw_impl( |
753 | 773 | case '7': |
754 | 774 | case '8': |
755 | 775 | 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 | + } |
757 | 780 | p++; |
758 | 781 | break; |
759 | 782 | case ';': |
|
0 commit comments