Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent segfault on unicode surrogate characters #68

Merged
merged 1 commit into from
Jan 12, 2025

Conversation

mosquito
Copy link
Owner

When writing strings containing surrogate characters (e.g. \udfff) to the journal, the program would segfault instead of raising an exception. This occurred because _send() attempted to free uninitialized memory when encode() raised UnicodeEncodeError.

Changes:

  • Use PyMem_Calloc instead of PyMem_Malloc for cstring_list to ensure zero-initialized memory
  • Add NULL check before PyMem_Free to safely handle partially allocated memory
  • Add missing imports in async_reader.py

Example of previously failing code that now works safely:

from cysystemd.journal import write
write("Hello, \udfff world!")  # Now raises ValueError instead of segfault

fixes #67

When writing strings containing surrogate characters (e.g. \udfff) to the journal,
the program would segfault instead of raising an exception. This occurred because
_send() attempted to free uninitialized memory when encode() raised UnicodeEncodeError.

Changes:
- Use PyMem_Calloc instead of PyMem_Malloc for cstring_list to ensure zero-initialized memory
- Add NULL check before PyMem_Free to safely handle partially allocated memory
- Add missing imports in async_reader.py

Example of previously failing code that now works safely:

```python
from cysystemd.journal import write
write("Hello, \udfff world!")  # Now raises ValueError instead of segfault
```
@mosquito mosquito merged commit 9512a56 into master Jan 12, 2025
1 check passed
@mosquito mosquito deleted the segfault-on-unicode-surrogate-characters branch January 12, 2025 16:01
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.

Trying to write Unicode string to journal with a surrogate character causes crash.
1 participant