Skip to content

%g with high precision reads past powers_of_10[] for small values #239

Open

Description

@emincan2000

With the default configuration, %g with a high precision and a small value reads past the end of powers_of_10[] and prints garbage:

sprintf_(buf, "%.16g", 0.0001);    // "1"     expected "0.0001"
sprintf_(buf, "%.16g", 0.000123);  // "1"     expected "0.000123"
sprintf_(buf, "%#.16g", 0.001);    // "1.000000000000000000"  expected "0.001000000000000000"
sprintf_(buf, "%.15g", 0.000338938832258952);  // "0"  expected "0.000338938832258952"

ASan:

global-buffer-overflow ... 0 bytes after global variable 'powers_of_10'
    #0 get_components printf.c:699
    #1 print_exponential_number printf.c:1100

In print_exponential_number(), when %g falls back to decimal mode, precision becomes precision - floored_exp10. For a negative exponent this grows past PRINTF_MAX_SUPPORTED_PRECISION, and the following get_components() call indexes powers_of_10[precision] out of bounds. print_floating_point() limits the precision before this point, but the fallback raises it again.

Found by comparing snprintf_() against glibc snprintf() on random format strings and values, built with ASan/UBSan. Seen on master and develop (e2ceeaf).

Activity

  1. added 4 commits that reference this issue on Oct 27, 2026
    aff888f
    b0490d3
    4044189
    4424515
  2. self-assigned this
    on Oct 30, 2026
  3. added
    resolved-on-develop
    bug
    on Oct 30, 2026
  4. added a commit that references this issue on Nov 24, 2026
    12916b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugresolved-on-develop

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions