[njs] Catching invalid njs_mp_free() calls.

Dmitry Volyntsev xeioex at nginx.com
Sat Jun 11 07:17:00 UTC 2022


details:   https://hg.nginx.org/njs/rev/77758def397a
branches:  
changeset: 1887:77758def397a
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Sat Jun 11 00:15:30 2022 -0700
description:
Catching invalid njs_mp_free() calls.

diffstat:

 auto/options     |   4 ++++
 src/njs_assert.h |  10 ++++++++++
 src/njs_mp.c     |   6 ++----
 3 files changed, 16 insertions(+), 4 deletions(-)

diffs (54 lines):

diff -r e38e175c0063 -r 77758def397a auto/options
--- a/auto/options	Sat Jun 11 00:15:29 2022 -0700
+++ b/auto/options	Sat Jun 11 00:15:30 2022 -0700
@@ -62,3 +62,7 @@ do
     NJS_CONFIGURE_OPTIONS="$NJS_CONFIGURE_OPTIONS $njs_opt"
 
 done
+
+if [ "$NJS_DEBUG_MEMORY" = "YES" ]; then
+    NJS_DEBUG=YES
+fi
diff -r e38e175c0063 -r 77758def397a src/njs_assert.h
--- a/src/njs_assert.h	Sat Jun 11 00:15:29 2022 -0700
+++ b/src/njs_assert.h	Sat Jun 11 00:15:30 2022 -0700
@@ -18,9 +18,19 @@
         }                                                                     \
     } while (0)
 
+#define njs_assert_msg(condition, fmt, ...)                                   \
+    do {                                                                      \
+        if (!(condition)) {                                                   \
+            njs_stderror(fmt, ##__VA_ARGS__);                                 \
+            njs_stderror(" at %s:%d\n", __FILE__, __LINE__);                  \
+            abort();                                                          \
+        }                                                                     \
+    } while (0)
+
 #else
 
 #define njs_assert(condition) (void) (condition)
+#define njs_assert_msg(condition, fmt, ...) (void) (condition)
 
 #endif
 
diff -r e38e175c0063 -r 77758def397a src/njs_mp.c
--- a/src/njs_mp.c	Sat Jun 11 00:15:29 2022 -0700
+++ b/src/njs_mp.c	Sat Jun 11 00:15:30 2022 -0700
@@ -678,14 +678,12 @@ njs_mp_free(njs_mp_t *mp, void *p)
             return;
 
         } else {
-            err = "freed pointer points to middle of block: %p\n";
+            njs_assert_msg(0, "freed pointer points to middle of blk: %p\n", p);
         }
 
     } else {
-        err = "freed pointer is out of mp: %p\n";
+        njs_assert_msg(0, "freed pointer is out of mp: %p\n", p);
     }
-
-    njs_debug_alloc(err, p);
 }
 
 



More information about the nginx-devel mailing list