@@ -592,7 +592,8 @@ static void
592
592
log_vprintf (const struct ly_ctx * ctx , LY_LOG_LEVEL level , LY_ERR err , LY_VECODE vecode , char * data_path ,
593
593
char * schema_path , uint64_t line , const char * apptag , const char * format , va_list args )
594
594
{
595
- char * msg = NULL ;
595
+ char * dyn_msg = NULL ;
596
+ const char * msg ;
596
597
ly_bool free_strs = 1 , lolog , lostore ;
597
598
598
599
/* learn effective logger options */
@@ -610,33 +611,26 @@ log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR err, LY_VECODE
610
611
}
611
612
612
613
if (err == LY_EMEM ) {
613
- /* just print it, anything else would most likely fail anyway */
614
- if (lolog ) {
615
- if (log_clb ) {
616
- log_clb (level , LY_EMEM_MSG , data_path , schema_path , line );
617
- } else {
618
- fprintf (stderr , "libyang[%d]: " , level );
619
- vfprintf (stderr , format , args );
620
- log_stderr_path_line (data_path , schema_path , line );
621
- }
614
+ /* no not use more dynamic memory */
615
+ vsnprintf (last_msg , LY_LAST_MSG_SIZE , format , args );
616
+ msg = last_msg ;
617
+ } else {
618
+ /* print into a single message */
619
+ if (vasprintf (& dyn_msg , format , args ) == -1 ) {
620
+ LOGMEM (ctx );
621
+ goto cleanup ;
622
622
}
623
- goto cleanup ;
624
- }
623
+ msg = dyn_msg ;
625
624
626
- /* print into a single message */
627
- if (vasprintf (& msg , format , args ) == -1 ) {
628
- LOGMEM (ctx );
629
- goto cleanup ;
625
+ /* store as the last message */
626
+ strncpy (last_msg , msg , LY_LAST_MSG_SIZE - 1 );
630
627
}
631
628
632
- /* store as the last message */
633
- strncpy (last_msg , msg , LY_LAST_MSG_SIZE - 1 );
634
-
635
629
/* store the error/warning in the context (if we need to store errors internally, it does not matter what are
636
- * the user log options) */
637
- if ((level < LY_LLVRB ) && ctx && lostore ) {
630
+ * the user log options), if the message is not dynamic, it would most likely fail to store (no memory) */
631
+ if ((level < LY_LLVRB ) && ctx && lostore && dyn_msg ) {
638
632
free_strs = 0 ;
639
- if (log_store (ctx , level , err , vecode , msg , data_path , schema_path , line , apptag ? strdup (apptag ) : NULL )) {
633
+ if (log_store (ctx , level , err , vecode , dyn_msg , data_path , schema_path , line , apptag ? strdup (apptag ) : NULL )) {
640
634
goto cleanup ;
641
635
}
642
636
}
@@ -656,7 +650,7 @@ log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR err, LY_VECODE
656
650
if (free_strs ) {
657
651
free (data_path );
658
652
free (schema_path );
659
- free (msg );
653
+ free (dyn_msg );
660
654
}
661
655
}
662
656
0 commit comments