Skip to content

Fix Python Unicode buffer lifetime in annotation_as_text #774

Description

@coderabbitai

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions