diff --git a/userspace/libsinsp/sinsp_filtercheck.cpp b/userspace/libsinsp/sinsp_filtercheck.cpp index 02d5d16a74..9b325590cc 100644 --- a/userspace/libsinsp/sinsp_filtercheck.cpp +++ b/userspace/libsinsp/sinsp_filtercheck.cpp @@ -23,6 +23,8 @@ limitations under the License. #include #include +#define STR_STORAGE_SIZE 1024 + #ifndef _GNU_SOURCE // // Fallback implementation of memmem @@ -861,10 +863,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, return NULL; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, prfmt, *(int8_t *)rawval); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); case PT_INT16: if(print_format == PF_OCT) { @@ -885,10 +888,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, return NULL; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, prfmt, *(int16_t *)rawval); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); case PT_INT32: if(print_format == PF_OCT) { @@ -909,10 +913,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, return NULL; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, prfmt, *(int32_t *)rawval); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); case PT_INT64: case PT_PID: case PT_ERRNO: @@ -939,10 +944,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, prfmt = (char*)"%" PRId64; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, prfmt, *(int64_t *)rawval); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); case PT_L4PROTO: // This can be resolved in the future case PT_UINT8: if(print_format == PF_OCT) @@ -964,10 +970,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, return NULL; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, prfmt, *(uint8_t *)rawval); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); case PT_PORT: // This can be resolved in the future case PT_UINT16: if(print_format == PF_OCT) @@ -989,10 +996,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, return NULL; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, prfmt, *(uint16_t *)rawval); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); case PT_UINT32: if(print_format == PF_OCT) { @@ -1013,10 +1021,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, return NULL; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, prfmt, *(uint32_t *)rawval); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); case PT_UINT64: case PT_RELTIME: case PT_ABSTIME: @@ -1042,11 +1051,12 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, ASSERT(false); return NULL; } - - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, prfmt, *(uint64_t *)rawval); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); case PT_CHARBUF: case PT_FSPATH: case PT_FSRELPATH: @@ -1060,10 +1070,11 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, } else { - auto copy_len = std::min(len, (uint32_t) sizeof(m_getpropertystr_storage)); - memcpy(m_getpropertystr_storage, rawval, copy_len); - m_getpropertystr_storage[copy_len] = 0; - return m_getpropertystr_storage; + auto copy_len = std::min(len, (uint32_t) STR_STORAGE_SIZE); + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + memcpy(m_getpropertystr_storage.data(), rawval, copy_len); + m_getpropertystr_storage.data()[copy_len] = 0; + return m_getpropertystr_storage.data(); } case PT_SOCKADDR: ASSERT(false); @@ -1081,14 +1092,15 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, return (char*)"false"; } case PT_IPV4ADDR: - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8, rawval[0], rawval[1], rawval[2], rawval[3]); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); case PT_IPV6ADDR: { char address[INET6_ADDRSTRLEN]; @@ -1098,9 +1110,10 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, strlcpy(address, "", INET6_ADDRSTRLEN); } - strlcpy(m_getpropertystr_storage, address, sizeof(m_getpropertystr_storage)); + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + strlcpy(m_getpropertystr_storage.data(), address, STR_STORAGE_SIZE); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); } case PT_IPADDR: if(len == sizeof(struct in_addr)) @@ -1117,15 +1130,17 @@ char* sinsp_filter_check::rawval_to_string(uint8_t* rawval, } case PT_DOUBLE: - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%.1lf", *(double*)rawval); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); case PT_IPNET: - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, ""); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); default: ASSERT(false); throw sinsp_exception("wrong param type " + std::to_string((long long) ptype)); @@ -1152,8 +1167,9 @@ char* sinsp_filter_check::tostring(sinsp_evt* evt) res += rawval_to_string(val.ptr, m_field->m_type, m_field->m_print_format, val.len); } res += ")"; - strlcpy(m_getpropertystr_storage, res.c_str(), sizeof(m_getpropertystr_storage)); - return m_getpropertystr_storage; + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + strlcpy(m_getpropertystr_storage.data(), res.c_str(), STR_STORAGE_SIZE); + return m_getpropertystr_storage.data(); } return rawval_to_string(m_extracted_values[0].ptr, m_field->m_type, m_field->m_print_format, m_extracted_values[0].len); } diff --git a/userspace/libsinsp/sinsp_filtercheck.h b/userspace/libsinsp/sinsp_filtercheck.h index 414d6bdc48..8b507b1cd8 100644 --- a/userspace/libsinsp/sinsp_filtercheck.h +++ b/userspace/libsinsp/sinsp_filtercheck.h @@ -268,7 +268,7 @@ class sinsp_filter_check inline uint8_t* filter_value_p(uint16_t i = 0) { return &m_val_storages[i][0]; } inline std::vector* filter_value(uint16_t i = 0) { return &m_val_storages[i]; } - char m_getpropertystr_storage[1024]; + std::vector m_getpropertystr_storage; std::vector> m_val_storages; std::vector m_vals; diff --git a/userspace/libsinsp/sinsp_filtercheck_reference.cpp b/userspace/libsinsp/sinsp_filtercheck_reference.cpp index afba882942..c4c5e84e71 100644 --- a/userspace/libsinsp/sinsp_filtercheck_reference.cpp +++ b/userspace/libsinsp/sinsp_filtercheck_reference.cpp @@ -20,6 +20,8 @@ limitations under the License. #include #include +#define STR_STORAGE_SIZE 1024 + using namespace std; sinsp_filter_check_reference::sinsp_filter_check_reference() @@ -70,51 +72,58 @@ char* sinsp_filter_check_reference::format_bytes(double val, uint32_t str_len, b if(val > (1024LL * 1024 * 1024 * 1024 * 1024)) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, pr_fmt, str_len - 1, (val) / (1024LL * 1024 * 1024 * 1024 * 1024), 'P'); } else if(val > (1024LL * 1024 * 1024 * 1024)) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, pr_fmt, str_len - 1, (val) / (1024LL * 1024 * 1024 * 1024), 'T'); } else if(val > (1024LL * 1024 * 1024)) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, pr_fmt, str_len - 1, (val) / (1024LL * 1024 * 1024), 'G'); } else if(val > (1024 * 1024)) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, pr_fmt, str_len - 1, (val) / (1024 * 1024), 'M'); } else if(val > 1024) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, pr_fmt, str_len - 1, (val) / (1024), 'K'); } else { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, pr_fmt, str_len, val, 0); } - uint32_t len = (uint32_t)strlen(m_getpropertystr_storage); + uint32_t len = (uint32_t)strlen(m_getpropertystr_storage.data()); if(len > str_len) { - memmove(m_getpropertystr_storage, - m_getpropertystr_storage + len - str_len, + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + memmove(m_getpropertystr_storage.data(), + m_getpropertystr_storage.data() + len - str_len, str_len + 1); // include trailing \0 } - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); } // @@ -128,62 +137,71 @@ char* sinsp_filter_check_reference::format_time(uint64_t val, uint32_t str_len) { if(val >= 3600 * ONE_SECOND_IN_NS) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%.2u:%.2u:%.2u", (unsigned int)(val / (3600 * ONE_SECOND_IN_NS)), (unsigned int)((val / (60 * ONE_SECOND_IN_NS)) % 60 ), (unsigned int)((val / ONE_SECOND_IN_NS) % 60)); } else if(val >= 60 * ONE_SECOND_IN_NS) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%u:%u", (unsigned int)(val / (60 * ONE_SECOND_IN_NS)), (unsigned int)((val / ONE_SECOND_IN_NS) % 60)); } else if(val >= ONE_SECOND_IN_NS) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%u.%02us", (unsigned int)(val / ONE_SECOND_IN_NS), (unsigned int)((val % ONE_SECOND_IN_NS) / 10000000)); } else if(val >= ONE_SECOND_IN_NS / 100) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%ums", (unsigned int)(val / (ONE_SECOND_IN_NS / 1000))); } else if(val >= ONE_SECOND_IN_NS / 1000) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%u.%02ums", (unsigned int)(val / (ONE_SECOND_IN_NS / 1000)), (unsigned int)((val % ONE_MILLISECOND_IN_NS) / 10000)); } else if(val >= ONE_SECOND_IN_NS / 100000) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%uus", (unsigned int)(val / (ONE_SECOND_IN_NS / 1000000))); } else if(val >= ONE_SECOND_IN_NS / 1000000) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%u.%02uus", (unsigned int)(val / (ONE_SECOND_IN_NS / 1000000)), (unsigned int)((val % ONE_MICROSECOND_IN_NS) / 10)); } else { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%uns", (unsigned int)val); } - uint32_t reslen = (uint32_t)strlen(m_getpropertystr_storage); + uint32_t reslen = (uint32_t)strlen(m_getpropertystr_storage.data()); if(reslen < str_len) { uint32_t padding_size = str_len - reslen; - memmove(m_getpropertystr_storage + padding_size, - m_getpropertystr_storage, + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + memmove(m_getpropertystr_storage.data() + padding_size, + m_getpropertystr_storage.data(), str_len + 1); for(uint32_t j = 0; j < padding_size; j++) @@ -192,7 +210,7 @@ char* sinsp_filter_check_reference::format_time(uint64_t val, uint32_t str_len) } } - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); } char* sinsp_filter_check_reference::print_double(uint8_t* rawval, uint32_t str_len) @@ -238,10 +256,11 @@ char* sinsp_filter_check_reference::print_double(uint8_t* rawval, uint32_t str_l if(m_print_format == PF_ID) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%*lf", str_len, val); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); } else { @@ -293,10 +312,11 @@ char* sinsp_filter_check_reference::print_int(uint8_t* rawval, uint32_t str_len) if(m_print_format == PF_ID) { - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%*" PRId64, str_len, val); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); } else { @@ -355,10 +375,11 @@ char* sinsp_filter_check_reference::tostring_nice(sinsp_evt* evt, dval /= m_cnt; } - snprintf(m_getpropertystr_storage, - sizeof(m_getpropertystr_storage), + m_getpropertystr_storage.resize(STR_STORAGE_SIZE); + snprintf(m_getpropertystr_storage.data(), + STR_STORAGE_SIZE, "%*.2lf", str_len, dval); - return m_getpropertystr_storage; + return m_getpropertystr_storage.data(); } else {