Merge pull request #467 from protobuf-c/issue-440

protobuf-c.c: fix packed repeated bool parsing
This commit is contained in:
Robert Edmonds 2021-04-06 20:18:34 -04:00 committed by GitHub
commit 7c6b54b162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 4 deletions

View File

@ -301,6 +301,22 @@ BUILT_SOURCES += \
t/issue389/issue389.pb-c.c t/issue389/issue389.pb-c.h
EXTRA_DIST += \
t/issue389/issue389.proto
check_PROGRAMS += \
t/issue440/issue440
TESTS += \
t/issue440/issue440
t_issue440_issue440_SOURCES = \
t/issue440/issue440.c \
t/issue440/issue440.pb-c.c
t_issue440_issue440_LDADD = \
protobuf-c/libprotobuf-c.la
t/issue440/issue440.pb-c.c t/issue440/issue440.pb-c.h: $(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) $(top_srcdir)/t/issue440/issue440.proto
$(AM_V_GEN)@PROTOC@ --plugin=protoc-gen-c=$(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) -I$(top_srcdir) --c_out=$(top_builddir) $(top_srcdir)/t/issue440/issue440.proto
BUILT_SOURCES += \
t/issue440/issue440.pb-c.c t/issue440/issue440.pb-c.h
EXTRA_DIST += \
t/issue440/issue440.proto
endif # BUILD_PROTO3
EXTRA_DIST += \
t/issue330/issue330.proto

View File

@ -2750,7 +2750,9 @@ parse_packed_repeated_member(ScannedMember *scanned_member,
const uint8_t *at = scanned_member->data + scanned_member->length_prefix_len;
size_t rem = scanned_member->len - scanned_member->length_prefix_len;
size_t count = 0;
#if defined(WORDS_BIGENDIAN)
unsigned i;
#endif
switch (field->type) {
case PROTOBUF_C_TYPE_SFIXED32:
@ -2843,13 +2845,15 @@ parse_packed_repeated_member(ScannedMember *scanned_member,
}
break;
case PROTOBUF_C_TYPE_BOOL:
count = rem;
for (i = 0; i < count; i++) {
if (at[i] > 1) {
while (rem > 0) {
unsigned s = scan_varint(rem, at);
if (s == 0) {
PROTOBUF_C_UNPACK_ERROR("bad packed-repeated boolean value");
return FALSE;
}
((protobuf_c_boolean *) array)[i] = at[i];
((protobuf_c_boolean *) array)[count++] = parse_boolean(s, at);
at += s;
rem -= s;
}
break;
default:

1
t/issue440/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
issue440

30
t/issue440/issue440.c Normal file
View File

@ -0,0 +1,30 @@
#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;
}

View File

@ -0,0 +1,9 @@
syntax = "proto3";
message Int {
repeated int32 int = 1;
}
message Boolean {
repeated bool boolean = 1;
}