enum-names: Fail gracefully when passing a NULL value as enum names
authorMartin Willi <martin@revosec.ch>
Mon, 13 Apr 2015 16:22:49 +0000 (18:22 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 15 Apr 2015 12:38:43 +0000 (14:38 +0200)
src/libstrongswan/tests/suites/test_enum.c
src/libstrongswan/utils/enum.c

index 53ebd29..70bfdb2 100644 (file)
@@ -291,6 +291,15 @@ START_TEST(test_enum_printf_hook_split)
 }
 END_TEST
 
+START_TEST(test_enum_printf_hook_null)
+{
+       char buf[16];
+
+       snprintf(buf, sizeof(buf), "%N", NULL, 7);
+       ck_assert_str_eq("(7)", buf);
+}
+END_TEST
+
 START_TEST(test_enum_printf_hook_flags)
 {
        char buf[1024];
@@ -406,6 +415,7 @@ Suite *enum_suite_create()
        tc = tcase_create("enum_printf_hook");
        tcase_add_loop_test(tc, test_enum_printf_hook_cont, 0, countof(printf_tests_cont));
        tcase_add_loop_test(tc, test_enum_printf_hook_split, 0, countof(printf_tests_split));
+       tcase_add_test(tc, test_enum_printf_hook_null);
        tcase_add_loop_test(tc, test_enum_printf_hook_flags, 0, countof(printf_tests_flags));
        tcase_add_loop_test(tc, test_enum_printf_hook_flags_incomplete, 0, countof(printf_tests_flags_incomplete));
        tcase_add_loop_test(tc, test_enum_printf_hook_flags_null, 0, countof(printf_tests_flags_null));
index 089bebb..1cead77 100644 (file)
  */
 char *enum_to_name(enum_name_t *e, int val)
 {
+       if (!e)
+       {
+               return NULL;
+       }
        do
        {
                if (val >= e->first && val <= e->last)
@@ -140,7 +144,7 @@ int enum_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
        int val = *((int*)(args[1]));
        char *name, buf[512];
 
-       if (ed->next == ENUM_FLAG_MAGIC)
+       if (ed && ed->next == ENUM_FLAG_MAGIC)
        {
                name = enum_flags_to_string(ed, val, buf, sizeof(buf));
                if (name == NULL)