From 2f9b764965e29a0d0e26d95352b8f0a7bb705aed Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 15 Mar 2024 11:43:09 -0400 Subject: [PATCH] argpar.{c,h}: fix clang-tidy issues Fix these issues: /home/smarchi/src/argpar/argpar/argpar.h:344:9: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses] 344 | _item = NULL; \ | ^ | ( ) argpar.c:525:34: warning: 'iter->tmp_buf.data' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage] 525 | iter->tmp_buf.data = ARGPAR_REALLOC(iter->tmp_buf.data, char, iter->tmp_buf.size); | ~~~~~~~~~~~~~~~~~~ ^ argpar.c:15:56: note: expanded from macro 'ARGPAR_REALLOC' 15 | #define ARGPAR_REALLOC(_ptr, _type, _nmemb) ((_type *) realloc(_ptr, (_nmemb) * sizeof(_type))) | ^ ~~~~ Change-Id: I2e42059f8c4c744e10bdbc7c78b2628a53cd60d7 Signed-off-by: Simon Marchi --- argpar/argpar.c | 10 +++++++--- argpar/argpar.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/argpar/argpar.c b/argpar/argpar.c index 67b7769..d4f000a 100644 --- a/argpar/argpar.c +++ b/argpar/argpar.c @@ -532,12 +532,16 @@ parse_long_opt(const char * const long_opt_arg, const char * const next_orig_arg /* Isolate the option name */ while (long_opt_name_size > iter->tmp_buf.size - 1) { - iter->tmp_buf.size *= 2; - iter->tmp_buf.data = ARGPAR_REALLOC(iter->tmp_buf.data, char, iter->tmp_buf.size); - if (!iter->tmp_buf.data) { + const size_t new_size = iter->tmp_buf.size * 2; + char * const new_data = ARGPAR_REALLOC(iter->tmp_buf.data, char, new_size); + + if (!new_data) { ret = PARSE_ORIG_ARG_OPT_RET_ERROR_MEMORY; goto error; } + + iter->tmp_buf.size = new_size; + iter->tmp_buf.data = new_data; } memcpy(iter->tmp_buf.data, long_opt_arg, long_opt_name_size); diff --git a/argpar/argpar.h b/argpar/argpar.h index 4f2083a..52bdbdd 100644 --- a/argpar/argpar.h +++ b/argpar/argpar.h @@ -307,7 +307,7 @@ void argpar_item_destroy(const argpar_item_t *item) ARGPAR_NOEXCEPT; #define ARGPAR_ITEM_DESTROY_AND_RESET(_item) \ { \ argpar_item_destroy(_item); \ - _item = NULL; \ + (_item) = NULL; \ } /// @} -- 2.34.1