Skip to content

Commit 8293af9

Browse files
authored
Fix bug when writing empty string to log (#3998)
1 parent 3249dfb commit 8293af9

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed

pjlib/src/pj/log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,11 @@ PJ_DEF(void) pj_log( const char *sender, int level,
468468
print_len = pj_ansi_snprintf(pre, sizeof(log_buffer)-len,
469469
"<logging error: msg too long>");
470470
}
471-
if (print_len < 1 || print_len >= (int)(sizeof(log_buffer)-len)) {
471+
if (print_len < 0 || print_len >= (int)(sizeof(log_buffer)-len)) {
472472
print_len = sizeof(log_buffer) - len - 1;
473473
}
474474
len = len + print_len;
475-
if (len > 0 && len < (int)sizeof(log_buffer)-2) {
475+
if (len >= 0 && len < (int)sizeof(log_buffer)-2) {
476476
if (log_decor & PJ_LOG_HAS_CR) {
477477
log_buffer[len++] = '\r';
478478
}

pjlib/src/pjlib-test/os.c

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "test.h"
2020
#include <pj/log.h>
2121
#include <pj/os.h>
22+
#include <string.h>
23+
#include <stdio.h>
24+
25+
#define THIS_FILE "os.c"
2226

2327
#if INCLUDE_OS_TEST
2428
static int endianness_test32(void)
@@ -90,6 +94,141 @@ static int endianness_test32(void)
9094
return 0;
9195
}
9296

97+
static int log_written;
98+
static char test_large_msg[PJ_LOG_MAX_SIZE];
99+
100+
static void log_write(int level, const char *buffer, int len)
101+
{
102+
log_written = len;
103+
//printf("%s", buffer);
104+
}
105+
106+
int log_test(void)
107+
{
108+
pj_log_func *old_func = pj_log_get_log_func();
109+
struct log_test_t {
110+
const char *title;
111+
unsigned decor;
112+
const char *fmt;
113+
const char *arg;
114+
int min_len;
115+
int max_len;
116+
} log_tests[] = {
117+
/* String lengths:
118+
* PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC: 13
119+
* PJ_LOG_HAS_SENDER: PJ_LOG_SENDER_WIDTH+1
120+
* "Hello world!": 12
121+
* PJ_LOG_HAS_NEWLINE: 1
122+
*/
123+
{
124+
"normal log",
125+
PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC |
126+
PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE,
127+
"Hello %s!",
128+
"world",
129+
13+PJ_LOG_SENDER_WIDTH+1+12+1,
130+
13+PJ_LOG_SENDER_WIDTH+1+12+1,
131+
},
132+
{
133+
"normal log with no decor",
134+
0,
135+
"Hello %s!",
136+
"world",
137+
12,
138+
12,
139+
},
140+
{
141+
"normal log with just newline",
142+
PJ_LOG_HAS_NEWLINE,
143+
"Hello %s!",
144+
"world",
145+
13,
146+
13,
147+
},
148+
{
149+
"empty string with normal decor",
150+
PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC |
151+
PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE,
152+
"%s",
153+
"",
154+
13+PJ_LOG_SENDER_WIDTH+1+1,
155+
13+PJ_LOG_SENDER_WIDTH+1+1,
156+
},
157+
{
158+
"empty string with nodecor",
159+
0,
160+
"%s",
161+
"",
162+
0,
163+
0,
164+
},
165+
{
166+
"empty string with just newline",
167+
PJ_LOG_HAS_NEWLINE,
168+
"%s",
169+
"",
170+
1,
171+
1,
172+
},
173+
{
174+
"large message with normal decor",
175+
PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC |
176+
PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE,
177+
"%s",
178+
test_large_msg,
179+
PJ_LOG_MAX_SIZE-1,
180+
PJ_LOG_MAX_SIZE-1,
181+
},
182+
{
183+
"large message with no decor",
184+
0,
185+
"%s",
186+
test_large_msg,
187+
PJ_LOG_MAX_SIZE-1,
188+
PJ_LOG_MAX_SIZE-1,
189+
},
190+
{
191+
"large message with just newline",
192+
PJ_LOG_HAS_NEWLINE,
193+
"%s",
194+
test_large_msg,
195+
PJ_LOG_MAX_SIZE-1,
196+
PJ_LOG_MAX_SIZE-1,
197+
},
198+
};
199+
unsigned old_decor = pj_log_get_decor();
200+
unsigned i;
201+
202+
memset(test_large_msg, 'A', sizeof(test_large_msg));
203+
test_large_msg[sizeof(test_large_msg)-1] = '\0';
204+
205+
pj_log_set_log_func( &log_write );
206+
207+
for (i=0; i<PJ_ARRAY_SIZE(log_tests); ++i) {
208+
struct log_test_t *t = &log_tests[i];
209+
210+
log_written = -1;
211+
pj_log_set_decor(t->decor);
212+
213+
PJ_LOG(1,(THIS_FILE, t->fmt, t->arg));
214+
215+
if (log_written < t->min_len || log_written > t->max_len) {
216+
pj_log_set_log_func( old_func );
217+
pj_log_set_decor(old_decor);
218+
219+
PJ_LOG(1,(THIS_FILE,
220+
" Error: in test %d (%s), writing (\"%s\", \"%s\"), expecting %d<=len<=%d, got len=%d",
221+
i, t->title, t->fmt, t->arg, t->min_len, t->max_len, log_written));
222+
return 33;
223+
}
224+
}
225+
226+
pj_log_set_log_func( old_func );
227+
pj_log_set_decor(old_decor);
228+
229+
return 0;
230+
}
231+
93232
int os_test(void)
94233
{
95234
const pj_sys_info *si;

pjlib/src/pjlib-test/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ int test_inner(void)
8989
#endif
9090

9191
#if INCLUDE_OS_TEST
92+
DO_TEST( log_test() );
9293
DO_TEST( os_test() );
9394
#endif
9495

pjlib/src/pjlib-test/test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ extern int exception_test(void);
8787
extern int rand_test(void);
8888
extern int list_test(void);
8989
extern int hash_test(void);
90+
extern int log_test(void);
9091
extern int os_test(void);
9192
extern int pool_test(void);
9293
extern int pool_perf_test(void);

0 commit comments

Comments
 (0)