Skip to content

Commit

Permalink
cleanup(sinsp): handle path too long in a better way
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Terzolo <andreaterzolo3@gmail.com>
  • Loading branch information
Andreagit97 authored and poiana committed Nov 25, 2024
1 parent 365f1aa commit 55ff79f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion test/libsinsp_e2e/sys_call_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2180,7 +2180,7 @@ TEST_F(sys_call_test, thread_lookup_live) {
TEST_F(sys_call_test, fd_name_max_path) {
int callnum = 0;
std::string pathname("/");
// Using only 1022 chars otherwise the path will be "/PATH_TOO_LONG".
// Using only 1022 chars otherwise the path will be "/DIR_TOO_LONG/FILENAME_TOO_LONG".
pathname.insert(1, 1021, 'A');

event_filter_t filter = [&](sinsp_evt* evt) {
Expand Down
8 changes: 5 additions & 3 deletions userspace/libsinsp/test/events_file.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ TEST_F(sinsp_with_test_input, path_too_long) {
(uint32_t)0,
(uint32_t)5,
(uint64_t)123);
ASSERT_EQ(get_field_as_string(evt, "fd.name"), "/PATH_TOO_LONG");
ASSERT_EQ(get_field_as_string(evt, "fd.name"), "/DIR_TOO_LONG/FILENAME_TOO_LONG");
ASSERT_EQ(get_field_as_string(evt, "fd.directory"), "/DIR_TOO_LONG");
ASSERT_EQ(get_field_as_string(evt, "fd.filename"), "FILENAME_TOO_LONG");

fd = 4;
add_event_advance_ts(increasing_ts(), 1, PPME_SYSCALL_OPEN_BY_HANDLE_AT_E, 0);
Expand All @@ -220,8 +222,8 @@ TEST_F(sinsp_with_test_input, path_too_long) {
PPM_O_RDWR,
long_path.c_str());

ASSERT_EQ(get_field_as_string(evt, "fd.name"), "/PATH_TOO_LONG");
ASSERT_EQ(get_field_as_string(evt, "evt.abspath"), "/PATH_TOO_LONG");
ASSERT_EQ(get_field_as_string(evt, "fd.name"), "/DIR_TOO_LONG/FILENAME_TOO_LONG");
ASSERT_EQ(get_field_as_string(evt, "evt.abspath"), "/DIR_TOO_LONG/FILENAME_TOO_LONG");
}

TEST_F(sinsp_with_test_input, creates_fd_generic) {
Expand Down
6 changes: 6 additions & 0 deletions userspace/libsinsp/test/sinsp_utils.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ TEST(sinsp_utils_test, concatenate_paths) {
res = sinsp_utils::concatenate_paths(path1, path2);
EXPECT_EQ("/app", res);

// This path is too long so we should receive our predefined string.
path1 = std::string(1500, 'C');
path2 = "dir/term";
res = sinsp_utils::concatenate_paths(path1, path2);
EXPECT_EQ("/DIR_TOO_LONG/FILENAME_TOO_LONG", res);

/* No unicode support
path1 = "/root/";
path2 = "../😉";
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ static inline bool concatenate_paths_(char* target,
const char* path2,
uint32_t len2) {
if(targetlen < (len1 + len2 + 1)) {
strlcpy(target, "/PATH_TOO_LONG", targetlen);
strlcpy(target, "/DIR_TOO_LONG/FILENAME_TOO_LONG", targetlen);
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion userspace/libsinsp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ class sinsp_utils {
// Concatenate posix-style path1 and path2 up to max_len in size, normalizing the result.
// path1 MUST be '/' terminated and is not sanitized.
// If path2 is absolute, the result will be equivalent to path2.
// If the result would be too long, the output will contain the string "/PATH_TOO_LONG" instead.
// If the result would be too long, the output will contain the string
// "/DIR_TOO_LONG/FILENAME_TOO_LONG" instead.
//
static std::string concatenate_paths(std::string_view path1, std::string_view path2);

Expand Down

0 comments on commit 55ff79f

Please sign in to comment.