protobuf-c/t/issue440/issue440.c
Ilya Lipnitskiy 733ae77e23
protobuf-c.c: fix packed repeated bool parsing
From https://developers.google.com/protocol-buffers/docs/proto3#updating:
  int32, uint32, int64, uint64, and bool are all compatible – this means
  you can change a field from one of these types to another without
  breaking forwards- or backwards-compatibility. If a number is parsed
  from the wire which doesn't fit in the corresponding type, you will
  get the same effect as if you had cast the number to that type in C++
  (e.g. if a 64-bit number is read as an int32, it will be truncated to
  32 bits).

Until this fix, protobuf-c did not conform to the rule above when it
came to deserializing non-boolean packed repeated data into a
protobuf_c_boolean array. Fully scan the varint and use parse_boolean to
truncate the resulting value.

Fixes #440
2021-03-28 12:27:51 -07:00

31 lines
991 B
C

#include <stdlib.h>
#include <string.h>
#include "t/issue440/issue440.pb-c.h"
int main(void)
{
/* Output of $ echo "int: 1 int: -142342 int: 0 int: 423423222" | \
* protoc issue440.proto --encode=Int | xxd -i:
* 0x0a, 0x11, 0x01, 0xfa, 0xa7, 0xf7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
* 0x01, 0x00, 0xf6, 0xd9, 0xf3, 0xc9, 0x01
*
* Output of $ echo "int: 1 int: -142342 int: 0 int: 423423222" | \
* protoc issue440.proto --encode=Int | protoc issue440.proto \
* --decode=Boolean: boolean: true boolean: true boolean: false boolean: true
*/
uint8_t protoc[] = {0x0a, 0x11, 0x01, 0xfa, 0xa7, 0xf7, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf6, 0xd9, 0xf3, 0xc9,
0x01};
Boolean *msg = boolean__unpack (NULL, sizeof protoc, protoc);
assert(msg);
assert(msg->n_boolean == 4);
assert(msg->boolean[0] == 1);
assert(msg->boolean[1] == 1);
assert(msg->boolean[2] == 0);
assert(msg->boolean[3] == 1);
boolean__free_unpacked (msg, NULL);
return EXIT_SUCCESS;
}