Merge pull request #377 from protobuf-c/ilya/issue330

Fix proto3 repeated scalar field default packing behavior (#330)
This commit is contained in:
Robert Edmonds 2019-05-18 17:16:37 -04:00 committed by GitHub
commit c404ce3eb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 1 deletions

View File

@ -250,6 +250,23 @@ BUILT_SOURCES += \
EXTRA_DIST += \
t/issue251/issue251.proto
# Issue #330
check_PROGRAMS += \
t/issue330/issue330
TESTS += \
t/issue330/issue330
t_issue330_issue330_SOURCES = \
t/issue330/issue330.c \
t/issue330/issue330.pb-c.c
t_issue330_issue330_LDADD = \
protobuf-c/libprotobuf-c.la
t/issue330/issue330.pb-c.c t/issue330/issue330.pb-c.h: $(top_builddir)/protoc-c/protoc-gen-c$(EXEEXT) $(top_srcdir)/t/issue330/issue330.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/issue330/issue330.proto
BUILT_SOURCES += \
t/issue330/issue330.pb-c.c t/issue330/issue330.pb-c.h
EXTRA_DIST += \
t/issue330/issue330.proto
# Issue #375
check_PROGRAMS += \
t/issue375/issue375

View File

@ -140,8 +140,14 @@ void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer,
if (descriptor_->label() == FieldDescriptor::LABEL_REPEATED
&& is_packable_type (descriptor_->type())
&& descriptor_->options().packed())
&& descriptor_->options().packed()) {
variables["flags"] += " | PROTOBUF_C_FIELD_FLAG_PACKED";
} else if (descriptor_->label() == FieldDescriptor::LABEL_REPEATED
&& is_packable_type (descriptor_->type())
&& FieldSyntax(descriptor_) == 3
&& !descriptor_->options().has_packed()) {
variables["flags"] += " | PROTOBUF_C_FIELD_FLAG_PACKED";
}
if (descriptor_->options().deprecated())
variables["flags"] += " | PROTOBUF_C_FIELD_FLAG_DEPRECATED";

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

@ -0,0 +1 @@
issue330

25
t/issue330/issue330.c Normal file
View File

@ -0,0 +1,25 @@
#include <stdlib.h>
#include <string.h>
#include "t/issue330/issue330.pb-c.h"
int main(void)
{
/* Output of $ echo acl_id: 2 acl_id: 3 | protoc issue330.proto \
* --encode=pbr_route | xxd -i: 0x52, 0x02, 0x02, 0x03
*/
uint8_t protoc[] = {0x52, 0x02, 0x02, 0x03};
PbrRoute msg = PBR_ROUTE__INIT;
int ids[] = {2, 3};
uint8_t buf[16] = {0};
size_t sz = 0;
msg.n_acl_id = 2;
msg.acl_id = ids;
sz = pbr_route__pack(&msg, buf);
assert (sz == sizeof protoc);
assert (memcmp (protoc, buf, sz) == 0);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,5 @@
syntax = "proto3";
message pbr_route {
repeated int32 acl_id = 10;
}