protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
64.64k stars 15.37k forks source link

string disappeared after parsing #12991

Closed MMitsuha closed 7 months ago

MMitsuha commented 1 year ago

What version of protobuf and what language are you using? Version: v3.21.12 etc. (NOTE: please try updating to the latest version of protoc/runtime possible beforehand to attempt to resolve your problem) Language: C++

What operating system (Linux, Windows, ...) and version? Windows 11 22H2

What runtime / compiler are you using (e.g., python version or gcc version) Microsoft Visual Studio 2022

What did you do? Here are my codes:

main.cpp:

auto* header = new common_t();
header->set_version(1001);
header->set_timestamp(0);

auto* handshake = new handshake_t();
handshake->set_client_id(666);
handshake->set_message("qwq");

packet_t packet;
packet.set_allocated_header(header);
packet.set_allocated_handshake(handshake);

size_t data_size = packet.ByteSizeLong();
auto* raw_buffer = new char[data_size];
packet.SerializeToArray(raw_buffer, data_size);

packet_t qwq;
qwq.ParseFromArray(raw_buffer, data_size);
MessageBoxA(nullptr, qwq.handshake().message().c_str(), "qwq", MB_OK);

message.proto:

syntax = "proto3";

message common_t {
    uint32 version = 1;
    uint64 timestamp = 2;
}

message handshake_t {
    uint64 client_id = 1;
    string message = 2;
}

message packet_t {
    common_t header = 1;

    oneof body {
        handshake_t handshake = 2;
    }
}

What did you expect to see MessageBoxA outputs string: "qwq"

What did you see instead? No output from MessageBoxA

Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).

Anything else we should know about your project / environment

I am using vcpkg and the version of protobuf seemed a little bit old

deannagarcia commented 1 year ago

Can you try upgrading to the latest release (23.2) and see if that fixes anything?

mkruskal-google commented 1 year ago

3.21 isn't so old I'd expect any issues like this. There are three things I'd be curious to see: 1) the return value of SerializeToArray. If this returns false it would explain the symptom, and means that the root cause it that message failed to serialize for some reason 2) the return value of ParseFromArray for similar reasons 3) qwq.DebugString(), to see if there's anything else in there or if it's just empty

MMitsuha commented 1 year ago

Thanks for your reply, the problem is parsing, I see protobuf did generated the correct buffer (I used a online parser and parsed the buffer which protobuf gave me and its output is correct), but when it comes to my protobuf, everything expect the string is okay, the string field (in std::string)'s pointer pointed to a wrong position (which is behind the string I wanted)

I would send these debug information ASAP.

MMitsuha commented 1 year ago

It seems to be qt's fault Before calling ParseFromString (Stack trace is at right buttom): image After calling ParseFromString: image

github-actions[bot] commented 8 months ago

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago.

github-actions[bot] commented 7 months ago

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.