@@ -3767,7 +3767,13 @@ char * mkstr_dynamic(const char *fmt_dynamic, const char *fmt_reference, ...)
37673767static void vupslog (int priority , const char * fmt , va_list va , int use_strerror )
37683768{
37693769 int ret , errno_orig = errno ;
3770+ #ifdef HAVE_VA_COPY_VARIANT
3771+ size_t bufsize = 128 ;
3772+ #else
3773+ /* err on the safe(r) side, as re-runs can truncate
3774+ * the output when varargs are re-used */
37703775 size_t bufsize = LARGEBUF ;
3776+ #endif
37713777 char * buf = (char * )xcalloc (bufsize , sizeof (char ));
37723778
37733779 /* Be pedantic about our limitations */
@@ -3827,7 +3833,7 @@ static void vupslog(int priority, const char *fmt, va_list va, int use_strerror)
38273833 * Based on https://stackoverflow.com/a/72981237/4715872
38283834 */
38293835 if (bufsize < SIZE_MAX /2 ) {
3830- size_t newbufsize = bufsize * 2 ;
3836+ size_t newbufsize = bufsize < LARGEBUF ? LARGEBUF : bufsize * 2 ;
38313837 if (ret > 0 ) {
38323838 /* Be generous, we snprintfcat() some
38333839 * suffixes, prefix a timestamp, etc. */
@@ -4794,10 +4800,14 @@ static void nut_free_search_paths(void) {
47944800 }
47954801
47964802 if (search_paths != search_paths_builtin ) {
4803+ #if HAVE_DECL_REALPATH
47974804 size_t i ;
47984805 for (i = 0 ; search_paths [i ] != NULL ; i ++ ) {
4806+ upsdebugx (7 , "%s: freeing search_paths[%" PRIuSIZE "]: '%s'" ,
4807+ __func__ , i , NUT_STRARG (search_paths [i ]));
47994808 free ((char * )search_paths [i ]);
48004809 }
4810+ #endif /* else: curated selection of pointers to some of the built-in strings */
48014811 free (search_paths );
48024812 search_paths = search_paths_builtin ;
48034813 }
@@ -4819,10 +4829,17 @@ void nut_prepare_search_paths(void) {
48194829 size_t count_builtin = 0 , count_filtered = 0 , i , j , index = 0 ;
48204830 const char * * filtered_search_paths ;
48214831 DIR * dp ;
4832+ #if HAVE_DECL_REALPATH
4833+ /* Per docs, buffer must be at least PATH_MAX bytes */
4834+ char realpath_buf [NUT_PATH_MAX + 1 ] = {0 }, * realpath_dirname = NULL ;
4835+ #endif
48224836
48234837 /* As a starting point, allow at least as many items as before */
48244838 /* TODO: somehow extend (xrealloc?) if we mix other paths later */
4825- for (i = 0 ; search_paths_builtin [i ] != NULL ; i ++ ) {}
4839+ for (i = 0 ; search_paths_builtin [i ] != NULL ; i ++ ) {
4840+ /* Different way of printing with minimal crash-ability on older systems */
4841+ upsdebugx (7 , "counting search_paths_builtin[%d] : %s" , (int )i , NUT_STRARG (search_paths_builtin [i ]));
4842+ }
48264843 count_builtin = i + 1 ; /* +1 for the NULL */
48274844
48284845 /* Bytes inside should all be zeroed... */
@@ -4834,22 +4851,35 @@ void nut_prepare_search_paths(void) {
48344851 int dupe = 0 ;
48354852 const char * dirname = search_paths_builtin [i ];
48364853
4854+ upsdebugx (7 , "%s: checking search_paths_builtin[%" PRIuSIZE " of %" PRIuSIZE "] : %s" ,
4855+ __func__ , i , count_builtin - 1 , NUT_STRARG (dirname ));
48374856 if ((dp = opendir (dirname )) == NULL ) {
48384857 upsdebugx (5 , "%s: SKIP "
48394858 "unreachable directory #%" PRIuSIZE " : %s" ,
4840- __func__ , index ++ , dirname );
4859+ __func__ , index , NUT_STRARG (dirname ));
4860+ index ++ ;
48414861 continue ;
48424862 }
48434863 index ++ ;
48444864
48454865#if HAVE_DECL_REALPATH
48464866 /* allocates the buffer we free() later */
4847- dirname = (const char * )realpath (dirname , NULL );
4867+ upsdebugx (7 , "%s: call realpath()" , __func__ );
4868+ errno = 0 ;
4869+ realpath_dirname = realpath (dirname , realpath_buf );
4870+ if (errno || !realpath_dirname )
4871+ upsdebug_with_errno (7 , "%s: realpath() failed and returned: %s" , __func__ , NUT_STRARG (realpath_dirname ));
4872+ else
4873+ upsdebugx (7 , "%s: realpath() returned: %s" , __func__ , NUT_STRARG (realpath_dirname ));
4874+ if (realpath_dirname )
4875+ dirname = (const char * )realpath_dirname ;
48484876#endif
48494877
48504878 /* Revise for duplicates */
48514879 /* Note: (count_filtered == 0) means first existing dir seen, no hassle */
48524880 for (j = 0 ; j < count_filtered ; j ++ ) {
4881+ upsdebugx (7 , "%s: check for duplicates filtered_search_paths[%" PRIuSIZE " of %" PRIuSIZE "] : %s" ,
4882+ __func__ , j , count_filtered , NUT_STRARG (filtered_search_paths [j ]));
48534883 if (!strcmp (filtered_search_paths [j ], dirname )) {
48544884#if HAVE_DECL_REALPATH
48554885 if (strcmp (search_paths_builtin [i ], dirname )) {
@@ -4866,7 +4896,6 @@ void nut_prepare_search_paths(void) {
48664896
48674897 dupe = 1 ;
48684898#if HAVE_DECL_REALPATH
4869- free ((char * )dirname );
48704899 /* Have some valid value, for kicks (likely
48714900 * to be ignored in the code path below) */
48724901 dirname = search_paths_builtin [i ];
@@ -4879,9 +4908,10 @@ void nut_prepare_search_paths(void) {
48794908 upsdebugx (5 , "%s: ADD[#%" PRIuSIZE "] "
48804909 "existing unique directory: %s" ,
48814910 __func__ , count_filtered , dirname );
4882- #if !HAVE_DECL_REALPATH
4883- /* Make a copy of table entry, else we have
4884- * a dynamic result of realpath() made above.
4911+ #if HAVE_DECL_REALPATH
4912+ /* Make a copy of table entry, or the buffer
4913+ * with a result of realpath() made above,
4914+ * to eventually conststently free().
48854915 */
48864916 dirname = (const char * )xstrdup (dirname );
48874917#endif
@@ -4984,7 +5014,7 @@ static char * get_libname_in_dir(const char* base_libname, size_t base_libname_l
49845014 char current_test_path [NUT_PATH_MAX + 1 ];
49855015
49865016 upsdebugx (3 , "%s('%s', %" PRIuSIZE ", '%s', %i): Entering method..." ,
4987- __func__ , base_libname , base_libname_length , dirname , index );
5017+ __func__ , NUT_STRARG ( base_libname ) , base_libname_length , NUT_STRARG ( dirname ) , index );
49885018
49895019 memset (current_test_path , 0 , sizeof (current_test_path ));
49905020
@@ -5117,7 +5147,7 @@ static char * get_libname_in_pathset(const char* base_libname, size_t base_libna
51175147 /* First call to tokenization passes the string, others pass NULL */
51185148 pathset_tmp = xstrdup (pathset );
51195149 upsdebugx (4 , "%s: Looking for lib %s in a colon-separated path set" ,
5120- __func__ , base_libname );
5150+ __func__ , NUT_STRARG ( base_libname ) );
51215151 while (NULL != (onedir = strtok ( (onedir ? NULL : pathset_tmp ), ":" ))) {
51225152 libname_path = get_libname_in_dir (base_libname , base_libname_length , onedir , (* counter )++ );
51235153 if (libname_path != NULL )
@@ -5153,7 +5183,7 @@ char * get_libname(const char* base_libname)
51535183 size_t base_libname_length = strlen (base_libname );
51545184 struct stat st ;
51555185
5156- upsdebugx (3 , "%s('%s'): Entering method..." , __func__ , base_libname );
5186+ upsdebugx (3 , "%s('%s'): Entering method..." , __func__ , NUT_STRARG ( base_libname ) );
51575187
51585188 /* First, check for an exact hit by absolute/relative path
51595189 * if `base_libname` includes path separator character(s) */
0 commit comments