Skip to content

Invalid wire type and index out of range errors #602

@smashnick

Description

@smashnick

protobuf.js version: 6.3.1

I am a novice, so bear with me on the below explanation. Please do not mind the excessive buffer declarations.

I am reading a TCP stream with cap from npm to obtain a TCP payload by listening to a particular port. This is the structure of the packet:

struct PegasusPacket {
uint32_t type;
uint32_t size;
uint8_t *message;
};
The first 4 bytes are the message type
The next 4 bytes are the message size
The remaining size bytes are the protocol buffer encoded message

The packets themselves don't seem to have any issue because I can always extract the type and size faithfully, but when I try to decode the message from my .proto file, things start to go wrong. I have included some buffer examples and stack trace results below. The buffers are the expected message after being reversed.

I also had an issue with index out of range, but I was unable to generate that error for this issue. I think it resolved when I reverse()'d the message buffer..

function getPayloadData(data){
	protobuf.load("../proto/PegasusGame.proto",function(err, root){
		var type,size,message,pl,pl2,pl3,pl4;
		try{
			//slice does some bad things to original data
			pl1 = Buffer.from(data);
			pl2 = Buffer.from(data);
			pl3 = Buffer.from(data);
			pl4 = Buffer.from(data);
			
			//extract 4-byt4 type, 4-byte size, and message
			type = pl1.slice(0,4).reverse().readUIntBE(0,4);
			size = pl2.slice(4,8).reverse().readUIntBE(0,4);
			message = pl3.slice(8,pl4.length).reverse();
			console.log(message);
		} catch(e){
			// console.log(e);
		}
		if(err) throw err;
		if(type !== 19) return;
		
		var PowerHistory = root.lookup("PegasusGame.PowerHistory");
		var PHData = PowerHistory.decode(message);
		console.log(PHData.list);
	});
}
<Buffer 10 01 08 07 22 09 0a 00 20 03 18 02 10 05 08 08 32 0a 0a 0a 18 13 10 01 08 06 22 08 0a 00 3a 02 0a 0a 18 01 c6 10 01 08 07 22 09 0a 00 18 03 d3 10 03 ... >

C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\reader.js:490
            throw Error("invalid wire type: " + wireType);
            ^

Error: invalid wire type: 6
    at Error (native)
    at BufferReader.ReaderPrototype.skipType (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src
\reader.js:490:19)
    at Type._PegasusGame_PowerHistory$decode [as decode] (eval at eof (C:\Users\Nick\Documents\Coding Memes\MessageDecode\no
de_modules\@protobufjs\codegen\index.js:102:25), <anonymous>:14:7)
    at Type.decode_setup [as decode] (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\type.js
:407:25)
    at C:\Users\Nick\Documents\Coding Memes\MessageDecode\test\test_cap.js:102:29
    at finish (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\root.js:97:9)
    at process (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\root.js:126:13)
    at C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\root.js:181:17
    at fetchReadFileCallback (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\@protobufjs\fetch\index.js:32:
19)
    at tryToString (fs.js:455:3)

<Buffer 10 03 08 07 22 09 0a 01 18 02 8a 10 03 08 07 22 09 0a 04 18 31 10 33 08 06 22 08 0a 00 3a 02 0a 0c 18 2c 10 40 08 06 22 08 0a 33 18 12 10 40 08 06 22 ... >

Error: invalid wire type: 4
    at Error (native)
    at BufferReader.ReaderPrototype.skipType (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\reader.js:490:19)
    at Type._PegasusGame_PowerHistoryData$decode [as decode] (eval at eof (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\@protobufjs\codegen\index.js:102:25), <anonymous>:34:7)
    at Type._PegasusGame_PowerHistory$decode [as decode] (eval at eof (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\@protobufjs\codegen\index.js:102:25), <anonymous>:11:29)
    at Type.decode_setup [as decode] (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\type.js:407:25)
    at C:\Users\Nick\Documents\Coding Memes\MessageDecode\test\test_cap.js:102:30
    at finish (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\root.js:97:9)
    at process (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\root.js:126:13)
    at C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\root.js:181:17
    at fetchReadFileCallback (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\@protobufjs\fetch\index.js:32:19)

<Buffer 10 01 08 07 22 09 0a 00 20 03 18 02 10 05 08 08 32 0a 0a 0a 18 13 10 01 08 06 22 08 0a 00 3a 02 0a 00 18 24 10 40 08 06 22 08 0a 00 18 26 10 49 08 06 ... >

Error: invalid wire type: 7
    at Error (native)
    at BufferReader.ReaderPrototype.skipType (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\reader.js:490:19)
    at Type._PegasusGame_PowerHistory$decode [as decode] (eval at eof (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\@protobufjs\codegen\index.js:102:25), <anonymous>:14:7)
    at Type.decode_setup [as decode] (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\type.js:407:25)
    at C:\Users\Nick\Documents\Coding Memes\MessageDecode\test\test_cap.js:102:30
    at finish (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\root.js:97:9)
    at process (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\root.js:126:13)
    at C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\protobufjs\src\root.js:181:17
    at fetchReadFileCallback (C:\Users\Nick\Documents\Coding Memes\MessageDecode\node_modules\@protobufjs\fetch\index.js:32:19)
    at tryToString (fs.js:455:3)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions