handle EOF condition in mp_fread, fix #163

This commit is contained in:
Daniel Mendler 2019-05-20 08:38:25 +02:00 committed by Steffen Jaeckel
parent fd7cb14ad9
commit 6552f55f78
No known key found for this signature in database
GPG Key ID: AF0CB17621EDAD72
2 changed files with 15 additions and 12 deletions

View File

@ -9,14 +9,9 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
{
mp_err err;
mp_sign neg;
int ch, y;
unsigned pos;
/* clear a */
mp_zero(a);
/* if first digit is - then set negative */
ch = fgetc(stream);
int ch = fgetc(stream);
if (ch == (int)'-') {
neg = MP_NEG;
ch = fgetc(stream);
@ -24,8 +19,17 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
neg = MP_ZPOS;
}
for (;;) {
pos = (unsigned)(ch - (int)'(');
/* no digits, return error */
if (ch == EOF) {
return MP_ERR;
}
/* clear a */
mp_zero(a);
do {
int y;
unsigned pos = (unsigned)(ch - (int)'(');
if (mp_s_rmap_reverse_sz < pos) {
break;
}
@ -43,10 +47,9 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
return err;
}
} while ((ch = fgetc(stream)) != EOF);
ch = fgetc(stream);
}
if (mp_cmp_d(a, 0uL) != MP_EQ) {
if (!mp_iszero(a)) {
a->sign = neg;
}

View File

@ -398,7 +398,7 @@
# define BN_MP_ZERO_C
# define BN_MP_MUL_D_C
# define BN_MP_ADD_D_C
# define BN_MP_CMP_D_C
# define BN_MP_ISZERO_C
#endif
#if defined(BN_MP_FWRITE_C)