Merge pull request #255 from ewxrjk/free-null

Document and extend the effect of passing NULL to ..._free_unpacked functions
This commit is contained in:
Robert Edmonds 2017-02-25 12:49:38 -08:00 committed by GitHub
commit 424c498728
3 changed files with 12 additions and 2 deletions

4
protobuf-c/protobuf-c.h Normal file → Executable file
View File

@ -139,7 +139,7 @@ Foo__Bar__BazBah *
~~~ ~~~
* *
* - `free_unpacked()`. Frees a message object obtained with the `unpack()` * - `free_unpacked()`. Frees a message object obtained with the `unpack()`
* method. * method. Freeing `NULL` is allowed (the same as with `free()`).
* *
~~~{.c} ~~~{.c}
void foo__bar__baz_bah__free_unpacked void foo__bar__baz_bah__free_unpacked
@ -956,7 +956,7 @@ protobuf_c_message_unpack(
* protobuf_c_message_unpack(). * protobuf_c_message_unpack().
* *
* \param message * \param message
* The message object to free. * The message object to free. May be NULL.
* \param allocator * \param allocator
* `ProtobufCAllocator` to use for memory deallocation. May be NULL to * `ProtobufCAllocator` to use for memory deallocation. May be NULL to
* specify the default allocator. * specify the default allocator.

2
protoc-c/c_message.cc Normal file → Executable file
View File

@ -376,6 +376,8 @@ GenerateHelperFunctionDefinitions(io::Printer* printer, bool is_submessage)
" ($classname$ *message,\n" " ($classname$ *message,\n"
" ProtobufCAllocator *allocator)\n" " ProtobufCAllocator *allocator)\n"
"{\n" "{\n"
" if(!message)\n"
" return;\n"
" assert(message->base.descriptor == &$lcclassname$__descriptor);\n" " assert(message->base.descriptor == &$lcclassname$__descriptor);\n"
" protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);\n" " protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);\n"
"}\n" "}\n"

8
t/generated-code2/test-generated-code2.c Normal file → Executable file
View File

@ -2181,6 +2181,12 @@ test_message_check(void)
assert(1 == protobuf_c_message_check(&m.base)); 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 === */ /* === simple testing framework === */
typedef void (*TestFunc) (void); typedef void (*TestFunc) (void);
@ -2316,6 +2322,8 @@ static Test tests[] =
{ "test field flags", test_field_flags }, { "test field flags", test_field_flags },
{ "test message_check()", test_message_check }, { "test message_check()", test_message_check },
{ "test freeing NULL", test_message_free_null },
}; };
#define n_tests (sizeof(tests)/sizeof(Test)) #define n_tests (sizeof(tests)/sizeof(Test))