Skip to content
This repository
Browse code

bugfix: ngx.log() would truncate the log messages with a null charact…

…er (\0) in it. thanks Wang Xi for reporting this issue.
  • Loading branch information...
commit 8d92e6c670b65ae4c21d4dbb75a2175ec98ad455 1 parent 7c0948a
Yichun Zhang authored November 20, 2012
10  src/ngx_http_lua_log.c
@@ -181,7 +181,7 @@ log_wrapper(ngx_log_t *log, const char *ident, ngx_uint_t level,
181 181
         }
182 182
     }
183 183
 
184  
-    buf = lua_newuserdata(L, size + 1);
  184
+    buf = lua_newuserdata(L, size);
185 185
 
186 186
     p = ngx_copy(buf, name.data, name.len);
187 187
 
@@ -245,14 +245,12 @@ log_wrapper(ngx_log_t *log, const char *ident, ngx_uint_t level,
245 245
         }
246 246
     }
247 247
 
248  
-    *p++ = '\0';
249  
-
250  
-    if (p - buf > (off_t) (size + 1)) {
  248
+    if (p - buf > (off_t) size) {
251 249
         return luaL_error(L, "buffer error: %d > %d", (int) (p - buf),
252  
-                          (int) (size + 1));
  250
+                          (int) size);
253 251
     }
254 252
 
255  
-    ngx_log_error(level, log, 0, "%s%s", ident, buf);
  253
+    ngx_log_error(level, log, 0, "%s%*s", ident, (size_t) (p - buf), buf);
256 254
 
257 255
     return 0;
258 256
 }
21  t/009-log.t
@@ -8,7 +8,7 @@ log_level('debug'); # to ensure any log-level can be outputed
8 8
 
9 9
 repeat_each(2);
10 10
 
11  
-plan tests => repeat_each() * (blocks() * 3 + 3);
  11
+plan tests => repeat_each() * (blocks() * 3 + 4);
12 12
 
13 13
 #no_diff();
14 14
 #no_long_string();
@@ -435,3 +435,22 @@ GET /log
435 435
 --- error_log
436 436
 bad log level: 9
437 437
 
  438
+
  439
+
  440
+=== TEST 22: \0 in the log message
  441
+--- config
  442
+    location = /t {
  443
+        content_by_lua '
  444
+            ngx.log(ngx.WARN, "hello\\0world")
  445
+            ngx.say("ok")
  446
+        ';
  447
+    }
  448
+--- request
  449
+GET /t
  450
+--- response_body
  451
+ok
  452
+--- no_error_log
  453
+[error]
  454
+--- error_log eval
  455
+"2: hello\0world, client: "
  456
+

0 notes on commit 8d92e6c

Please sign in to comment.
Something went wrong with that request. Please try again.