Blob Blame History Raw
From eb7eca006b5f290481720dc4d7fcdba88d8613cb Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Sun, 17 Apr 2022 06:54:50 +0900
Subject: [PATCH] macro: check over flow in reference counter

(cherry picked from commit c8431e9e35a904673cf659fd238cb63b3c3896fc)

Related: #2087652
---
 src/basic/macro.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/basic/macro.h b/src/basic/macro.h
index aa04039e80..e7dc83ddc5 100644
--- a/src/basic/macro.h
+++ b/src/basic/macro.h
@@ -387,8 +387,12 @@ static inline int __coverity_check_and_return__(int condition) {
                 if (!p)                                         \
                         return NULL;                            \
                                                                 \
-                assert(p->n_ref > 0);                           \
-                p->n_ref++;                                     \
+                /* For type check. */                           \
+                unsigned *q = &p->n_ref;                        \
+                assert(*q > 0);                                 \
+                assert(*q < UINT_MAX);                          \
+                                                                \
+                (*q)++;                                         \
                 return p;                                       \
         }