diff --git a/userspace/libsinsp/chisel.cpp b/userspace/libsinsp/chisel.cpp index 1d83cf5133..28b854a859 100644 --- a/userspace/libsinsp/chisel.cpp +++ b/userspace/libsinsp/chisel.cpp @@ -1106,7 +1106,7 @@ void sinsp_chisel::get_chisel_list(vector* chisel_descs) tinydir_dir dir = {}; - tinydir_open(&dir, it->m_dir); + tinydir_open(&dir, it->m_dir.c_str()); while(dir.has_next) { diff --git a/userspace/libsinsp/chisel.h b/userspace/libsinsp/chisel.h index 77ef214767..70a2f55028 100644 --- a/userspace/libsinsp/chisel.h +++ b/userspace/libsinsp/chisel.h @@ -38,7 +38,7 @@ typedef struct lua_State lua_State; typedef struct chiseldir_info { bool m_need_to_resolve; - char m_dir[1024]; + std::string m_dir; }chiseldir_info; class chiselarg_desc diff --git a/userspace/libsinsp/sinsp.cpp b/userspace/libsinsp/sinsp.cpp index a7944322d3..a9e386b586 100644 --- a/userspace/libsinsp/sinsp.cpp +++ b/userspace/libsinsp/sinsp.cpp @@ -1948,7 +1948,7 @@ void sinsp::add_chisel_dir(string dirname, bool front_add) chiseldir_info ncdi; - strcpy(ncdi.m_dir, dirname.c_str()); + ncdi.m_dir = std::move(dirname); ncdi.m_need_to_resolve = false; if(front_add) diff --git a/userspace/libsinsp/utils.cpp b/userspace/libsinsp/utils.cpp index a50e557847..bb090371c1 100644 --- a/userspace/libsinsp/utils.cpp +++ b/userspace/libsinsp/utils.cpp @@ -31,6 +31,7 @@ limitations under the License. #include #include #include +#include #else #pragma comment(lib, "Ws2_32.lib") #include @@ -71,19 +72,29 @@ const chiseldir_info g_chisel_dirs_array[] = #endif #ifndef _WIN32 -char* realpath_ex(const char *path, char *buff) +static std::string realpath_ex(const std::string& path) { char *home; + char* resolved; - if(*path=='~' && (home = getenv("HOME"))) + if(!path.empty() && path[0]=='~' && (home = getenv("HOME"))) { - char s[PATH_MAX]; - return realpath(strncat(strncpy(s, home, sizeof(s)), path+1, sizeof(path)+1), buff); - } + std::string expanded_home = home; + expanded_home += path.c_str()+1; + resolved = realpath(expanded_home.c_str(), nullptr); + } else { - return realpath(path, buff); + resolved = realpath(path.c_str(), nullptr); + } + + if (!resolved) + { + return ""; } + std::string ret = resolved; + free(resolved); + return resolved; } #endif @@ -133,20 +144,17 @@ sinsp_initializer::sinsp_initializer() if(g_chisel_dirs_array[j].m_need_to_resolve) { #ifndef _WIN32 - char resolved_path[PATH_MAX]; - - if(realpath_ex(g_chisel_dirs_array[j].m_dir, resolved_path) != NULL) + std::string resolved_path = realpath_ex(g_chisel_dirs_array[j].m_dir); + if(!resolved_path.empty()) { - string resolved_path_str(resolved_path); - - if(resolved_path_str[resolved_path_str.size() -1] != '/') + if(resolved_path[resolved_path.size() - 1] != '/') { - resolved_path_str += "/"; + resolved_path += '/'; } chiseldir_info cdi; cdi.m_need_to_resolve = false; - sprintf(cdi.m_dir, "%s", resolved_path_str.c_str()); + cdi.m_dir = std::move(resolved_path); g_chisel_dirs->push_back(cdi); } #else