]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: replace size_multiply_overflow() with MUL_ASSIGN_SAFE where applicable
authorMike Yuan <me@yhndnzj.com>
Fri, 20 Sep 2024 19:39:15 +0000 (21:39 +0200)
committerMike Yuan <me@yhndnzj.com>
Fri, 20 Sep 2024 20:44:34 +0000 (22:44 +0200)
src/basic/alloc-util.c
src/basic/alloc-util.h
src/basic/string-util.h

index fc98610a0f8eaeaf57d3b5afdcaa6edda14c4896..96cf27dc37d2b5cac20a7e3b663a3d0d822f7c03 100644 (file)
@@ -43,7 +43,7 @@ void* greedy_realloc(
                 size_t need,
                 size_t size) {
 
-        size_t a, newalloc;
+        size_t newalloc;
         void *q;
 
         assert(p);
@@ -60,14 +60,13 @@ void* greedy_realloc(
                 return NULL;
         newalloc = need * 2;
 
-        if (size_multiply_overflow(newalloc, size))
+        if (!MUL_ASSIGN_SAFE(&newalloc, size))
                 return NULL;
-        a = newalloc * size;
 
-        if (a < 64) /* Allocate at least 64 bytes */
-                a = 64;
+        if (newalloc < 64) /* Allocate at least 64 bytes */
+                newalloc = 64;
 
-        q = realloc(*p, a);
+        q = realloc(*p, newalloc);
         if (!q)
                 return NULL;
 
index c215c33f4bfaa78f19826cb1d970cfa0ffeb126e..462092703aa5f0d4e89a93feb68195a1d9569ea1 100644 (file)
@@ -26,23 +26,23 @@ typedef void* (*mfree_func_t)(void *p);
 
 #define alloca_safe(n)                                                  \
         ({                                                              \
-                size_t _nn_ = n;                                        \
+                size_t _nn_ = (n);                                      \
                 assert(_nn_ <= ALLOCA_MAX);                             \
                 alloca(_nn_ == 0 ? 1 : _nn_);                           \
         })                                                              \
 
 #define newa(t, n)                                                      \
         ({                                                              \
-                size_t _n_ = n;                                         \
-                assert(!size_multiply_overflow(sizeof(t), _n_));        \
-                (t*) alloca_safe(sizeof(t)*_n_);                        \
+                size_t _n_ = (n);                                       \
+                assert_se(MUL_ASSIGN_SAFE(&_n_, sizeof(t)));            \
+                (t*) alloca_safe(_n_);                                  \
         })
 
 #define newa0(t, n)                                                     \
         ({                                                              \
-                size_t _n_ = n;                                         \
-                assert(!size_multiply_overflow(sizeof(t), _n_));        \
-                (t*) alloca0((sizeof(t)*_n_));                          \
+                size_t _n_ = (n);                                       \
+                assert_se(MUL_ASSIGN_SAFE(&_n_, sizeof(t)));            \
+                (t*) alloca0(_n_);                                      \
         })
 
 #define newdup(t, p, n) ((t*) memdup_multiply(p, n, sizeof(t)))
index 0f6e0b72bfbfb91514ca04d9c504a662b8c348b0..cc6aa183c0c6e6dde2e247038c2d14d82ac6bde8 100644 (file)
@@ -201,18 +201,17 @@ int strextendf_with_separator(char **x, const char *separator, const char *forma
 
 char* strrep(const char *s, unsigned n);
 
-#define strrepa(s, n)                                           \
-        ({                                                      \
-                const char *_sss_ = (s);                        \
-                size_t _nnn_ = (n), _len_ = strlen(_sss_);      \
-                assert(!size_multiply_overflow(_len_, _nnn_));  \
-                _len_ *= _nnn_;                                 \
-                char *_d_, *_p_;                                \
-                _p_ = _d_ = newa(char, _len_ + 1);              \
-                for (size_t _i_ = 0; _i_ < _nnn_; _i_++)        \
-                        _p_ = stpcpy(_p_, _sss_);               \
-                *_p_ = 0;                                       \
-                _d_;                                            \
+#define strrepa(s, n)                                                   \
+        ({                                                              \
+                const char *_sss_ = (s);                                \
+                size_t _nnn_ = (n), _len_ = strlen(_sss_);              \
+                assert_se(MUL_ASSIGN_SAFE(&_len_, _nnn_));              \
+                char *_d_, *_p_;                                        \
+                _p_ = _d_ = newa(char, _len_ + 1);                      \
+                for (size_t _i_ = 0; _i_ < _nnn_; _i_++)                \
+                        _p_ = stpcpy(_p_, _sss_);                       \
+                *_p_ = 0;                                               \
+                _d_;                                                    \
         })
 
 int split_pair(const char *s, const char *sep, char **l, char **r);