From f06a172a915457b5d3fec5dca9bc259389624f05 Mon Sep 17 00:00:00 2001 From: Richard Kettlewell Date: Sat, 25 Feb 2017 19:46:36 +0000 Subject: [PATCH 1/2] Document that protobuf_c_message_free_unpacked(NULL, ...) is allowed --- protobuf-c/protobuf-c.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 protobuf-c/protobuf-c.h diff --git a/protobuf-c/protobuf-c.h b/protobuf-c/protobuf-c.h old mode 100644 new mode 100755 index 55cce8d..3e02fa3 --- a/protobuf-c/protobuf-c.h +++ b/protobuf-c/protobuf-c.h @@ -956,7 +956,7 @@ protobuf_c_message_unpack( * protobuf_c_message_unpack(). * * \param message - * The message object to free. + * The message object to free. May be NULL. * \param allocator * `ProtobufCAllocator` to use for memory deallocation. May be NULL to * specify the default allocator. From c23065e4b1a2d3e21315d9556913c23648a23145 Mon Sep 17 00:00:00 2001 From: Richard Kettlewell Date: Sat, 25 Feb 2017 19:58:34 +0000 Subject: [PATCH 2/2] Allowing __free_unpacked(NULL, ...) This makes __free_unpacked() consistent with free(), and simplifies callers by allowing them to indiscriminately free message objects without regard to whether they have been allocated or not. --- protobuf-c/protobuf-c.h | 2 +- protoc-c/c_message.cc | 2 ++ t/generated-code2/test-generated-code2.c | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) mode change 100644 => 100755 protoc-c/c_message.cc mode change 100644 => 100755 t/generated-code2/test-generated-code2.c diff --git a/protobuf-c/protobuf-c.h b/protobuf-c/protobuf-c.h index 3e02fa3..54053c9 100755 --- a/protobuf-c/protobuf-c.h +++ b/protobuf-c/protobuf-c.h @@ -139,7 +139,7 @@ Foo__Bar__BazBah * ~~~ * * - `free_unpacked()`. Frees a message object obtained with the `unpack()` - * method. + * method. Freeing `NULL` is allowed (the same as with `free()`). * ~~~{.c} void foo__bar__baz_bah__free_unpacked diff --git a/protoc-c/c_message.cc b/protoc-c/c_message.cc old mode 100644 new mode 100755 index 53ec51a..671b708 --- a/protoc-c/c_message.cc +++ b/protoc-c/c_message.cc @@ -376,6 +376,8 @@ GenerateHelperFunctionDefinitions(io::Printer* printer, bool is_submessage) " ($classname$ *message,\n" " ProtobufCAllocator *allocator)\n" "{\n" + " if(!message)\n" + " return;\n" " assert(message->base.descriptor == &$lcclassname$__descriptor);\n" " protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);\n" "}\n" diff --git a/t/generated-code2/test-generated-code2.c b/t/generated-code2/test-generated-code2.c old mode 100644 new mode 100755 index c0cd36c..4739736 --- a/t/generated-code2/test-generated-code2.c +++ b/t/generated-code2/test-generated-code2.c @@ -2181,6 +2181,12 @@ test_message_check(void) assert(1 == protobuf_c_message_check(&m.base)); } +static void +test_message_free_null (void) +{ + foo__sub_mess__free_unpacked (NULL, NULL); +} + /* === simple testing framework === */ typedef void (*TestFunc) (void); @@ -2316,6 +2322,8 @@ static Test tests[] = { "test field flags", test_field_flags }, { "test message_check()", test_message_check }, + + { "test freeing NULL", test_message_free_null }, }; #define n_tests (sizeof(tests)/sizeof(Test))