0

I have a .proto file that looks like this:

message ObjValue
{
// ...
    optional bytes byteval    = 6 [max_size = 256]; // arbitrary structure or message
// ...

I use this proto encoding to send structs that change. This struct basically contains ints and strings (a null-terminated char array).

The sender sends the struct doing something like this:

   // Here I create the struct and fill it with integers and strings
   struct flow_msg *flow_msg = malloc(sizeof(struct flow_msg));
    flow_msg->dst_addr        = 1;
    flow_msg->src_addr        = 2;
    flow_msg->src_ap_name     = strdup(src_ap_name);

    // Here I save the length of the struct and a void pointer that points to the struct
    struct ser_obj_value *obj_value = malloc(sizeof(struct ser_obj_value));
    obj_value->size = sizeof(struct flow_msg) + strlen(src_ap_name) + 1; // +1 because of '\0'
    obj_value->data = flow_msg;

Then, the receiver gets the message, decodes it using nanopb and then:

int
handle_msg(void *f_msg)
{
    struct flow_msg *flow_msg = (struct flow_msg *)f_msg;
}

At that point, if I try to read the value of an integer there's no problem (flow_msg->dst_addr, for example), but if I want to read the string value, I get a null pointer because the flow_msg->src_ap_name is empty.

I'm lost in terms of how I should properly encode/decode the string value... Really don't know what I'm missing here... Any clues?

  • Your code is hard to follow, but I think the main thing is that your structure (and thus the protobuf message) does not actually contain the string, only a pointer to it. Once you deserialize it on the other side, the pointer is just to a random location in memory. – jpa Nov 22 '21 at 11:38
  • I'm sorry, I actually only wrote the relevant code chunks in order to make it more understandable. The `nanopb` encoding part is not there because is well tested and I'm pretty sure that it works well. But then, if the problem is in the encoder side, am I doing something wrong in the code I wrote? – Sergio Giménez Nov 22 '21 at 12:23
  • 3
    Is the type of the `src_ap_name` field `char *` or similar? In that case, you are just sending a memory address over. And because the other application does not share the same memory, that memory address will be useless. Kind of related to [this question](https://stackoverflow.com/questions/31084673/representing-pointer-variables-in-protobuf-c) – jpa Nov 22 '21 at 12:29
  • Yes, is a `char *` type. Thanks for pointing out, what you say makes sense. But by doing `strdup()` I'm already malloc-ing the string in memory, so I should be saving the string in memory already. – Sergio Giménez Nov 22 '21 at 14:31
  • Sure, it is in the memory on the machine that is sending. But it is completely elsewhere in memory than the struct, and the struct is the only thing you are sending over. – jpa Nov 22 '21 at 15:37

1 Answers1

0

First, I realize now how silly my question was. Actually, I'm not deleting it because @jpa took some time to try to answer my doubts.

Mainly the problem was that I was trying to send pointers (i.e., memory addresses) and then the receiver of course did not know what to do with this random memory address. Also, the problem was that I was "unlucky" that probably because I was using the same machine and same compiler in the receiver side some pointers were actually properly dereferenced, and that confused me a lot.