Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Illegal wire type...what doe it mean in general? #313

Closed
novellizator opened this issue Aug 20, 2015 · 18 comments
Closed

Illegal wire type...what doe it mean in general? #313

novellizator opened this issue Aug 20, 2015 · 18 comments
Labels

Comments

@novellizator
Copy link

In a complex code I receive a response . When I tried to decode it, I got:
[Error: Illegal wire type for field Message.Field .sync_pb.CommitResponse.entryresponse: 3 (2 expected)]

Could you please exaplain what it means? Th error itself doesn't clarify much.
One of the response fields is CommitResponse.entryresponse. What wrong could have happened?

And I get this response only if I supply the request with a specific (valid) value.

@dcodeIO
Copy link
Member

dcodeIO commented Aug 20, 2015

Basically, wire type 2 is a length delimited value (like a string, a couple of bytes, an inner message), while wire type 3 is the start of a legacy group, which is something entirely different. Wire types are used in the binary format to define which exact data type is following and an illegal wire type makes it impossible to decode the data correctly.

This is usually caused by corruption of the binary data, like when converting to strings and back.

See: https://github.com/dcodeIO/ProtoBuf.js/wiki/How-to-read-binary-data-in-the-browser-or-under-node.js%3F

@novellizator
Copy link
Author

The funny thing is that when I send protobuf message A, it works, and then when I send a slightly modified protobuf message (let's call it B) I get this error. I don't think google server would send me a corrupted response to one message and a correct one to a similar one. Do you think it would? Or are there chances there's a bug in the library?

@dcodeIO
Copy link
Member

dcodeIO commented Aug 24, 2015

Whether the data becomes corrupted or not also depends on the data in this scenario, so it is possible that one payload works while others don't. It's like converting raw bytes to UTF8: The first 128 characters are just ASCII, while other characters combine multiple bytes. Converting that back to ASCII most of the time gives you strange results, depending on the characters used.

If you are sure that the sender is giving you valid data, ensure to properly read the data as binary on your side and prevent any (implicit) conversion to a string (or explicitly use base64, hex, a real (internal and carefully converted) binary string). See the link I provided previously for more information.

@novellizator
Copy link
Author

Ok thanks. I think everything should work fine (using npm request):

request.post({
    url: url,
    qs: {
      'client': 'Google+Chrome',
      'client_id': config.clientId
    },
    headers: {
      'Content-Type': 'application/octet-stream',
      'Authorization': 'Bearer '+ accessToken
    },
    encoding: null, //  if you expect binary data
    responseType: 'buffer',
    body: body
  },<CALLBACK>);

So if the Protobuf.js is fine, I'll try out some other request module...

@seishun
Copy link
Contributor

seishun commented Aug 24, 2015

Maybe you could paste the raw response you receive so we can take a look at it.

@novellizator
Copy link
Author

Ok, this is the binary response:

 { type: 'Buffer', data: [ 10, 124, 11, 16, 1, 26, 102, 90, 58, 65, 68, 113, 116, 65, 90, 120, 89, 116, 112, 79, 100, 109, 122, 70, 108, 52, 70, 120, 47, 69, 67, 87, 69, 89, 50, 85, 50, 120, 121, 116, 82, 43, 72, 75, 98, 103, 83, 54, 85, 100, 49, 51, 66, 98, 57, 66, 72, 69, 80, 111, 120, 85, 119, 49, 51, 77, 114, 83, 87, 78, 103, 103, 109, 66, 97, 107, 106, 114, 70, 87, 70, 116, 107, 90, 118, 97, 67, 77, 57, 101, 89, 80, 115, 89, 118, 119, 111, 56, 68, 49, 73, 49, 104, 108, 122, 87, 119, 61, 61, 48, 183, 199, 167, 150, 249, 195, 199, 2, 80, 229, 206, 248, 161, 246, 41, 12, 32, 0, 50, 37, 122, 48, 48, 48, 48, 48, 49, 52, 52, 45, 52, 57, 56, 57, 45, 100, 55, 99, 97, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 53, 51, 48, 52, 55, 100, 51, 54, 58, 11, 8, 192, 112, 16, 224, 168, 1, 24, 90, 32, 11, 66, 12, 8, 0, 16, 0, 24, 0, 32, 0, 40, 0, 48, 40 ] }

The error message, when running the decode is following: [Error: Illegal wire type for field Message.Field .sync_pb.CommitResponse.entryresponse: 3 (2 expected)]

And the protocol buffer in use is this https://github.com/kitt-browser/chrome-sync/blob/master/src/js/sync.proto.js

The data above represents one ClientToServerResponse.

@cataquil
Copy link

It maybe character 10...I get an illegal character on that. I am serializing my object in another enviroment than node.js

@novellizator
Copy link
Author

This was a consequence of #355. Fixed.

@dcodeIO
Copy link
Member

dcodeIO commented May 30, 2018

Should decode(encodeDelimited(...)) be decodeDelimited(encodeDelimited(...))? The delimited functions write/read the length of the message as a varint to/from the start of the buffer, while the non-delimited ones do not.

Not sure why @Ishanisarkar's post is below the response now.

@Ishanisarkar
Copy link

I am writing a protocol which exchange messages between two clients directly using WebRTC. I have a message with two string fields. My proto file looks like :

syntax proto3
package xyz;
message Ah {
string Id = 1;
string Resource = 2;
}
Both Id and resource are defined as string type in the code, id is unique key and resource is a url. I am using pbjs to compile my protofiles and using the compiled files in my nodejs code. The message gets encoded and is transmitted but while decoding, it gives an error : Error:Invalid wire type 7 at offset 63. Both Id and url are string type in js as well as in .proto file. In another message I am just sending the id which get decoded properly. But in this specific message where the url is included too, it gives an error. Can you please tell me any possible reasons or areas where I can look for the solution.
This block is used for encoding:
const error: string = ProtoPeers.Message.verify(message);
if (error === null) {
console.log('message verified');
}
if (error !== null) {
logger().warn(Trying to send an invalid message: ${error});
return Promise.reject(new Error(error));
}
var message_1 = ProtoPeers.Message.create(message);
return Promise.try(
(): Uint8Array => ProtoPeers.Message.encodeDelimited(message_1).finish(),
)
.then((data: Uint8Array) => { this.syncChannel.send(data); });
This is the block where I decode the message.

try {
console.log('inside try');
const u8Array = new Uint8Array(data);
incoming = ProtoPeers.Message.decode(u8Array);
console.log('incoming message',incoming);
} catch (err) {
logger().warn(Invalid incoming message from ${this.id}: ${err});
return;
}

Do tell me if there is a need for more information.Thank You in advance.

@Ishanisarkar
Copy link

Ishanisarkar commented May 30, 2018

I am not very clear as to how these functions work, so I tried testing it with just encode and decode functions too rather than encodeDelimited() and decodeDelimited().. It still gives the same error.

@Ishanisarkar
Copy link

@dcodeIO I am sorry its my mistake, I thought I should create a new issue rather than commenting here.

@Ishanisarkar
Copy link

I checked by making the decode(encodeDeliited) as decodeDelimited(encodeDelimited). It still gives the same error. Plus it is strange because the other message which contains only the id works just fine.
Possibly the url string gets corrupted. Is it possible that while conversion from string to Uint8Array it gets corrupted? If so how can I check it?

@Ishanisarkar
Copy link

Ishanisarkar commented May 30, 2018

This is the encoded buffer. Does this buffer make sense : [98, 191, 1, 16, 36, 99, 48, 57, 101, 50, 54, 98, 51, 45, 98, 99, 49, 100, 45, 52, 51, 55, 101, 45, 98, 100, 102, 52, 45, 49, 102, 101, 97, 49, 48, 55, 100, 53, 57, 52, 101, 10, 150, 1, 104, 116, 116, 112, 58, 47, 47, 99, 100, 110, 97, 109, 100, 45, 104, 108, 115, 45, 103, 108, 111, 98, 101, 99, 97, 115, 116, 46, 97, 107, 97, 109, 97, 105, 122, 101, 100, 46, 110, 101, 116, 47, 108, 105, 118, 101, 47, 114, 97, 109, 100, 105, 115, 107, 47, 98, …]. According to the explanation here #55, the wire type is 0? whereas it should be 2. Any possible reason why ?

@dcodeIO
Copy link
Member

dcodeIO commented May 30, 2018

Usually happens when binary becomes accidentally converted to a string and back, see.

@Ishanisarkar
Copy link

I read the explanation you pointed too.
My
syncChannel.binaryType = 'arraybuffer';
I used JSON.Stringify(incoming) after I decoded the message i.e.
Private onMessage(data: ArrayBuffer): void { let incoming: ProtoPeers.Message; try { incoming = ProtoPeers.Message.decodeDelimited(new Uint8Array(data)); JSON.stringify(incoming); } catch (err) { logger().warn(Invalid incoming message from ${this.id}: ${err}); return; }
It still gives me the same error.
I am using a message where I just send the id and it works for me. I tried sending just the url and it works too with the same piece of code. Then why can't I send both of them together in a single message and decode them properly.
Also, since I am using CLI to generate static code and use type definitions I dont need to use JSON.stringify() ,according to this https://github.com/dcodeIO/protobuf.js#valid-message.
Any other possible areas I should be looking at for the solution? I really need to send both of theses parameters as string in a single message.

@Ishanisarkar
Copy link

Hey, I have still not being able to solve my problem. The above binary string which I posted is what I have when the string is encoded but at the decoding(client) side, I am receving something like this :
screen shot 2018-06-01 at 11 52 56
Why is Int16array present at the decoding side? As per my understanding, the data should only be in uint8array. Am I right? If it shouldn't be present at all , what could be the possible reason.
Thank You for your help.

@mibr020
Copy link

mibr020 commented Jan 31, 2023

Any updates on this error? I'm running into the same thing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants