@@ -101,24 +101,30 @@ int zlog_set_level(int new_value) /* {{{ */
101
101
void vzlog (const char * function , int line , int flags , const char * fmt , va_list args ) /* {{{ */
102
102
{
103
103
struct timeval tv ;
104
- char buf [MAX_LINE_LENGTH ];
105
- const size_t buf_size = MAX_LINE_LENGTH ;
106
- size_t len = 0 ;
107
- int truncated = 0 ;
104
+ char tmp_buf [MAX_LINE_LENGTH ];
105
+ int tmp_buf_size = MAX_LINE_LENGTH ;
106
+ int tmp_buf_len = 0 ;
107
+
108
+ char * final_buf ;
109
+ int final_buf_len ;
110
+
111
+ char * fmt_buf ;
112
+ int fmt_buf_len ;
113
+
108
114
int saved_errno ;
109
115
110
116
if (external_logger ) {
111
117
va_list ap ;
112
118
va_copy (ap , args );
113
- len = vsnprintf ( buf , buf_size , fmt , ap );
119
+ fmt_buf_len = vasprintf ( & fmt_buf , fmt , ap );
114
120
va_end (ap );
115
- if (len >= buf_size ) {
116
- memcpy (buf + buf_size - sizeof ("..." ), "..." , sizeof ("..." ) - 1 );
117
- len = buf_size - 1 ;
121
+ if (fmt_buf_len < 0 ) {
122
+ external_logger (flags & ZLOG_LEVEL_MASK , "Can't allocate memory for zlog()" , strlen ("Can't allocate memory for zlog()" ));
123
+ } else {
124
+ external_logger (flags & ZLOG_LEVEL_MASK , fmt_buf , fmt_buf_len );
125
+ fmt_buf_len = 0 ;
126
+ free (fmt_buf );
118
127
}
119
- external_logger (flags & ZLOG_LEVEL_MASK , buf , len );
120
- len = 0 ;
121
- memset (buf , '\0' , buf_size );
122
128
}
123
129
124
130
if ((flags & ZLOG_LEVEL_MASK ) < zlog_level ) {
@@ -128,70 +134,61 @@ void vzlog(const char *function, int line, int flags, const char *fmt, va_list a
128
134
saved_errno = errno ;
129
135
#ifdef HAVE_SYSLOG_H
130
136
if (zlog_fd == ZLOG_SYSLOG /* && !fpm_globals.is_child */ ) {
131
- len = 0 ;
132
137
if (zlog_level == ZLOG_DEBUG ) {
133
- len += snprintf (buf , buf_size , "[%s] %s(), line %d: " , level_names [flags & ZLOG_LEVEL_MASK ], function , line );
138
+ tmp_buf_len += snprintf (tmp_buf , tmp_buf_size , "[%s] %s(), line %d: " , level_names [flags & ZLOG_LEVEL_MASK ], function , line );
134
139
} else {
135
- len += snprintf (buf , buf_size , "[%s] " , level_names [flags & ZLOG_LEVEL_MASK ]);
140
+ tmp_buf_len += snprintf (tmp_buf , tmp_buf_size , "[%s] " , level_names [flags & ZLOG_LEVEL_MASK ]);
136
141
}
137
142
} else
138
143
#endif
139
144
{
140
145
if (!fpm_globals .is_child ) {
141
146
gettimeofday (& tv , 0 );
142
- len = zlog_print_time (& tv , buf , buf_size );
147
+ tmp_buf_len = zlog_print_time (& tv , tmp_buf , tmp_buf_size );
143
148
}
144
149
if (zlog_level == ZLOG_DEBUG ) {
145
150
if (!fpm_globals .is_child ) {
146
- len += snprintf (buf + len , buf_size - len , "%s: pid %d, %s(), line %d: " , level_names [flags & ZLOG_LEVEL_MASK ], getpid (), function , line );
151
+ tmp_buf_len += snprintf (tmp_buf + tmp_buf_len , tmp_buf_size - tmp_buf_len , "%s: pid %d, %s(), line %d: " , level_names [flags & ZLOG_LEVEL_MASK ], getpid (), function , line );
147
152
} else {
148
- len += snprintf (buf + len , buf_size - len , "%s: %s(), line %d: " , level_names [flags & ZLOG_LEVEL_MASK ], function , line );
153
+ tmp_buf_len += snprintf (tmp_buf + tmp_buf_len , tmp_buf_size - tmp_buf_len , "%s: %s(), line %d: " , level_names [flags & ZLOG_LEVEL_MASK ], function , line );
149
154
}
150
155
} else {
151
- len += snprintf (buf + len , buf_size - len , "%s: " , level_names [flags & ZLOG_LEVEL_MASK ]);
152
- }
153
- }
154
-
155
- if (len > buf_size - 1 ) {
156
- truncated = 1 ;
157
- }
158
-
159
- if (!truncated ) {
160
- len += vsnprintf (buf + len , buf_size - len , fmt , args );
161
- if (len >= buf_size ) {
162
- truncated = 1 ;
156
+ tmp_buf_len += snprintf (tmp_buf + tmp_buf_len , tmp_buf_size - tmp_buf_len , "%s: " , level_names [flags & ZLOG_LEVEL_MASK ]);
163
157
}
164
158
}
165
159
166
- if (!truncated ) {
167
- if (flags & ZLOG_HAVE_ERRNO ) {
168
- len += snprintf (buf + len , buf_size - len , ": %s (%d)" , strerror (saved_errno ), saved_errno );
169
- if (len >= buf_size ) {
170
- truncated = 1 ;
171
- }
172
- }
160
+ if (flags & ZLOG_HAVE_ERRNO ) {
161
+ tmp_buf_len += snprintf (tmp_buf + tmp_buf_len , tmp_buf_size - tmp_buf_len , ": %s (%d)" , strerror (saved_errno ), saved_errno );
173
162
}
174
163
175
- if (truncated ) {
176
- memcpy (buf + buf_size - sizeof ("..." ), "..." , sizeof ("..." ) - 1 );
177
- len = buf_size - 1 ;
164
+ fmt_buf_len = vasprintf (& fmt_buf , fmt , args );
165
+ if (fmt_buf_len < 0 ) {
166
+ final_buf = strndup ("Can't allocate memory for zlog()" , strlen ("Can't allocate memory for zlog()" ));
167
+ final_buf_len = strlen ("Can't allocate memory for zlog()" );
168
+ } else {
169
+ final_buf_len = tmp_buf_len + fmt_buf_len + 2 ;
170
+
171
+ final_buf = calloc (1 , final_buf_len );
172
+ memcpy (final_buf , tmp_buf , tmp_buf_len );
173
+ memcpy (final_buf + tmp_buf_len , fmt_buf , fmt_buf_len );
174
+ free (fmt_buf );
178
175
}
179
176
180
177
#ifdef HAVE_SYSLOG_H
181
178
if (zlog_fd == ZLOG_SYSLOG ) {
182
- buf [len ] = '\0' ;
183
- php_syslog (syslog_priorities [zlog_level ], "%s" , buf );
184
- buf [len ++ ] = '\n' ;
179
+ php_syslog (syslog_priorities [zlog_level ], "%s" , final_buf );
185
180
} else
186
181
#endif
187
182
{
188
- buf [ len ++ ] = '\n' ;
189
- write (zlog_fd > -1 ? zlog_fd : STDERR_FILENO , buf , len );
183
+ final_buf [ final_buf_len - 2 ] = '\n' ;
184
+ write (zlog_fd > -1 ? zlog_fd : STDERR_FILENO , final_buf , final_buf_len );
190
185
}
191
186
192
187
if (zlog_fd != STDERR_FILENO && zlog_fd != -1 && !launched && (flags & ZLOG_LEVEL_MASK ) >= ZLOG_NOTICE ) {
193
- write (STDERR_FILENO , buf , len );
188
+ write (STDERR_FILENO , final_buf , final_buf_len );
194
189
}
190
+
191
+ free (final_buf );
195
192
}
196
193
/* }}} */
197
194
0 commit comments