@@ -160,21 +160,25 @@ void log_printf(int level, const char *file, const char *func, int line,
160
160
time_t now ;
161
161
struct tm * tm ;
162
162
char buf [LOG_MAX_LEN ];
163
+ int num_chars_written = 0 ;
163
164
164
165
/* Get local time and format as 2020-07-03 05:17:42 -0400 */
165
166
if (time (& now ) == -1 || (tm = localtime (& now )) == NULL ||
166
- strftime (buf , sizeof (buf ), "[%F %T %z]" , tm ) == 0 )
167
- sprintf (buf , "[-]" );
167
+ strftime (buf , sizeof (buf ), "[%F %T %z]" , tm ) == 0 ) {
168
+ num_chars_written = sprintf (buf , "[-]" );
169
+ assert (num_chars_written >= 0 && "sprintf write error to buf" );
170
+ }
168
171
169
172
/* Output thread ID, log level, file name, function number, and line number.
170
173
* Note that pthread_t on most platforms is unsigned long but is a struct
171
174
* of 8 bytes on z/OS.
172
175
*/
173
176
pthread_t tid = get_threadid ();
174
177
assert (LOG_MAX_LEN >= strlen (buf ) && "error in snprintf length" );
175
- snprintf (buf + strlen (buf ), LOG_MAX_LEN - strlen (buf ),
178
+ num_chars_written = snprintf (buf + strlen (buf ), LOG_MAX_LEN - strlen (buf ),
176
179
"[%016lx][%s]%s:%s:%d " , * (unsigned long * )& tid , log_level_name [level ],
177
180
get_filename (file ), func , line );
181
+ assert (num_chars_written >= 0 && "snprintf write error to buf" );
178
182
179
183
/* Output actual log data */
180
184
/* Definition of vsnprintf:
@@ -203,11 +207,15 @@ void log_printf(int level, const char *file, const char *func, int line,
203
207
204
208
va_list log_data ;
205
209
va_start (log_data , fmt );
206
- vsnprintf (buf + strlen (buf ), LOG_MAX_LEN - strlen (buf ), fmt , log_data );
210
+ num_chars_written =
211
+ vsnprintf (buf + strlen (buf ), LOG_MAX_LEN - strlen (buf ), fmt , log_data );
212
+ assert (num_chars_written >= 0 && "vsnprintf write error to buf" );
207
213
va_end (log_data );
208
214
209
215
/* Add new line */
210
- snprintf (buf + strlen (buf ), LOG_MAX_LEN - strlen (buf ), "\n" );
216
+ num_chars_written =
217
+ snprintf (buf + strlen (buf ), LOG_MAX_LEN - strlen (buf ), "\n" );
218
+ assert (num_chars_written >= 0 && "snprintf write error to buf" );
211
219
212
220
/* Write out and flush the output buffer */
213
221
FILE * fp = get_log_fp ();
@@ -238,9 +246,11 @@ static FILE *get_log_file_by_name(char *name) {
238
246
char * tname = (char * )malloc (strlen (name ) + 32 );
239
247
if (tname ) {
240
248
pthread_t tid = get_threadid ();
241
- snprintf (
249
+ int num_chars_written = snprintf (
242
250
tname , strlen (name ) + 32 , "%s.%016lx" , name , * (unsigned long * )& tid );
251
+ assert (num_chars_written >= 0 && "snprintf write error to tname" );
243
252
fp = fopen (tname , "w" );
253
+ assert (fp != NULL && "fopen error on tname" );
244
254
free (tname );
245
255
}
246
256
}
0 commit comments