Skip to content

THRIFT-6076: Fix GCC 14 LTO false-positive stringop-overflow in compiler generators#3611

Open
bigjust wants to merge 2 commits into
apache:masterfrom
bigjust:THRIFT-6076-fix-gcc14-lto-stringop
Open

THRIFT-6076: Fix GCC 14 LTO false-positive stringop-overflow in compiler generators#3611
bigjust wants to merge 2 commits into
apache:masterfrom
bigjust:THRIFT-6076-fix-gcc14-lto-stringop

Conversation

@bigjust

@bigjust bigjust commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Summary

GCC 14 with LTO (-flto=auto -ffat-lto-objects) produces false-positive -Werror=stringop-overflow / -Werror=stringop-overread errors at link time when chained std::string() + "literal" + "literal" expressions in the compiler generator sources are inlined across translation units. The code is correct; this is a GCC 14 LTO inter-procedural analysis limitation.

Tracked in: https://issues.apache.org/jira/browse/THRIFT-6076

Affected functions

File Function
t_javame_generator.cc java_type_imports(), java_thrift_imports()
t_haxe_generator.cc haxe_type_imports(), haxe_thrift_imports()
t_js_generator.cc autogen_comment()
t_ocaml_generator.cc ocaml_autogen_comment()
t_perl_generator.cc autogen_comment()
t_erl_generator.cc erl_autogen_comment()
t_go_generator.cc go_autogen_comment()
t_delphi_generator.cc autogen_comment()
t_rb_generator.cc rb_autogen_comment()
t_xsd_generator.cc xml_autogen_comment()

Fix

Replace chained operator+ on temporaries with:

  • Adjacent string literal concatenation for pure-literal returns (resolved entirely at compile time, no temporary objects, no operator+ at all).
  • Single operator+ call for returns that embed THRIFT_VERSION, with compile-time-concatenated literal strings on both sides of the variable — this gives GCC LTO a correctly-sized left operand to analyse.

Example (pure literals — t_javame_generator.cc)

// Before
return string() + "import java.util.Hashtable;\n" + "import java.util.Vector;\n"
       + "import java.util.Enumeration;\n\n";

// After
return "import java.util.Hashtable;\n"
       "import java.util.Vector;\n"
       "import java.util.Enumeration;\n\n";

Example (contains THRIFT_VERSION — t_js_generator.cc)

// Before
return std::string("//\n") + "// Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n"
       + "//\n" + "// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n"
       + "//\n";

// After
return "//\n"
       "// Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n"
       "//\n"
       "// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n"
       "//\n";

Copilot AI review requested due to automatic review settings June 30, 2026 19:27
@mergeable mergeable Bot added delphi erlang golang Pull requests that update Go code haxe labels Jun 30, 2026
@mergeable mergeable Bot added java Pull requests that update Java code javascript Pull requests that update Javascript code nodejs typescript ocaml perl ruby Pull requests that update Ruby code compiler labels Jun 30, 2026

Copilot AI 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.

Pull request overview

This PR updates several C++ compiler generator helpers to avoid GCC 14 LTO false-positive -Werror=stringop-overflow/-overread diagnostics by eliminating chained std::string temporary concatenations in returned string constants.

Changes:

  • Replaced std::string() + "..." + "..." patterns with adjacent string-literal concatenation for pure-literal returns.
  • Refactored autogenerated-header comment construction in multiple generators to reduce/avoid operator+ chains (notably where THRIFT_VERSION is embedded).
  • Simplified a few returns that previously forced std::string temporaries (e.g., Go generator autogen comment).

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
compiler/cpp/src/thrift/generate/t_xsd_generator.cc Refactors XML autogen comment string construction.
compiler/cpp/src/thrift/generate/t_rb_generator.cc Refactors Ruby autogen comment string construction.
compiler/cpp/src/thrift/generate/t_perl_generator.cc Refactors Perl autogen comment string construction.
compiler/cpp/src/thrift/generate/t_ocaml_generator.cc Refactors OCaml autogen comment string construction.
compiler/cpp/src/thrift/generate/t_js_generator.cc Refactors JS autogen comment string construction.
compiler/cpp/src/thrift/generate/t_javame_generator.cc Converts JavaME import lists to adjacent-literal concatenation.
compiler/cpp/src/thrift/generate/t_haxe_generator.cc Converts Haxe import lists to adjacent-literal concatenation.
compiler/cpp/src/thrift/generate/t_go_generator.cc Simplifies Go autogen comment return expression.
compiler/cpp/src/thrift/generate/t_erl_generator.cc Refactors Erlang autogen comment string construction.
compiler/cpp/src/thrift/generate/t_delphi_generator.cc Refactors Delphi autogen comment string construction.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread compiler/cpp/src/thrift/generate/t_xsd_generator.cc
Comment thread compiler/cpp/src/thrift/generate/t_rb_generator.cc
Comment thread compiler/cpp/src/thrift/generate/t_perl_generator.cc
Comment thread compiler/cpp/src/thrift/generate/t_ocaml_generator.cc
Comment thread compiler/cpp/src/thrift/generate/t_js_generator.cc
Comment thread compiler/cpp/src/thrift/generate/t_go_generator.cc Outdated
Comment thread compiler/cpp/src/thrift/generate/t_erl_generator.cc
Comment thread compiler/cpp/src/thrift/generate/t_delphi_generator.cc
…ler generators

