Skip to content

Fix memory leak in utf8_decode via RAII#157

Open
jules-ai wants to merge 1 commit into
p-ranav:masterfrom
jules-ai:jules-fix-utf8-decode-memory-leak
Open

Fix memory leak in utf8_decode via RAII#157
jules-ai wants to merge 1 commit into
p-ranav:masterfrom
jules-ai:jules-fix-utf8-decode-memory-leak

Conversation

@jules-ai
Copy link
Copy Markdown

The utf8_decode function in include/indicators/display_width.hpp used raw new and delete[] to manage the buffer for mbstowcs_s and mbstowcs. This pattern is susceptible to memory leaks if an exception is thrown before delete[] is called, which can pose a security risk in a long-running process.

This commit replaces the manual memory management with std::wstring. By initializing a string of sufficient size, passing its internal buffer to mbstowcs, and resizing it afterwards, the memory is managed safely and exception-safe.

The `utf8_decode` function in `include/indicators/display_width.hpp` used raw `new` and `delete[]` to manage the buffer for `mbstowcs_s` and `mbstowcs`. This pattern is susceptible to memory leaks if an exception is thrown before `delete[]` is called, which can pose a security risk in a long-running process.

This commit replaces the manual memory management with `std::wstring`. By initializing a string of sufficient size, passing its internal buffer to `mbstowcs`, and resizing it afterwards, the memory is managed safely and exception-safe.
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.

1 participant