richardphibel / rpms / systemd

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