argpar.{c,h}: fix clang-tidy issues
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 15 Mar 2024 15:43:09 +0000 (11:43 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Mon, 18 Mar 2024 14:59:40 +0000 (10:59 -0400)
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 <simon.marchi@efficios.com>
argpar/argpar.c
argpar/argpar.h

index 67b77691cbe19784667e1c17013e686f74dc38a6..d4f000a344588ef7883139d4b3296b86e17d2b13 100644 (file)
@@ -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);
index 4f2083a8c6d59ed0a3219ebb863e5178d14ed61c..52bdbdd93ac00dab91c8661736e738329d1eb79d 100644 (file)
@@ -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;                                                                            \
     }
 
 /// @}
This page took 0.024245 seconds and 4 git commands to generate.