From c8431e9e35a904673cf659fd238cb63b3c3896fc 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
 
---
 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 68d8b062e8..6e3966ff48 100644
--- a/src/basic/macro.h
+++ b/src/basic/macro.h
@@ -396,8 +396,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;                                       \
         }
 
-- 
2.33.0
 
