Prevent underflow in `memcached_*_response_decrement` macros 124/head
authorAdam Saponara <as@php.net>
Mon, 7 Feb 2022 21:40:44 +0000 (16:40 -0500)
committerAdam Saponara <as@php.net>
Mon, 7 Feb 2022 21:40:44 +0000 (16:40 -0500)
src/libmemcached/common.h

index 954353ac5da55cf63fd4909f853b94bfb77eb4f8..ed12d617fad5d74424b9caceaa21b6e19d5e1d8a 100644 (file)
@@ -140,11 +140,21 @@ static inline void memcached_server_response_increment(memcached_instance_st *in
 }
 #endif
 
-#define memcached_server_response_decrement(A) (A)->cursor_active_--
-#define memcached_server_response_reset(A)     (A)->cursor_active_ = 0
+#define memcached_server_response_decrement(A) do {     \
+    WATCHPOINT_ASSERT((A)->cursor_active_ > 0);         \
+    if ((A)->cursor_active_ > 0) {                      \
+        (A)->cursor_active_--;                          \
+    }                                                   \
+} while (0)
+#define memcached_server_response_reset(A)       (A)->cursor_active_ = 0
 
 #define memcached_instance_response_increment(A) (A)->cursor_active_++
-#define memcached_instance_response_decrement(A) (A)->cursor_active_--
+#define memcached_instance_response_decrement(A) do {   \
+    WATCHPOINT_ASSERT((A)->cursor_active_ > 0);         \
+    if ((A)->cursor_active_ > 0) {                      \
+        (A)->cursor_active_--;                          \
+    }                                                   \
+} while (0)
 #define memcached_instance_response_reset(A)     (A)->cursor_active_ = 0
 
 #ifdef __cplusplus