Skip to content

Fix overflow in strtod mantissa read when sizeof(int) is 2 #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mlpce
Copy link

@mlpce mlpce commented Jun 16, 2025

The mantissa read loop in strtod reads 18 digits using two ints to convert nine digits each. When sizeof(int) is 2 the conversion value overflows. To fix this the types of frac1 and frac2 have been changed to long.

Example:
printf("val f: %f\n", strtof("0.123456789012345678",NULL));
printf("val d: %f\n", strtod("0.123456789012345678",NULL));

Without fix:
val f: 0.000249
val d: 0.000249

With fix:
val f: 0.123457
val d: 0.123457

The mantissa read loop in strtod reads 18 digits using two ints to convert nine
digits each. When sizeof(int) is 2 the conversion value overflows. To fix this
the types of frac1 and frac2 have been changed to long.

Example:
  printf("val f: %f\n", strtof("0.123456789012345678",NULL));
  printf("val d: %f\n", strtod("0.123456789012345678",NULL));

Without fix:
  val f: 0.000249
  val d: 0.000249

With fix:
  val f: 0.123457
  val d: 0.123457
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant