|
7 | 7 | #include <string.h> |
8 | 8 | #include <stdlib.h> |
9 | 9 | #include <ctype.h> |
| 10 | +#include <limits.h> |
10 | 11 | #include "mmvstr.h" |
11 | 12 | #include "mmdata.h" |
12 | 13 | #include "mminou.h" |
@@ -2730,6 +2731,12 @@ char parseProof(long statemNum) |
2730 | 2731 | return returnFlag; |
2731 | 2732 | } // parseProof() |
2732 | 2733 |
|
| 2734 | +long saturatingMulAdd(long x, long y, long z) { |
| 2735 | + if (x > LONG_MAX / y) return LONG_MAX; |
| 2736 | + x *= y; |
| 2737 | + if (x > LONG_MAX - z) return LONG_MAX; |
| 2738 | + return x + z; |
| 2739 | +} |
2733 | 2740 | // Parse proof in compressed format. |
2734 | 2741 | // Parse proof of one statement in source file. Uses wrkProof structure. |
2735 | 2742 | // Returns 0 if OK; returns 1 if proof is incomplete (is empty or has '?' |
@@ -3099,8 +3106,8 @@ char parseCompressedProof(long statemNum) |
3099 | 3106 | // * for each character c: |
3100 | 3107 | // * if c in ['U'..'Y']: n := n * 5 + (c - 'U' + 1) |
3101 | 3108 | // * if c in ['A'..'T']: n := n * 20 + (c - 'A' + 1) |
3102 | | - labelMapIndex = labelMapIndex * lettersLen + |
3103 | | - chrWeight[(long)(fbPtr[0])]; |
| 3109 | + labelMapIndex = saturatingMulAdd( |
| 3110 | + labelMapIndex, lettersLen, chrWeight[(long)(fbPtr[0])]); |
3104 | 3111 | if (labelMapIndex >= g_WrkProof.compressedPfNumLabels) { |
3105 | 3112 | if (!g_WrkProof.errorCount) { |
3106 | 3113 | sourceError(labelStart, tokLength, statemNum, cat( |
@@ -3184,8 +3191,8 @@ char parseCompressedProof(long statemNum) |
3184 | 3191 | labelMapIndex = chrWeight[(long)(fbPtr[0])] + 1; |
3185 | 3192 | labelStart = fbPtr; // Save label start for error msg |
3186 | 3193 | } else { |
3187 | | - labelMapIndex = labelMapIndex * digitsLen + |
3188 | | - chrWeight[(long)(fbPtr[0])] + 1; |
| 3194 | + labelMapIndex = saturatingMulAdd( |
| 3195 | + labelMapIndex, digitsLen, chrWeight[(long)(fbPtr[0])] + 1); |
3189 | 3196 | if (bggyAlgo) labelMapIndex--; // Adjust for buggy algorithm |
3190 | 3197 | } |
3191 | 3198 | break; |
|
0 commit comments