Flatbuffers in Go and C++

xuanbao · · 410 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>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. </p> <p>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&#39;d be happy to share the fbs file and the code if it helps but didn&#39;t want to spam the forum.</p> <p>Thanks!</p> <hr/>**评论:**<br/><br/>_zchee_: <pre><p>it means, dump fbs binary with C++ and Go. but C++ can&#39;t read Go&#39;s generated binary? I don&#39;t know C++implementation, but I have Go&#39;s project that using flatbuffer for libclang, and dump data into leveldb. <a href="https://github.com/zchee/clang-server" rel="nofollow">https://github.com/zchee/clang-server</a></p> <p>So I might be able to help you.</p></pre>echophant: <pre><p>Knowing very little about Flatbuffers (but being familiar with Cap&#39;n Proto and Protocol Buffers), I would think that the same data serialized in C++ and Go should be bit-for-bit identical.</p> <p>That would make me think that the issue is that the data isn&#39;t being read from file correctly, perhaps being parsed as text instead of binary. Sharing the data/code might help here.</p></pre>therealfakemoot: <pre><p>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&#39;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&#39;t post earlier.</p></pre>zhenjl: <pre><p>Hi all, I figured out my issue finally. I was pushing the offsets to a slice declared as</p> <pre><code>var treeOffsets, nodeOffsets []flatbuffers.UOffsetT </code></pre> <p>And for some reason appending to these didn&#39;t take, and I had to change to</p> <pre><code>treeOffsets := make([]flatbuffers.UOffsetT, 0) nodeOffsets := make([]flatbuffers.UOffsetT, 0) </code></pre> <p>And this fixed it. I thought append will allocate a new slice if there&#39;s no room, but I may have misunderstood how it works.</p> <p>The final output is still not exactly the same for the 2 languages, but as <a href="/u/therealfakemoot" rel="nofollow">/u/therealfakemoot</a> said, the two may not be byte-to-byte the same anyway. </p></pre>noonexx: <pre><p>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.</p></pre>

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

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