From a576156e5bf9de147efe2c76af1ee29f5efd773e Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Mon, 9 Sep 2019 20:44:18 +0200 Subject: common: prevent stdarg errors when format is NULL --- src/common/error.c | 4 ++++ src/common/utils.c | 6 ++++++ tests/common/check_error.c | 7 ++++++- tests/common/check_utils.c | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/common/error.c b/src/common/error.c index 03695e7..caf47c3 100644 --- a/src/common/error.c +++ b/src/common/error.c @@ -26,6 +26,8 @@ bc_error_new(bc_error_type_t type, const char *msg) bc_error_t* bc_error_new_printf(bc_error_type_t type, const char *format, ...) { + if (format == NULL) + return bc_error_new(type, ""); va_list ap; va_start(ap, format); char *tmp = bc_strdup_vprintf(format, ap); @@ -40,6 +42,8 @@ bc_error_t* bc_error_parser(bc_error_type_t type, const char *src, size_t src_len, size_t current, const char *format, ...) { + if (format == NULL) + return bc_error_new(type, ""); va_list ap; va_start(ap, format); char *msg = bc_strdup_vprintf(format, ap); diff --git a/src/common/utils.c b/src/common/utils.c index 692d1ce..f686222 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -138,6 +138,8 @@ bc_strndup(const char *s, size_t n) char* bc_strdup_vprintf(const char *format, va_list ap) { + if (format == NULL) + return NULL; va_list ap2; va_copy(ap2, ap); int l = vsnprintf(NULL, 0, format, ap2); @@ -159,6 +161,8 @@ bc_strdup_vprintf(const char *format, va_list ap) char* bc_strdup_printf(const char *format, ...) { + if (format == NULL) + return NULL; va_list ap; va_start(ap, format); char *tmp = bc_strdup_vprintf(format, ap); @@ -464,6 +468,8 @@ bc_string_append_printf(bc_string_t *str, const char *format, ...) { if (str == NULL) return NULL; + if (format == NULL) + return str; va_list ap; va_start(ap, format); char *tmp = bc_strdup_vprintf(format, ap); diff --git a/tests/common/check_error.c b/tests/common/check_error.c index d9806f9..1b6f855 100644 --- a/tests/common/check_error.c +++ b/tests/common/check_error.c @@ -28,7 +28,12 @@ test_error_new(void **state) static void test_error_new_printf(void **state) { - bc_error_t *error = bc_error_new_printf(2, "bola %s", "guda"); + bc_error_t *error = bc_error_new_printf(2, NULL); + assert_non_null(error); + assert_int_equal(error->type, 2); + assert_string_equal(error->msg, ""); + bc_error_free(error); + error = bc_error_new_printf(2, "bola %s", "guda"); assert_non_null(error); assert_int_equal(error->type, 2); assert_string_equal(error->msg, "bola guda"); diff --git a/tests/common/check_utils.c b/tests/common/check_utils.c index cc14f4e..471cd77 100644 --- a/tests/common/check_utils.c +++ b/tests/common/check_utils.c @@ -117,6 +117,7 @@ test_strndup(void **state) static void test_strdup_printf(void **state) { + assert_null(bc_strdup_printf(NULL)); char *str = bc_strdup_printf("bola"); assert_string_equal(str, "bola"); free(str); @@ -554,6 +555,9 @@ static void test_string_append_printf(void **state) { bc_string_t *str = bc_string_new(); + str = bc_string_append_printf(str, NULL); + assert_string_equal(str->str, ""); + assert_non_null(str); str = bc_string_append_printf(str, "guda: %s %d", "bola", 1); assert_non_null(str); assert_string_equal(str->str, "guda: bola 1"); -- cgit v1.2.3-18-g5258