Skip to content

Fix write_significand buffer size to avoid GCC 16 LTO -Wstringop-overflow (#4767) - #4957

Open
MuskanKhatwani wants to merge 1 commit into
fmtlib:mainfrom
MuskanKhatwani:fix/gcc16-lto-buffer
Open

MuskanKhatwani wants to merge 1 commit into
fmtlib:mainfrom
MuskanKhatwani:fix/gcc16-lto-buffer

Conversation

@MuskanKhatwani

Copy link
Copy Markdown

Description

  • Increase the temporary buffer in write_significand to ensure there is enough headroom for backward writes performed by the digit-writing path under LTO.
  • Change: digits10() + 2 -> digits10() + 3 in include/fmt/format.h.

Rationale

  • GCC 16 with LTO reports -Wstringop-overflow in the floating-point formatting path because the backward-writing routine can touch significand_size + 1 bytes while the previous buffer allocation was tight in one edge case.
  • Increasing the buffer by one element removes the off-by-one risk without changing observable behavior.

Verification

  • Built on Windows (MSYS2 UCRT64) with:
    • gcc 16.2.0, g++ 16.2.0, cmake 4.4.3
    • cmake configured with -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON
  • Example link-time warning excerpt observed during build (for reviewer context):

warning: writing 2 bytes into a region of size 0 [-Wstringop-overflow=]
include/fmt/format.h:1375:11: memcpy(out, digits2(value), 2);
note: at offset [-13, -2] into destination object 'buffer' of size 12
include/fmt/format.h:2659:8: Char buffer[digits10() + 3];

Compatibility & Risk

  • No API or behavioral changes.
  • Minimal internal change to buffer sizing; low risk.

Suggested testing

  • Build with GCC 16 + LTO (-flto) on CI (MSYS2/Ubuntu with GCC 16) to confirm no false-positive -Wstringop-overflow remains.
  • Run unit tests with and without LTO to ensure no regressions.

Notes

  • If maintainers prefer not to change allocation, an alternate approach is to gate a compiler warning suppression for affected toolchains. The buffer-size fix is simpler and safer.

@MuskanKhatwani

Copy link
Copy Markdown
Author

Hi, attaching the full verbose LTO build log (lto-build-log.txt) I captured locally.
Summary:

  • Toolchain: MSYS2 UCRT64, gcc/g++ 16.2.0, CMake 4.4.3
  • Built with -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON
  • The log contains the link-time -Wstringop-overflow warnings observed during LTO.
    Thanks for taking a look. I can also:
  • produce a tiny standalone repro (CMake + source) to reproduce the warning, or
  • run additional builds/tests and attach their logs — tell me which and I’ll add it.
    lto-build-log.txt

@vitaut vitaut left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a false positive but please provide a godbolt repro.

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.

2 participants