Skip to content

Commit c893d21

Browse files
authored
Merge pull request #439 from libtom/radix-code-cleanup
mp_radix off-by-one error and other related code-cleanup
2 parents 7e47ae6 + bbb1780 commit c893d21

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

demo/test.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ static int test_mp_read_radix(void)
11381138

11391139
DO(mp_read_radix(&a, "123456", 10));
11401140

1141-
DO(mp_to_radix(&a, buf, SIZE_MAX, &written, 10));
1141+
DO(mp_to_radix(&a, buf, sizeof(buf), &written, 10));
11421142
printf(" '123456' a == %s, length = %zu", buf, written);
11431143

11441144
/* See comment in mp_to_radix.c */
@@ -1153,11 +1153,11 @@ static int test_mp_read_radix(void)
11531153
buf, written, mp_error_to_string(err));
11541154
*/
11551155
DO(mp_read_radix(&a, "-123456", 10));
1156-
DO(mp_to_radix(&a, buf, SIZE_MAX, &written, 10));
1156+
DO(mp_to_radix(&a, buf, sizeof(buf), &written, 10));
11571157
printf("\r '-123456' a == %s, length = %zu", buf, written);
11581158

11591159
DO(mp_read_radix(&a, "0", 10));
1160-
DO(mp_to_radix(&a, buf, SIZE_MAX, &written, 10));
1160+
DO(mp_to_radix(&a, buf, sizeof(buf), &written, 10));
11611161
printf("\r '0' a == %s, length = %zu", buf, written);
11621162

11631163
while (0) {
@@ -1335,7 +1335,7 @@ static int test_mp_reduce_2k_l(void)
13351335
mp_int a, b, c, d;
13361336
int cnt;
13371337
char buf[4096];
1338-
size_t length[1];
1338+
size_t length;
13391339
DOR(mp_init_multi(&a, &b, NULL));
13401340
/* test the mp_reduce_2k_l code */
13411341
# if LTM_DEMO_TEST_REDUCE_2K_L == 1
@@ -1353,9 +1353,8 @@ static int test_mp_reduce_2k_l(void)
13531353
# else
13541354
# error oops
13551355
# endif
1356-
*length = sizeof(buf);
1357-
DO(mp_to_radix(&a, buf, length, 10));
1358-
printf("\n\np==%s, length = %zu\n", buf, *length);
1356+
DO(mp_to_radix(&a, buf, sizeof(buf), &length, 10));
1357+
printf("\n\np==%s, length = %zu\n", buf, length);
13591358
/* now mp_reduce_is_2k_l() should return */
13601359
if (mp_reduce_is_2k_l(&a) != 1) {
13611360
printf("mp_reduce_is_2k_l() return 0, should be 1\n");

mp_fread.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
3535
uint8_t y;
3636
unsigned pos;
3737
ch = (radix <= 36) ? MP_TOUPPER(ch) : ch;
38-
pos = (unsigned)(ch - (int)'(');
39-
if (MP_RMAP_REVERSE_SIZE < pos) {
38+
pos = (unsigned)(ch - (int)'+');
39+
if (MP_RMAP_REVERSE_SIZE <= pos) {
4040
break;
4141
}
4242

4343
y = s_mp_rmap_reverse[pos];
4444

45-
if ((y == 0xff) || (y >= radix)) {
45+
if (y >= radix) {
4646
break;
4747
}
4848

mp_radix_smap.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66
/* chars used in radix conversions */
77
const char s_mp_rmap[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
88
const uint8_t s_mp_rmap_reverse[] = {
9-
0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, /* ()*+,-./ */
10-
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 01234567 */
11-
0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 89:;<=>? */
12-
0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, /* @ABCDEFG */
13-
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, /* HIJKLMNO */
14-
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, /* PQRSTUVW */
15-
0x21, 0x22, 0x23, 0xff, 0xff, 0xff, 0xff, 0xff, /* XYZ[\]^_ */
16-
0xff, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, /* `abcdefg */
17-
0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, /* hijklmno */
18-
0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, /* pqrstuvw */
19-
0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, /* xyz{|}~. */
9+
0x3e, 0xff, 0xff, 0xff, 0x3f, 0x00, 0x01, 0x02, 0x03, 0x04, /* +,-./01234 */
10+
0x05, 0x06, 0x07, 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, /* 56789:;<=> */
11+
0xff, 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, /* ?@ABCDEFGH */
12+
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, /* IJKLMNOPQR */
13+
0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0xff, 0xff, /* STUVWXYZ[\ */
14+
0xff, 0xff, 0xff, 0xff, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, /* ]^_`abcdef */
15+
0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, /* ghijklmnop */
16+
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d /* qrstuvwxyz */
2017
};
2118
MP_STATIC_ASSERT(correct_rmap_reverse_size, sizeof(s_mp_rmap_reverse) == MP_RMAP_REVERSE_SIZE)
2219
#endif

mp_read_radix.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
3333
*/
3434
uint8_t y;
3535
char ch = (radix <= 36) ? (char)MP_TOUPPER((int)*str) : *str;
36-
unsigned pos = (unsigned)(ch - '(');
37-
if (MP_RMAP_REVERSE_SIZE < pos) {
36+
unsigned pos = (unsigned)(ch - '+');
37+
if (MP_RMAP_REVERSE_SIZE <= pos) {
3838
break;
3939
}
4040
y = s_mp_rmap_reverse[pos];
@@ -43,7 +43,7 @@ mp_err mp_read_radix(mp_int *a, const char *str, int radix)
4343
* and is less than the given radix add it
4444
* to the number, otherwise exit the loop.
4545
*/
46-
if ((y == 0xff) || (y >= radix)) {
46+
if (y >= radix) {
4747
break;
4848
}
4949
if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {

tommath_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ MP_PRIVATE void s_mp_copy_digs(mp_digit *d, const mp_digit *s, int digits);
193193
MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR;
194194
MP_PRIVATE void s_mp_rand_jenkins_init(uint64_t seed);
195195

196-
#define MP_RMAP_REVERSE_SIZE 88u
196+
#define MP_RMAP_REVERSE_SIZE 80u
197197
extern MP_PRIVATE const char s_mp_rmap[];
198198
extern MP_PRIVATE const uint8_t s_mp_rmap_reverse[];
199199
extern MP_PRIVATE const mp_digit s_mp_prime_tab[];

0 commit comments

Comments
 (0)