go-python / go-python/gopy

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

オープン
#359 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Go
スター
2.3k
フォーク
136
平均マージ
14分
マージ済み PR(30日)
2

説明

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

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。