Creating Python bytes from a 0-length Golang bytes (Slice_bytes) crashes
- Langage dominant
- Go
- Étoiles
- 2.3k
- Forks
- 136
- Merge moyen
- 14 min
- PR mergées (30 j)
- 2
Description
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).
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Évaluation
Cette issue n'a pas encore été évaluée.