Summary
annotation_as_text in plugins/python/src/modulewrap.cpp obtains a UTF-8 pointer from a temporary normalized Python Unicode object with PyUnicode_AsUTF8(norm), then decrements norm before constructing/returning the C++ string.
The pointer returned by PyUnicode_AsUTF8 is owned by the Python Unicode object, so it must not be accessed after that object can be destroyed.
Required change
Update annotation_as_text to copy the UTF-8 contents into an owning std::string while norm is still alive. Release norm after the copy, while preserving existing error handling for normalization and UTF-8 conversion failures.
Rationale
This avoids a potential use-after-free when the Py_DECREF(norm) releases the final reference to the Unicode object and invalidates its cached UTF-8 buffer.
Affected area
plugins/python/src/modulewrap.cpp
annotation_as_text(PyObject* pyobj)
Acceptance criteria
- The UTF-8 result is copied into a
std::string before Py_DECREF(norm).
norm is decremented on both success and UTF-8 conversion failure paths.
- Existing behavior for failed normalization or UTF-8 conversion remains unchanged.
- The fix is delivered separately from the
readability-braces-around-statements cleanup.
Backlinks
Summary
annotation_as_textinplugins/python/src/modulewrap.cppobtains a UTF-8 pointer from a temporary normalized Python Unicode object withPyUnicode_AsUTF8(norm), then decrementsnormbefore constructing/returning the C++ string.The pointer returned by
PyUnicode_AsUTF8is owned by the Python Unicode object, so it must not be accessed after that object can be destroyed.Required change
Update
annotation_as_textto copy the UTF-8 contents into an owningstd::stringwhilenormis still alive. Releasenormafter the copy, while preserving existing error handling for normalization and UTF-8 conversion failures.Rationale
This avoids a potential use-after-free when the
Py_DECREF(norm)releases the final reference to the Unicode object and invalidates its cached UTF-8 buffer.Affected area
plugins/python/src/modulewrap.cppannotation_as_text(PyObject* pyobj)Acceptance criteria
std::stringbeforePy_DECREF(norm).normis decremented on both success and UTF-8 conversion failure paths.readability-braces-around-statementscleanup.Backlinks
readability-braces-around-statements#762: clang-tidy: resolvereadability-braces-around-statements#762readability-braces-around-statements#762 (comment)