GCC 14 with LTO (-flto=auto) produces false-positive -Werror=stringop-overflow
and -Werror=stringop-overread diagnostics at link time when chained
std::string() + "literal" + "literal" expressions are inlined across
translation units.

The root cause is a GCC 14 LTO inter-procedural analysis limitation that
misestimates intermediate std::string buffer sizes for this idiom. The code
is correct and produces the right result; the warnings are spurious.

Fix all affected sites in the compiler generator sources by replacing the
chained operator+ construction with:
- Adjacent string literal concatenation (resolved at compile time) for
  pure-literal returns.
- A single operator+ call for returns that embed THRIFT_VERSION, with
  compile-time-concatenated literals on either side of the variable.

Affected functions:
  t_javame_generator.cc : java_type_imports(), java_thrift_imports()
  t_haxe_generator.cc   : haxe_type_imports(), haxe_thrift_imports()
  t_js_generator.cc     : autogen_comment()
  t_ocaml_generator.cc  : ocaml_autogen_comment()
  t_perl_generator.cc   : autogen_comment()
  t_erl_generator.cc    : erl_autogen_comment()
  t_go_generator.cc     : go_autogen_comment()
  t_delphi_generator.cc : autogen_comment()
  t_rb_generator.cc     : rb_autogen_comment()
  t_xsd_generator.cc    : xml_autogen_comment()

See: https://issues.apache.org/jira/browse/THRIFT-6076
@bigjust bigjust force-pushed the THRIFT-6076-fix-gcc14-lto-stringop branch from 786b4a1 to 72fbd71 Compare June 30, 2026 19:38
@bigjust bigjust requested a review from Copilot June 30, 2026 19:41

Copilot AI 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.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated no new comments.

@Jens-G

Jens-G commented Jun 30, 2026

Copy link
Copy Markdown
Member

Code review

No verified issues found. Checked for bugs and CLAUDE.md compliance; all candidate findings were refuted by adversarial verification.

🤖 Generated with Claude Code

@Jens-G

Jens-G commented Jul 4, 2026

Copy link
Copy Markdown
Member

Something is sill broken here.

@bigjust bigjust requested a review from jimexist as a code owner July 6, 2026 17:14
@mergeable mergeable Bot added the github_actions Pull requests that update GitHub Actions code label Jul 6, 2026
…ompiler-cpp SCA job

cppcheck needs -I compiler/cpp/src to resolve project headers
like thrift/version.h (which defines THRIFT_VERSION).
Without it, the adjacent string-literal concatenation pattern
introduced in this PR triggers unknownMacro errors.

Also suppress danglingTempReference false positive in
t_cpp_generator.cc where a ternary between a derived-type
lvalue and a base-class lvalue produces a valid reference,
not a dangling temporary.

Client: sca
Copilot AI review requested due to automatic review settings July 6, 2026 19:21
@bigjust bigjust force-pushed the THRIFT-6076-fix-gcc14-lto-stringop branch from 83694ba to e8a9faa Compare July 6, 2026 19:21

Copilot AI 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.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler delphi erlang github_actions Pull requests that update GitHub Actions code golang Pull requests that update Go code haxe java Pull requests that update Java code javascript Pull requests that update Javascript code nodejs ocaml perl ruby Pull requests that update Ruby code typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants