GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gcc/cp/reflection

Fix nits in C++ reflection string translation and length handling.

Corrects memory management and string length interpretation in GCC's C++ reflection implementation.

This commit addresses two issues in GCC’s C++ reflection subsystem. First, it ensures that dynamically allocated string buffers are properly freed after use in cpp_translate_string. Second, it corrects the interpretation of TREE_STRING_LENGTH to align with its documented behavior (size, not null-terminated length), preventing off-by-one errors when calculating string lengths.

In Details

In gcc/cp/reflect.cc, this patch refactors string handling within reflection evaluation functions. It corrects a memory leak by ensuring ostr.text is freed when cpp_translate_string succeeds. Additionally, it adjusts how string lengths are handled, specifically in eval_exception__S_exception_cvt_tofrom_utf8, to correctly use TREE_STRING_LENGTH as a size rather than assuming it's a null-terminated string length plus one, aligning with the tree.h definition.

For Context
C++ reflection
A feature that allows a program to inspect and modify its own structure and behavior at runtime or compile time. GCC's implementation is part of its experimental support for C++26 reflection proposals.
TREE_STRING_LENGTH
A macro or field in GCC's internal representation (tree) that stores the length of a string constant.
ostr.text
A pointer to a character buffer used for output strings within the reflection code.
Filed Under: c++reflectionmemory-management