@@ -31,6 +31,7 @@ limitations under the License.
3131#include < strings.h>
3232#include < sys/ioctl.h>
3333#include < fnmatch.h>
34+ #include < string>
3435#else
3536#pragma comment(lib, "Ws2_32.lib")
3637#include < WinSock2.h>
@@ -71,19 +72,29 @@ const chiseldir_info g_chisel_dirs_array[] =
7172#endif
7273
7374#ifndef _WIN32
74- char * realpath_ex (const char * path, char *buff )
75+ static std::string realpath_ex (const std::string& path)
7576{
7677 char *home;
78+ char * resolved;
7779
78- if (* path==' ~' && (home = getenv (" HOME" )))
80+ if (! path. empty () && path[ 0 ] ==' ~' && (home = getenv (" HOME" )))
7981 {
80- char s[PATH_MAX];
81- return realpath (strncat (strncpy (s, home, sizeof (s)), path+1 , sizeof (path)+1 ), buff);
82- }
82+ std::string expanded_home = home;
83+ expanded_home += path.c_str ()+1 ;
84+ resolved = realpath (expanded_home.c_str (), nullptr );
85+ }
8386 else
8487 {
85- return realpath (path, buff);
88+ resolved = realpath (path.c_str (), nullptr );
89+ }
90+
91+ if (!resolved)
92+ {
93+ return " " ;
8694 }
95+ std::string ret = resolved;
96+ free (resolved);
97+ return resolved;
8798}
8899#endif
89100
@@ -133,20 +144,17 @@ sinsp_initializer::sinsp_initializer()
133144 if (g_chisel_dirs_array[j].m_need_to_resolve )
134145 {
135146#ifndef _WIN32
136- char resolved_path[PATH_MAX];
137-
138- if (realpath_ex (g_chisel_dirs_array[j].m_dir , resolved_path) != NULL )
147+ std::string resolved_path = realpath_ex (g_chisel_dirs_array[j].m_dir );
148+ if (!resolved_path.empty ())
139149 {
140- string resolved_path_str (resolved_path);
141-
142- if (resolved_path_str[resolved_path_str.size () -1 ] != ' /' )
150+ if (resolved_path[resolved_path.size () - 1 ] != ' /' )
143151 {
144- resolved_path_str += " / " ;
152+ resolved_path += ' / ' ;
145153 }
146154
147155 chiseldir_info cdi;
148156 cdi.m_need_to_resolve = false ;
149- sprintf ( cdi.m_dir , " %s " , resolved_path_str. c_str () );
157+ cdi.m_dir = std::move (resolved_path );
150158 g_chisel_dirs->push_back (cdi);
151159 }
152160#else
0 commit comments