问题描述
我正在使用一个名为 GEOS 的 C 库,它提供了一个 C 支持的实现来处理几何运算。在某些反射操作期间使用了包含此 C 类型的结构,但在升级到 Go v1.15.4(或之后的任何版本)时,这会导致崩溃。
我已经隔离了这个问题,看起来当使用反射 Type
创建新的反射 Value
时,对 Interface()
的后续调用会导致 {{1} }:
panic: bad indir
真正的恐慌来自 var test *C.GEOSGeometry
reflectType := reflect.ValueOf(test).Type().Elem()
value := reflect.New(reflectType)
// Panics in 1.15.4+
value.Interface()
内的 func packEface
内,发生以下情况:
reflect/value.go
我在这里做的事情似乎不正确,还是更有可能是 switch {
case ifaceIndir(t):
if v.flag&flagIndir == 0 { // here v.flag = 22 and flagIndir = 128
panic("bad indir")
}
...
中引入的错误?
解决方法
I reported the issue to the Golang GitHub
经过进一步调查,在 C 代码中,GEOSGeometry
被定义为不完整的结构类型:
typedef struct GEOSGeom_t GEOSGeometry;
开发人员创建了一个补丁,以便在更好的地方失败,而不是在调用 Interface()
时随机恐慌