go-python / gopy

gopy generates a CPython extension module from a go package.
BSD 3-Clause "New" or "Revised" License
2.03k stars 112 forks source link

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

Open Scusemua opened 3 months ago

Scusemua commented 3 months ago

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).