7
7
*
8
8
*/
9
9
10
+ #include "base_alloc_global.h"
11
+ #include "umf/base.h"
10
12
#ifdef _WIN32
11
13
#include <windows.h>
12
14
#else
@@ -66,11 +68,11 @@ typedef struct {
66
68
utils_log_level_t level ;
67
69
utils_log_level_t flushLevel ;
68
70
FILE * output ;
69
- const char * file_name ;
71
+ char file_name [ MAX_FILE_PATH ] ;
70
72
} utils_log_config_t ;
71
73
72
74
utils_log_config_t loggerConfig = {false, false, LOG_ERROR ,
73
- LOG_ERROR , NULL , NULL };
75
+ LOG_ERROR , NULL , "" };
74
76
75
77
static const char * level_to_str (utils_log_level_t l ) {
76
78
switch (l ) {
@@ -257,10 +259,10 @@ void utils_log_init(void) {
257
259
const char * arg ;
258
260
if (utils_parse_var (envVar , "output:stdout" , NULL )) {
259
261
loggerConfig .output = stdout ;
260
- loggerConfig .file_name = "stdout" ;
262
+ strncpy ( loggerConfig .file_name , "stdout" , MAX_FILE_PATH ) ;
261
263
} else if (utils_parse_var (envVar , "output:stderr" , NULL )) {
262
264
loggerConfig .output = stderr ;
263
- loggerConfig .file_name = "stderr" ;
265
+ strncpy ( loggerConfig .file_name , "stderr" , MAX_FILE_PATH ) ;
264
266
} else if (utils_parse_var (envVar , "output:file" , & arg )) {
265
267
loggerConfig .output = NULL ;
266
268
const char * argEnd = strstr (arg , ";" );
@@ -289,7 +291,9 @@ void utils_log_init(void) {
289
291
loggerConfig .output = NULL ;
290
292
return ;
291
293
}
292
- loggerConfig .file_name = file ;
294
+ strncpy (loggerConfig .file_name , file , MAX_FILE_PATH - 1 );
295
+ loggerConfig .file_name [MAX_FILE_PATH - 1 ] =
296
+ '\0' ; // ensure null-termination
293
297
} else {
294
298
loggerConfig .output = stderr ;
295
299
LOG_ERR ("Logging output not set - logging disabled (UMF_LOG = \"%s\")" ,
@@ -506,17 +510,29 @@ static umf_result_t CTL_READ_HANDLER(output)(void *ctx,
506
510
/* suppress unused-parameter errors */
507
511
(void )source , (void )indexes , (void )ctx ;
508
512
509
- const char * * arg_out = (const char * * )arg ;
510
- if (arg_out == NULL || size < sizeof ( const char * ) ) {
513
+ char * arg_out = (char * )arg ;
514
+ if (arg_out == NULL ) {
511
515
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
512
516
}
513
517
514
518
if (loggerConfig .output == NULL ) {
515
- * arg_out = "disabled" ;
519
+ const char disabled [] = "disabled" ;
520
+ if (size < sizeof (disabled )) {
521
+ LOG_ERR ("Invalid output argument size: %zu, expected at least %zu" ,
522
+ size , sizeof (disabled ));
523
+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
524
+ }
525
+
526
+ strncpy (arg_out , disabled , size );
516
527
return UMF_RESULT_SUCCESS ;
517
528
}
529
+ if (size < strlen (loggerConfig .file_name )) {
530
+ LOG_ERR ("Invalid output argument size: %zu, expected at least %zu" ,
531
+ size , strlen (loggerConfig .file_name ));
532
+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
533
+ }
518
534
519
- * arg_out = loggerConfig .file_name ;
535
+ strncpy ( arg_out , loggerConfig .file_name , size ) ;
520
536
return UMF_RESULT_SUCCESS ;
521
537
}
522
538
@@ -525,16 +541,13 @@ static umf_result_t CTL_WRITE_HANDLER(output)(void *ctx,
525
541
void * arg , size_t size ,
526
542
umf_ctl_index_utlist_t * indexes ) {
527
543
/* suppress unused-parameter errors */
528
- (void )source , (void )indexes , (void )ctx ;
544
+ (void )source , (void )indexes , (void )ctx , ( void ) size ;
529
545
530
- const char * arg_in = * (const char * * )arg ;
531
- if (size < sizeof (const char * )) {
532
- return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
533
- }
546
+ const char * arg_in = (const char * )arg ;
534
547
535
548
FILE * oldHandle = loggerConfig .output ;
536
549
const char * oldName =
537
- loggerConfig .file_name ? loggerConfig .file_name : "disabled" ;
550
+ * loggerConfig .file_name == '\0' ? loggerConfig .file_name : "disabled" ;
538
551
539
552
if (arg_in == NULL ) {
540
553
if (loggerConfig .output ) {
@@ -543,7 +556,7 @@ static umf_result_t CTL_WRITE_HANDLER(output)(void *ctx,
543
556
fclose (oldHandle );
544
557
}
545
558
loggerConfig .output = NULL ;
546
- loggerConfig .file_name = NULL ;
559
+ loggerConfig .file_name [ 0 ] = '\0' ;
547
560
}
548
561
return UMF_RESULT_SUCCESS ;
549
562
}
@@ -552,16 +565,18 @@ static umf_result_t CTL_WRITE_HANDLER(output)(void *ctx,
552
565
553
566
if (strcmp (arg_in , "stdout" ) == 0 ) {
554
567
newHandle = stdout ;
555
- loggerConfig .file_name = "stdout" ;
568
+ strncpy ( loggerConfig .file_name , "stdout" , MAX_FILE_PATH ) ;
556
569
} else if (strcmp (arg_in , "stderr" ) == 0 ) {
557
570
newHandle = stderr ;
558
- loggerConfig .file_name = "stderr" ;
571
+ strncpy ( loggerConfig .file_name , "stderr" , MAX_FILE_PATH ) ;
559
572
} else {
560
573
newHandle = fopen (arg_in , "a" );
561
574
if (!newHandle ) {
562
575
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
563
576
}
564
- loggerConfig .file_name = arg_in ;
577
+ strncpy (loggerConfig .file_name , arg_in , MAX_FILE_PATH - 1 );
578
+ loggerConfig .file_name [MAX_FILE_PATH - 1 ] =
579
+ '\0' ; // ensure null-termination
565
580
}
566
581
567
582
loggerConfig .output = newHandle ;
0 commit comments