Skip to content

Commit b92f55e

Browse files
committed
Fixed off-by-one
1 parent 13227bf commit b92f55e

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

mp_read_radix.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
3737

3838
/* Try faster version first */
3939
if (MP_HAS(S_MP_FASTER_READ_RADIX)) {
40-
if ((err = s_mp_faster_read_radix(a, str, 0, (int)s_mp_strlen(str),
41-
radix)) != MP_OKAY) goto LTM_ERR;
40+
if ((err = s_mp_faster_read_radix(a, str, 0, (int)s_mp_strlen(str) - 1,
41+
radix)) != MP_OKAY) goto LTM_ERR;
4242
} else if (MP_HAS(S_MP_SLOWER_READ_RADIX)) {
43-
if ((err = s_mp_slower_read_radix(a, str, 0, (int)s_mp_strlen(str),
44-
radix)) != MP_OKAY) goto LTM_ERR;
43+
if ((err = s_mp_slower_read_radix(a, str, 0, (int)s_mp_strlen(str) - 1,
44+
radix)) != MP_OKAY) goto LTM_ERR;
4545
}
4646

4747
/* set the sign only if a != 0 */

s_mp_faster_read_radix.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ static const uint8_t s_read_radix_cutoff[65] = { 0, 0,
2121
/* This is in mp_prime_is_prime.c and can be reused */
2222
static int s_floor_ilog2(int value)
2323
{
24-
unsigned int r = 0;
24+
int r = 0;
2525
while ((value >>= 1) != 0) {
2626
r++;
2727
}
2828
return r;
2929
}
3030

31-
3231
mp_err s_mp_faster_read_radix(mp_int *a, const char *str, int start, int end, int radix)
3332
{
3433
int len, mid;
@@ -52,7 +51,7 @@ mp_err s_mp_faster_read_radix(mp_int *a, const char *str, int start, int end, in
5251
}
5352

5453
if ((err = s_mp_slower_read_radix(&A, str, start, start + mid + 1, radix)) != MP_OKAY) goto LTM_ERR;
55-
if ((err = s_mp_slower_read_radix(&B, str, start + mid + 1, end, radix)) != MP_OKAY) goto LTM_ERR;
54+
if ((err = s_mp_slower_read_radix(&B, str, start + mid +1, end, radix)) != MP_OKAY) goto LTM_ERR;
5655

5756
if (MP_IS_2EXPT((unsigned int)radix)) {
5857
if ((err = mp_mul_2d(&A, ((len - mid) -1) * s_floor_ilog2(radix), &A)) != MP_OKAY) goto LTM_ERR;

s_mp_slower_read_radix.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,26 @@ mp_err s_mp_slower_read_radix(mp_int *a, const char *str, int start, int end, in
1111
/* checks are done by caller */
1212

1313
char *_s = (char *)(str + start);
14-
for (i = start; i < end; i++) {
14+
for (i = start; (i < end) && (*_s != '\0'); i++) {
1515
uint8_t y;
1616

1717
char ch = (radix <= 36) ? (char)MP_TOUPPER((int)*_s) : *_s;
1818
unsigned int pos = (unsigned int)(ch - '+');
1919
if (MP_RADIX_MAP_REVERSE_SIZE <= pos) {
20-
err = MP_VAL;
21-
goto LBL_ERR;
20+
if ((*_s != '\0') && (*_s != '\r') && (*_s != '\n')) {
21+
return MP_VAL;
22+
}
23+
break;
2224
}
2325
y = s_mp_radix_map_reverse[pos];
2426
if (y >= radix) {
25-
err = MP_VAL;
26-
goto LBL_ERR;
27+
if ((*_s != '\0') && (*_s != '\r') && (*_s != '\n')) {
28+
return MP_VAL;
29+
}
30+
break;
2731
}
28-
if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) goto LBL_ERR;
29-
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) goto LBL_ERR;
32+
if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) goto LBL_ERR;
33+
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) goto LBL_ERR;
3034
_s++;
3135
}
3236

tommath_class.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,7 @@
10691069
# define MP_DIV_2D_C
10701070
# define MP_DIV_C
10711071
# define MP_EXPT_N_C
1072+
# define MP_GET_I32_C
10721073
# define MP_INIT_C
10731074
# define MP_INIT_I32_C
10741075
# define MP_INIT_MULTI_C

0 commit comments

Comments
 (0)