Message is not encoded correctly when an enum value set to zero

347 views
Skip to first unread message

Michael Videlgauz

unread,
Sep 13, 2011, 4:39:30 AM9/13/11
to Protocol Buffers
I have a message definition with enum type and field in it. When my
application sets this field to value that is equal to 0 the ENTIRE
message arrives to receiver (after serialization and de-serialization)
with ALL fields reset (all integers are 0-s and strings are empty)

Is it a bug in serialization/deserialization or zero is not allowed as
numeric enum value? The documentation (http://code.google.com/apis/
protocolbuffers/docs/proto.html#enum) shows example where 0 is used as
numeric value inside enum (UNIVERSAL):


enum Corpus {
UNIVERSAL = 0;
WEB = 1;
IMAGES = 2;
LOCAL = 3;
NEWS = 4;
PRODUCTS = 5;
VIDEO = 6;
}

Jason Hsueh

unread,
Sep 13, 2011, 2:48:13 PM9/13/11
to Michael Videlgauz, Protocol Buffers
What's your code snippet for serializing and parsing? The 0 enum value ends up getting encoded as the null character: assuming this is in C++, I'd guess that you're using some c-style string routines that are prematurely terminating the string at the null character.


--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.


Michael Videlgauz

unread,
Sep 20, 2011, 2:46:50 AM9/20/11
to Protocol Buffers
Thank you for pointing out the fact that encoded message may contain
null character!
I do not think it may be the problem because I am using only
std::string, SerializeToString() and never use string::c_str() to
manipulate with encoded buffers.
0MQ is used as network to transport those encoded strings.

But I'll still double check the entire flow again and come back to
thread soon...

On Sep 13, 9:48 pm, Jason Hsueh <jas...@google.com> wrote:
> What's your code snippet for serializing and parsing? The 0 enum value ends
> up getting encoded as the null character: assuming this is in C++, I'd guess
> that you're using some c-style string routines that are prematurely
> terminating the string at the null character.
>

Michael Videlgauz

unread,
Sep 20, 2011, 2:51:55 AM9/20/11
to Protocol Buffers
I forgot to mention in my previous post that there are int32 fields in
my messages and I do send messages with 0 value in them. If my problem
was null character then all those messages also were corrupted I
guess?...(and no, those 0-valued int32 fields are not last in the
message)

On Sep 13, 9:48 pm, Jason Hsueh <jas...@google.com> wrote:
> What's your code snippet for serializing and parsing? The 0 enum value ends
> up getting encoded as the null character: assuming this is in C++, I'd guess
> that you're using some c-style string routines that are prematurely
> terminating the string at the null character.
>

jsia

unread,
Oct 24, 2011, 9:53:07 AM10/24/11
to Protocol Buffers
Did you have any resolution on this? Im having the same issue, it
does not happen with int data types, this problem happens on boolean
data types and enum data types

Christopher Head

unread,
Oct 24, 2011, 7:01:12 PM10/24/11
to prot...@googlegroups.com
Not happening for me.

-- message.proto --
enum Value {
V1 = 0;
V2 = 1;
V3 = 2;
}

message Message {
required Value value = 1;
required int32 integer = 2;
required string text = 3;
}
-- test.cpp --
#include "message.pb.h"
#include <iostream>
#include <string>

int main() {
std::string buffer;
{
Message m;
m.set_value(V1);
m.set_integer(27);
m.set_text("Hello World");
m.SerializeToString(&buffer);
}

{
Message m;
m.ParseFromString(buffer);
std::cout << "value = " << m.value() << '\n';
std::cout << "integer = " << m.integer() << '\n';
std::cout << "text = " << m.text() << '\n';
}

return 0;
}
-- console output --
value = 0
integer = 27
text = Hello World
-- end of copy --

Can you narrow it down a bit more from this?

Chris

Reply all
Reply to author
Forward
0 new messages