[PATCH] Core: enclosed parameters of macros in parentheses.

balus balus at foxmail.com
Sat Jul 18 12:09:30 UTC 2020


Hi,


I have been writing a nginx module recently, in which I use
ngx_buf_t to store data received from network, such as:


static u_char     buffer[4096];
static ngx_buf_t  b;


b.pos = b.last = b.start = buffer;
b.end = b.start + sizeof(buffer);


...


size = ngx_buf_size(&b);


then I find it not work since the parameter in ngx_buf_size is not
closed by parenthese. And I think this is a common situation so I
made this patch:


# HG changeset patch
# User balus <balus at foxmail.com>
# Date 1595073121 -28800
#      Sat Jul 18 19:52:01 2020 +0800
# Node ID 92d9878c0c7549345f0a144cd81a6b6d45f21fc6
# Parent  32a343635b50662979975e1204417bb1fc7e1b1f
Core: enclosed parameters of macros in parentheses.


diff -r 32a343635b50 -r 92d9878c0c75 src/core/ngx_buf.h
--- a/src/core/ngx_buf.h	Thu Jul 09 16:21:37 2020 +0300
+++ b/src/core/ngx_buf.h	Sat Jul 18 19:52:01 2020 +0800
@@ -125,20 +125,22 @@
 #define NGX_CHAIN_ERROR     (ngx_chain_t *) NGX_ERROR
 
 
-#define ngx_buf_in_memory(b)        (b->temporary || b->memory || b->mmap)
-#define ngx_buf_in_memory_only(b)   (ngx_buf_in_memory(b) && !b->in_file)
+#define ngx_buf_in_memory(b)                                                 \
+    ((b)->temporary || (b)->memory || (b)->mmap)
+#define ngx_buf_in_memory_only(b)   (ngx_buf_in_memory(b) && !(b)->in_file)
 
 #define ngx_buf_special(b)                                                   \
-    ((b->flush || b->last_buf || b->sync)                                    \
-     && !ngx_buf_in_memory(b) && !b->in_file)
+    (((b)->flush || (b)->last_buf || (b)->sync)                              \
+     && !ngx_buf_in_memory(b) && !(b)->in_file)
 
 #define ngx_buf_sync_only(b)                                                 \
-    (b->sync                                                                 \
-     && !ngx_buf_in_memory(b) && !b->in_file && !b->flush && !b->last_buf)
+    ((b)->sync                                                               \
+     && !ngx_buf_in_memory(b)                                                \
+     && !(b)->in_file && !(b)->flush && !(b)->last_buf)
 
 #define ngx_buf_size(b)                                                      \
-    (ngx_buf_in_memory(b) ? (off_t) (b->last - b->pos):                      \
-                            (b->file_last - b->file_pos))
+    (ngx_buf_in_memory(b) ? (off_t) ((b)->last - (b)->pos)                   \
+                          : ((b)->file_last - (b)->file_pos))
 
 ngx_buf_t *ngx_create_temp_buf(ngx_pool_t *pool, size_t size);
 ngx_chain_t *ngx_create_chain_of_bufs(ngx_pool_t *pool, ngx_bufs_t *bufs);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20200718/cb2d6df5/attachment.htm>


More information about the nginx-devel mailing list