@@ -453,11 +453,13 @@ ly_vlog_build_path_reverse(enum LY_VLOG_ELEM elem_type, const void *elem, char *
453453 }
454454
455455 if (((struct lys_node * )elem )-> nodetype & (LYS_AUGMENT | LYS_GROUPING )) {
456+ LY_CHECK_ERR_RETURN ((* index ) < 1 , LOGERR (LY_SUCCESS , "%s: path is too long." ), );
456457 -- (* index );
457458 path [* index ] = ']' ;
458459
459460 name = ((struct lys_node * )elem )-> name ;
460461 len = strlen (name );
462+ LY_CHECK_ERR_RETURN ((* index ) < len , LOGERR (LY_SUCCESS , "%s: path is too long." ), );
461463 (* index ) -= len ;
462464 memcpy (& path [* index ], name , len );
463465
@@ -523,19 +525,24 @@ ly_vlog_build_path_reverse(enum LY_VLOG_ELEM elem_type, const void *elem, char *
523525 val_end = "']" ;
524526 }
525527
528+ len = strlen (((struct lyd_node_leaf_list * )diter )-> value_str );
529+ LY_CHECK_ERR_RETURN ((* index ) < len + 2 , LOGERR (LY_SUCCESS , "%s: path is too long." ), );
526530 (* index ) -= 2 ;
527531 memcpy (& path [(* index )], val_end , 2 );
528- len = strlen (((struct lyd_node_leaf_list * )diter )-> value_str );
529532 (* index ) -= len ;
530533 memcpy (& path [(* index )], ((struct lyd_node_leaf_list * )diter )-> value_str , len );
534+
535+ len = strlen (diter -> schema -> name );
536+ LY_CHECK_ERR_RETURN ((* index ) < len + 3 , LOGERR (LY_SUCCESS , "%s: path is too long." ), );
531537 (* index ) -= 2 ;
532538 memcpy (& path [(* index )], val_start , 2 );
533- len = strlen (diter -> schema -> name );
534539 (* index ) -= len ;
535540 memcpy (& path [(* index )], diter -> schema -> name , len );
541+
536542 if (lyd_node_module (dlist ) != lyd_node_module (diter )) {
537- path [-- (* index )] = ':' ;
538543 len = strlen (lyd_node_module (diter )-> name );
544+ LY_CHECK_ERR_RETURN ((* index ) < len + 2 , LOGERR (LY_SUCCESS , "%s: path is too long." ), );
545+ path [-- (* index )] = ':' ;
539546 (* index ) -= len ;
540547 memcpy (& path [(* index )], lyd_node_module (diter )-> name , len );
541548 }
@@ -544,7 +551,6 @@ ly_vlog_build_path_reverse(enum LY_VLOG_ELEM elem_type, const void *elem, char *
544551 }
545552 } else {
546553 /* schema list without keys - use instance position */
547- path [-- (* index )] = ']' ;
548554
549555 i = j = lyd_list_pos (dlist );
550556 len = 1 ;
@@ -557,6 +563,8 @@ ly_vlog_build_path_reverse(enum LY_VLOG_ELEM elem_type, const void *elem, char *
557563 LY_CHECK_ERR_RETURN (!str , LOGMEM , );
558564 sprintf (str , "%d" , i );
559565
566+ LY_CHECK_ERR_RETURN ((* index ) < len + 2 , LOGERR (LY_SUCCESS , "%s: path is too long." ), );
567+ path [-- (* index )] = ']' ;
560568 (* index ) -= len ;
561569 strncpy (& path [(* index )], str , len );
562570
@@ -575,9 +583,10 @@ ly_vlog_build_path_reverse(enum LY_VLOG_ELEM elem_type, const void *elem, char *
575583 val_end = "']" ;
576584 }
577585
586+ len = strlen (((struct lyd_node_leaf_list * )elem )-> value_str );
587+ LY_CHECK_ERR_RETURN ((* index ) < len + 6 , LOGERR (LY_SUCCESS , "%s: path is too long." ), );
578588 (* index ) -= 2 ;
579589 memcpy (& path [(* index )], val_end , 2 );
580- len = strlen (((struct lyd_node_leaf_list * )elem )-> value_str );
581590 (* index ) -= len ;
582591 memcpy (& path [(* index )], ((struct lyd_node_leaf_list * )elem )-> value_str , len );
583592 (* index ) -= 4 ;
@@ -588,10 +597,11 @@ ly_vlog_build_path_reverse(enum LY_VLOG_ELEM elem_type, const void *elem, char *
588597 break ;
589598 case LY_VLOG_STR :
590599 len = strlen ((const char * )elem ) + 1 ;
591- if (len > LY_BUF_SIZE ) {
592- len = LY_BUF_SIZE - 1 ;
600+ if ((* index ) < len ) {
601+ LOGERR (LY_SUCCESS , "%s: path is too long." )
602+ len = (* index );
593603 }
594- (* index ) = LY_BUF_SIZE - len ;
604+ (* index ) = ( * index ) - len ;
595605 memcpy (& path [(* index )], (const char * )elem , len - 1 );
596606 return ;
597607 default :
@@ -601,19 +611,23 @@ ly_vlog_build_path_reverse(enum LY_VLOG_ELEM elem_type, const void *elem, char *
601611 }
602612 if (name ) {
603613 len = strlen (name );
614+ LY_CHECK_ERR_RETURN ((* index ) < len , LOGERR (LY_SUCCESS , "%s: path is too long." ), );
604615 (* index ) -= len ;
605616 memcpy (& path [* index ], name , len );
606617 if (prefix ) {
607- path [-- (* index )] = ':' ;
608618 len = strlen (prefix );
609- (* index ) = (* index ) - len ;
619+ LY_CHECK_ERR_RETURN ((* index ) < len + 1 , LOGERR (LY_SUCCESS , "%s: path is too long." ), );
620+ path [-- (* index )] = ':' ;
621+ (* index ) -= len ;
610622 memcpy (& path [(* index )], prefix , len );
611623 }
612624 }
625+ LY_CHECK_ERR_RETURN ((* index ) < 1 , LOGERR (LY_SUCCESS , "%s: path is too long." ), );
613626 path [-- (* index )] = '/' ;
614627 if (elem_type == LY_VLOG_LYS && !elem && sparent && sparent -> nodetype == LYS_AUGMENT ) {
615628 len = strlen (((struct lys_node_augment * )sparent )-> target_name );
616- (* index ) = (* index ) - len ;
629+ LY_CHECK_ERR_RETURN ((* index ) < len , LOGERR (LY_SUCCESS , "%s: path is too long." ), );
630+ (* index ) -= len ;
617631 memcpy (& path [(* index )], ((struct lys_node_augment * )sparent )-> target_name , len );
618632 }
619633 }
0 commit comments