Unexpected structure of macros in ngx_string.h
Chris Newton
cnewton at netflix.com
Tue Jan 12 00:40:06 UTC 2021
Hello
I just came across a problem with an innocuous looking line of code in a
new module I have been working on:
if (tmp_str.len == 0)
ngx_str_set(&tmp_str, "/");
The modification of tmp_str.data to '/' was always being made, but the
length wasn't if the test failed. This turns out to be caused by the style
used in the definition of the ngx_str_set macro.
Altering this macro definition (and ngx_str_null which is similar) would
protect against this:
*--- a/ports/netflix/nginx/files/nginx/src/core/ngx_string.h*
*+++ b/ports/netflix/nginx/files/nginx/src/core/ngx_string.h*
@@ -40,8 +40,9 @@ typedef struct {
#define ngx_string(str) { sizeof(str) - 1, (u_char *) str }
#define ngx_null_string { 0, NULL }
#define ngx_str_set(str, text)
\
- (str)->len = sizeof(text) - 1; (str)->data = (u_char *) text
-#define ngx_str_null(str) (str)->len = 0; (str)->data = NULL
+ do { (str)->len = sizeof(text) - 1; (str)->data = (u_char *) text; }
while (0)
+#define ngx_str_null(str)
\
+ do { (str)->len = 0; (str)->data = NULL; } while (0)
#define ngx_tolower(c) (u_char) ((c >= 'A' && c <= 'Z') ? (c | 0x20)
: c)
I haven't looked further to see if others would benefit from such a change;
figured I would run by you first
TIA
Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20210112/9ddada85/attachment.htm>
More information about the nginx-devel
mailing list