go-python / go-python/gopy

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

Aperta
#359 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Go
Stelle
2.3k
Fork
136
Merge medio
14m
PR unite (30g)
2

Descrizione

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

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.