Skip to content

Commit

Permalink
[Fix] Avoid another overflow in fpconv
Browse files Browse the repository at this point in the history
Issue: #2904
  • Loading branch information
vstakhov committed May 18, 2019
1 parent 79cf1be commit 31a1224
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions contrib/fpconv/fpconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,32 @@ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg,
offset = -offset;
dest[0] = '0';
dest[1] = '.';
memset(dest + 2, '0', offset);
memcpy(dest + offset + 2, digits, ndigits);

return ndigits + 2 + offset;
/* We have up to 21 characters in output available */
if (offset + ndigits <= 21) {
memset(dest + 2, '0', offset);
memcpy(dest + offset + 2, digits, ndigits);

return ndigits + 2 + offset;
}
else {
/* Overflow */
dest[2] = '0';
return 3;
}

/* fp > 1.0 */
} else {
memcpy(dest, digits, offset);
dest[offset] = '.';
memcpy(dest + offset + 1, digits + offset, ndigits - offset);

return ndigits + 1;
/* Overflow check */
if (ndigits <= 23) {
dest[offset] = '.';
memcpy(dest + offset + 1, digits + offset, ndigits - offset);
return ndigits + 1;
}

return offset;
}
}

Expand Down

0 comments on commit 31a1224

Please sign in to comment.