Skip to content

Commit aeeea0d

Browse files
authored
Merge pull request #277 from libtom/fix-fread
handle EOF condition in mp_fread, fix #163
2 parents fd7cb14 + 6552f55 commit aeeea0d

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

bn_mp_fread.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,27 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
99
{
1010
mp_err err;
1111
mp_sign neg;
12-
int ch, y;
13-
unsigned pos;
14-
15-
/* clear a */
16-
mp_zero(a);
1712

1813
/* if first digit is - then set negative */
19-
ch = fgetc(stream);
14+
int ch = fgetc(stream);
2015
if (ch == (int)'-') {
2116
neg = MP_NEG;
2217
ch = fgetc(stream);
2318
} else {
2419
neg = MP_ZPOS;
2520
}
2621

27-
for (;;) {
28-
pos = (unsigned)(ch - (int)'(');
22+
/* no digits, return error */
23+
if (ch == EOF) {
24+
return MP_ERR;
25+
}
26+
27+
/* clear a */
28+
mp_zero(a);
29+
30+
do {
31+
int y;
32+
unsigned pos = (unsigned)(ch - (int)'(');
2933
if (mp_s_rmap_reverse_sz < pos) {
3034
break;
3135
}
@@ -43,10 +47,9 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
4347
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
4448
return err;
4549
}
50+
} while ((ch = fgetc(stream)) != EOF);
4651

47-
ch = fgetc(stream);
48-
}
49-
if (mp_cmp_d(a, 0uL) != MP_EQ) {
52+
if (!mp_iszero(a)) {
5053
a->sign = neg;
5154
}
5255

tommath_class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@
398398
# define BN_MP_ZERO_C
399399
# define BN_MP_MUL_D_C
400400
# define BN_MP_ADD_D_C
401-
# define BN_MP_CMP_D_C
401+
# define BN_MP_ISZERO_C
402402
#endif
403403

404404
#if defined(BN_MP_FWRITE_C)

0 commit comments

Comments
 (0)