@@ -592,7 +592,8 @@ static void
592592log_vprintf (const struct ly_ctx * ctx , LY_LOG_LEVEL level , LY_ERR err , LY_VECODE vecode , char * data_path ,
593593 char * schema_path , uint64_t line , const char * apptag , const char * format , va_list args )
594594{
595- char * msg = NULL ;
595+ char * dyn_msg = NULL ;
596+ const char * msg ;
596597 ly_bool free_strs = 1 , lolog , lostore ;
597598
598599 /* learn effective logger options */
@@ -610,33 +611,26 @@ log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR err, LY_VECODE
610611 }
611612
612613 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 ;
622622 }
623- goto cleanup ;
624- }
623+ msg = dyn_msg ;
625624
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 );
630627 }
631628
632- /* store as the last message */
633- strncpy (last_msg , msg , LY_LAST_MSG_SIZE - 1 );
634-
635629 /* 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 ) {
638632 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 )) {
640634 goto cleanup ;
641635 }
642636 }
@@ -656,7 +650,7 @@ log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR err, LY_VECODE
656650 if (free_strs ) {
657651 free (data_path );
658652 free (schema_path );
659- free (msg );
653+ free (dyn_msg );
660654 }
661655}
662656
0 commit comments