Skip to content

Commit 9c302bc

Browse files
committed
added clean-up for read_radix
1 parent b92f55e commit 9c302bc

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

mp_read_radix.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
1919

2020
mp_err err;
2121
mp_sign sign = MP_ZPOS;
22+
int correction = 0;
23+
size_t slen;
24+
/* should sit at the EOS now */
25+
const char *t;
2226

2327
/* make sure the radix is ok */
2428
if ((radix < 2) || (radix > 64)) {
@@ -33,14 +37,23 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
3337
sign = MP_NEG;
3438
}
3539

40+
slen = s_mp_strlen(str);
41+
t = str + slen - 1;
42+
43+
/* Check input (needed to get rid of unclean input from "mtest") */
44+
while ((*t == '\r') || (*t == '\n') || (*t == '\t')) {
45+
correction++;
46+
t--;
47+
}
48+
3649
mp_zero(a);
3750

3851
/* Try faster version first */
3952
if (MP_HAS(S_MP_FASTER_READ_RADIX)) {
40-
if ((err = s_mp_faster_read_radix(a, str, 0, (int)s_mp_strlen(str) - 1,
53+
if ((err = s_mp_faster_read_radix(a, str, 0, (int)slen - correction,
4154
radix)) != MP_OKAY) goto LTM_ERR;
4255
} 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) - 1,
56+
if ((err = s_mp_slower_read_radix(a, str, 0, (int)slen - correction,
4457
radix)) != MP_OKAY) goto LTM_ERR;
4558
}
4659

s_mp_slower_read_radix.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,17 @@ 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) && (*_s != '\0'); i++) {
14+
for (i = start; (i < end) && (*_s != '\0')&& (*_s != '\r') && (*_s != '\n'); 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-
if ((*_s != '\0') && (*_s != '\r') && (*_s != '\n')) {
21-
return MP_VAL;
22-
}
23-
break;
20+
goto LBL_ERR;
2421
}
2522
y = s_mp_radix_map_reverse[pos];
2623
if (y >= radix) {
27-
if ((*_s != '\0') && (*_s != '\r') && (*_s != '\n')) {
28-
return MP_VAL;
29-
}
30-
break;
24+
goto LBL_ERR;
3125
}
3226
if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) goto LBL_ERR;
3327
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) goto LBL_ERR;

0 commit comments

Comments
 (0)