Flatbuffers in Go and C++

xuanbao · 2017-05-24 03:00:03 · 468 次点击    
这是一个分享于 2017-05-24 03:00:03 的资源,其中的信息可能已经有所发展或是发生改变。

Hi all, I am having issues with flatbuffers that are written in Go and segfaulting in C++. In Go, I can read C++ written flatbuffers file. But for in C++, it segfaluts trying to read a Go-written flatbuffers file.

I have 2 questions: 1. Has anyone tried doing this before and succeeded? C++ generated flatbuffers code is a lot more than the Go equivalent. 2. I'd be happy to share the fbs file and the code if it helps but didn't want to spam the forum.

Thanks!


评论:

zchee:

it means, dump fbs binary with C++ and Go. but C++ can't read Go's generated binary? I don't know C++implementation, but I have Go's project that using flatbuffer for libclang, and dump data into leveldb. https://github.com/zchee/clang-server

So I might be able to help you.

echophant:

Knowing very little about Flatbuffers (but being familiar with Cap'n Proto and Protocol Buffers), I would think that the same data serialized in C++ and Go should be bit-for-bit identical.

That would make me think that the issue is that the data isn't being read from file correctly, perhaps being parsed as text instead of binary. Sharing the data/code might help here.

therealfakemoot:

I thought the same thing, but the Flatbuffers specification actually allows the serializer to control the ordering of fields to allow for {back,for}wards compatibility. So, technically, you can't rely on doing a byte-for-byte diff to compare a flatbuffer payload on-the-wire, since the ordering of member fields is not defined. My lack of better ideas is why I didn't post earlier.

zhenjl:

Hi all, I figured out my issue finally. I was pushing the offsets to a slice declared as

var treeOffsets, nodeOffsets []flatbuffers.UOffsetT

And for some reason appending to these didn't take, and I had to change to

treeOffsets := make([]flatbuffers.UOffsetT, 0)
nodeOffsets := make([]flatbuffers.UOffsetT, 0)

And this fixed it. I thought append will allocate a new slice if there's no room, but I may have misunderstood how it works.

The final output is still not exactly the same for the 2 languages, but as /u/therealfakemoot said, the two may not be byte-to-byte the same anyway.

noonexx:

Please keep in mind, that you still have the segfault in the C++ code. I strongly suggest fixing it, if it is anywhere near production use. In the best case, one could send broken data to perform a denial of service attack.


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

468 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传