Fix crash when converting 0-length Go []byte to Python bytes#394
Draft
b-long wants to merge 1 commit into
Draft
Conversation
…359) `Slice_byte_to_bytes` indexed `s[0]` unconditionally, causing a runtime panic on empty slices. Guard with an early return via `PyBytes_FromStringAndSize(nil, 0)` which the CPython API handles correctly.
There was a problem hiding this comment.
Pull request overview
This PR fixes a runtime panic when converting a zero-length Go []byte (go.Slice_byte) into Python bytes by avoiding &s[0] indexing on empty slices, and adds a regression check for issue #359.
Changes:
- Add an early return in generated
Slice_byte_to_bytesto safely create empty Pythonbytesforlen(s) == 0. - Extend the
_examples/gobytesexample to exercisebytes(empty_slice)and assert it producesb''. - Update the corresponding golden output in
main_test.go.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
bind/gen_slice.go |
Guards zero-length []byte conversions by returning an empty PyBytes object before indexing. |
_examples/gobytes/test.py |
Adds a regression test path that converts an empty Go byte slice to Python bytes. |
main_test.go |
Updates expected output to include the new empty-slice test prints. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| empty = gobytes.CreateBytes(0) | ||
| print("Go empty slice: ", empty) | ||
| empty_bytes = bytes(empty) | ||
| assert empty_bytes == b"", f"expected b'', got {empty_bytes!r}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Relates to issue 359
Slice_byte_to_bytesindexeds[0]unconditionally, causing a runtime panic on empty slices. Guard with an early return viaPyBytes_FromStringAndSize(nil, 0)which the CPython API handles correctly.