ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
8d419f
From eb7eca006b5f290481720dc4d7fcdba88d8613cb Mon Sep 17 00:00:00 2001
8d419f
From: Yu Watanabe <watanabe.yu+github@gmail.com>
8d419f
Date: Sun, 17 Apr 2022 06:54:50 +0900
8d419f
Subject: [PATCH] macro: check over flow in reference counter
8d419f
8d419f
(cherry picked from commit c8431e9e35a904673cf659fd238cb63b3c3896fc)
8d419f
8d419f
Related: #2087652
8d419f
---
8d419f
 src/basic/macro.h | 8 ++++++--
8d419f
 1 file changed, 6 insertions(+), 2 deletions(-)
8d419f
8d419f
diff --git a/src/basic/macro.h b/src/basic/macro.h
8d419f
index aa04039e80..e7dc83ddc5 100644
8d419f
--- a/src/basic/macro.h
8d419f
+++ b/src/basic/macro.h
8d419f
@@ -387,8 +387,12 @@ static inline int __coverity_check_and_return__(int condition) {
8d419f
                 if (!p)                                         \
8d419f
                         return NULL;                            \
8d419f
                                                                 \
8d419f
-                assert(p->n_ref > 0);                           \
8d419f
-                p->n_ref++;                                     \
8d419f
+                /* For type check. */                           \
8d419f
+                unsigned *q = &p->n_ref;                        \
8d419f
+                assert(*q > 0);                                 \
8d419f
+                assert(*q < UINT_MAX);                          \
8d419f
+                                                                \
8d419f
+                (*q)++;                                         \
8d419f
                 return p;                                       \
8d419f
         }
8d419f