【问题标题】:protoc-c: Nested structure with optional string throws seg faultprotoc-c:带有可选字符串的嵌套结构引发段错误
【发布时间】:2015-02-20 07:56:43
【问题描述】:

为我的 C 语言代码试用 Google 协议缓冲区。

messagefile.proto
===================
mesage othermessage
{
  optional string otherstring = 1;
}

message onemessage
{
  optional string messagestring = 1;
  optional int32 aninteger      = 2;
  optional othermessage otr_message= 3;
}

===============================================

--> protoc-c messagefile.proto --c_out=./ 这导致了两个文件

--> messagefile.pb-c.c 和 messagefile.pb-c.h

现在我的代码文件将尝试使用 简单示例.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "messagefile.pb-c.h"
#include <stdbool.h>

int main(int argc, const char * argv[])
{
    onemessage msg = ONE__MESSAGE__INIT; //from generated .h  code file
    void *buf;
    unsigned int len;
    char *ptr;

    //integer initialization 
    msg.has_aninteger = true;
    msg.aninteger = 1;

    //accessing the string in onemessage
    msg.messagestring = malloc(sizeof("a simple string"));
    strncpy(msg.messagestring,"a simple string",strlen("a simple string"));

    //trying to initialize the string in the nested structure othermessage        
    msg.otr_message = malloc(sizeof(othermessage));
    msg.otr_message->otherstring = malloc(sizeof("a not so simple string"));
    strncpy(msg.otr_message->otherstring,"a not so simple string",strlen("a not so simple string"));

    //lets find the length of the packed structure
    len = one_message__get_packed_size(&msg); //from generated .h code
 
    //lets arrange for as much size as len
    buf = malloc(len);

    //lets get the serialized structure in buf
    one_message__pack_to_buffer(&msg,buf); //from generated code

    //write it to a stream, for now the screen
    fwrite(buf,len,1,stdout);

    //free buffer
    free(buf);
     
     return 0;
}

我将它编译为 gcc -o testout messagefile.pb-c.c simpleexample.c -lprotobuf-c

我面临的问题是在尝试初始化嵌套的 othermessage 变量然后调用 get_packed_size 时会引发分段错误。

我尝试了各种组合,我可以说,每当嵌套类中有字符串时,我都面临着使用 google protoc 访问那些字符串的问题。 我错过了什么吗?有什么问题吗。

谁能帮忙。

注意:可能存在一些一般语法错误,请忽略它们。

谢谢。

【问题讨论】:

    标签: c serialization deserialization protocols protocol-buffers


    【解决方案1】:

    注意:可能存在一些一般语法错误,请忽略它们。

    Err...它们有点难以忽略,因为您的代码无法编译:-)

    无论如何,除了语法错误之外,您还需要对代码进行一些更正。为了使用字段otr_message,仅malloc() 是不够的。您还需要对其进行初始化,以便消息中的标头获得正确的值。这是通过init() 完成的,如下所示:

    //trying to initialize the string in the nested structure othermessage        
    msg.otr_message = malloc(sizeof(othermessage));
    othermessage__init(msg.otr_message);
    

    然后你使用错误的函数来打包你自己的数组。正如here 解释的那样,您需要使用pack() 而不是pack_to_buffer(),如下所示:

    //lets get the serialized structure in buf
    onemessage__pack(&msg,buf); //from generated code
    

    最后,您的strncpy() 调用出现了错误。使用strlen() 计算的长度不包括您需要的空终止符。所以你需要使用strlen()+1或者使用sizeof(),像这样:

    strncpy(msg.messagestring,"a simple string",sizeof("a simple string"));
    

    进行这些更改后,该示例对我有用:

    $ ./testout 
    
    a simple string
    a not so simple string
    

    【讨论】:

    • 非常感谢..您的指示帮助我解决了问题。然而,我仍然希望以更好的方式编写协议缓冲区文档。再次感谢。 :)
    • 不客气。由于您是 Stack Overflow 的新手,请查看What should I do when someone answers my question?
    猜你喜欢
    • 2021-02-13
    • 2016-01-16
    • 1970-01-01
    • 2021-02-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多