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

Creating Python bytes from a 0-length Golang bytes (Slice_bytes) crashes #359

Open
Scusemua opened this issue Jul 16, 2024 · 0 comments
Open

Comments

@Scusemua
Copy link

Scusemua commented Jul 16, 2024

If you attempt to create a Python bytes from a Golang Slice_bytes of length 0, then the application will crash.

Consider the following:

go_code.go

func CreateBytes(len byte) []byte {
	res := make([]byte, len)
	for i := (byte)(0); i < len; i++ {
		res[i] = i
	}
	return res
}

main.py

from go_code import CreateBytes
from go import Slice_byte

# This one is fine.
a = CreateBytes(10)  
# This one is not.
b = CreateBytes(0)

print(f"Go slice a: {a}") 
# Output: go.Slice_byte len: 10 handle: 4 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(f"Go slice b: {b}") 
# Output: go.Slice_byte len: 0 handle: 5 []

print(f"Go bytes to Python (a): {bytes(a)}") 
# Output: Go bytes to Python (a): b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t'

# The program will crash upon execution this next line of code.
print(f"Go bytes to Python (b): {bytes(b)}") 

In my case, this crashes (via SIGABRT) with no stack trace, no error messages of any sort.

Perhaps a simple/short-term fix could be to modify the __bytes__ method of Slice_bytes such that it simply returns an empty bytes() if len(self) == 0? This alleviates the problem for me, at least in the particular context present within the application I'm developing.

I'm not sure what line is triggering the crash, specifically. I assume it's ultimately something in Slice_byte_to_bytes or one of the functions called thereis (e.g., deptrFromHandle_Slice_byte, or maybe the C Python PyBytes_FromStringAndSize function doesn't like when you pass a length/size of 0).

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

No branches or pull requests

1 participant