Skip to content

strtod is locale-dependent (LC_NUMERIC) and breaks JSON float parsing under non-C locales (e.g. French) #241

@sohelzerdoumi

Description

@sohelzerdoumi

double result = strtod(ptr, &endptr);

Currently the code uses strtod to parse floating point numbers from JSON arrays.
However, strtod respects the current process locale (LC_NUMERIC).

This causes incorrect behavior when the user’s locale is not "C":

  • In French locale (fr_FR.UTF-8), "0.1" is rejected because the expected separator is ",".
  • JSON always requires a . (dot) as the decimal separator.
  • As a result, JSON parsing will fail on systems where the locale is set to something other than C or en_US.

Example:

setlocale(LC_NUMERIC, "fr_FR.UTF-8");
double x = strtod("0.1", NULL); // fails, does not parse
double y = strtod("0,1", NULL); // parses as 0.1

Impact:
Parsing JSON arrays of floats will fail depending on the user’s locale, even though the input is valid JSON.

Possible fixes:

  • Use strtod_l / newlocale with a C locale, for locale-independent parsing.
  • Or replace strtod with a locale-independent float parser (e.g. a custom implementation or a JSON parser library).

Expected behavior:
Parsing JSON should always interpret . as the decimal separator, regardless of the system locale.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions