diff --git a/protobuf-c/protobuf-c.h b/protobuf-c/protobuf-c.h old mode 100644 new mode 100755 index 55cce8d..54053c9 --- 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 @@ -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. 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))