From 2d6df81a3d233d7aaf34b874087555e72b105877 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Wed, 16 Mar 2022 11:52:42 -0400 Subject: [PATCH] Clean-up: use sizeof instead of repeating string length constant MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Looking into a number of coverity reports (false positives), I identified a number of sites which use the maximal symbol length constant when the actual size of an array can be used. This will prevent mismatches in the future should the array sizes change. Signed-off-by: Jérémie Galarneau Change-Id: Ia74f43d3871fdce60affbde068401b58c84b09ad --- src/bin/lttng-sessiond/trace-kernel.cpp | 3 +- src/common/channel.cpp | 12 ++++---- src/common/event-rule/user-tracepoint.cpp | 2 +- src/common/event.cpp | 37 ++++++++++++----------- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/bin/lttng-sessiond/trace-kernel.cpp b/src/bin/lttng-sessiond/trace-kernel.cpp index cbe4c3c89..1a1620aaa 100644 --- a/src/bin/lttng-sessiond/trace-kernel.cpp +++ b/src/bin/lttng-sessiond/trace-kernel.cpp @@ -594,7 +594,8 @@ enum lttng_error_code trace_kernel_init_event_notifier_from_event_rule( if (symbol_name) { strncpy_ret = lttng_strncpy( kernel_event_notifier->event.u.kprobe.symbol_name, - symbol_name, LTTNG_KERNEL_ABI_SYM_NAME_LEN); + symbol_name, + sizeof(kernel_event_notifier->event.u.kprobe.symbol_name)); if (strncpy_ret) { ret_code = LTTNG_ERR_INVALID; diff --git a/src/common/channel.cpp b/src/common/channel.cpp index c0a0299a4..ac2f915d8 100644 --- a/src/common/channel.cpp +++ b/src/common/channel.cpp @@ -146,11 +146,6 @@ ssize_t lttng_channel_create_from_buffer(const struct lttng_buffer_view *view, lttng_buffer_view_from_view(view, offset, channel_comm->name_len); - if (channel_comm->name_len > LTTNG_SYMBOL_NAME_LEN - 1) { - ret = -1; - goto end; - } - name = name_view.data; if (!lttng_buffer_view_contains_string( &name_view, name, channel_comm->name_len)) { @@ -158,7 +153,12 @@ ssize_t lttng_channel_create_from_buffer(const struct lttng_buffer_view *view, goto end; } - strcpy(local_channel->name, name); + ret = lttng_strncpy(local_channel->name, name, + sizeof(local_channel->name)); + if (ret) { + goto end; + } + offset += channel_comm->name_len; } diff --git a/src/common/event-rule/user-tracepoint.cpp b/src/common/event-rule/user-tracepoint.cpp index 5158eeaa0..cc76d229d 100644 --- a/src/common/event-rule/user-tracepoint.cpp +++ b/src/common/event-rule/user-tracepoint.cpp @@ -381,7 +381,7 @@ lttng_event_rule_user_tracepoint_generate_exclusions( LTTNG_ASSERT(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK); copy_ret = lttng_strncpy(exclusions->names[i], exclusion_str, - LTTNG_SYMBOL_NAME_LEN); + sizeof(exclusions->names[i])); if (copy_ret) { free(exclusions); exclusions = NULL; diff --git a/src/common/event.cpp b/src/common/event.cpp index ad3e23d4d..7946d3209 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -79,8 +79,9 @@ static int lttng_event_probe_attr_serialize( size_t symbol_name_len; struct lttng_event_probe_attr_comm comm = {}; - symbol_name_len = lttng_strnlen(probe->symbol_name, LTTNG_SYMBOL_NAME_LEN); - if (symbol_name_len == LTTNG_SYMBOL_NAME_LEN) { + symbol_name_len = lttng_strnlen( + probe->symbol_name, sizeof(probe->symbol_name)); + if (symbol_name_len == sizeof(probe->symbol_name)) { /* Not null-termintated. */ ret = -1; goto end; @@ -114,8 +115,9 @@ static int lttng_event_function_attr_serialize( size_t symbol_name_len; struct lttng_event_function_attr_comm comm = { 0 }; - symbol_name_len = lttng_strnlen(function->symbol_name, LTTNG_SYMBOL_NAME_LEN); - if (symbol_name_len == LTTNG_SYMBOL_NAME_LEN) { + symbol_name_len = lttng_strnlen( + function->symbol_name, sizeof(function->symbol_name)); + if (symbol_name_len == sizeof(function->symbol_name)) { /* Not null-termintated. */ ret = -1; goto end; @@ -187,7 +189,7 @@ static ssize_t lttng_event_probe_attr_create_from_payload( } ret = lttng_strncpy(local_attr->symbol_name, name, - LTTNG_SYMBOL_NAME_LEN); + sizeof(local_attr->symbol_name)); if (ret) { ret = -1; goto end; @@ -249,7 +251,7 @@ static ssize_t lttng_event_function_attr_create_from_payload( } ret = lttng_strncpy(local_attr->symbol_name, name, - LTTNG_SYMBOL_NAME_LEN); + sizeof(local_attr->symbol_name)); if (ret) { ret = -1; goto end; @@ -317,8 +319,8 @@ static ssize_t lttng_event_exclusions_create_from_payload( goto end; } - ret = lttng_strncpy(local_exclusions->names[i], - string, LTTNG_SYMBOL_NAME_LEN); + ret = lttng_strncpy(local_exclusions->names[i], string, + sizeof(local_exclusions->names[i])); if (ret) { ret = -1; goto end; @@ -406,8 +408,8 @@ ssize_t lttng_event_create_from_payload(struct lttng_payload_view *view, goto end; } - ret = lttng_strncpy( - local_event->name, name, LTTNG_SYMBOL_NAME_LEN); + ret = lttng_strncpy(local_event->name, name, + sizeof(local_event->name)); if (ret) { ret = -1; goto end; @@ -710,8 +712,8 @@ int lttng_event_serialize(const struct lttng_event *event, /* Save the header location for later in-place header update. */ header_offset = payload->buffer.size; - name_len = lttng_strnlen(event->name, LTTNG_SYMBOL_NAME_LEN); - if (name_len == LTTNG_SYMBOL_NAME_LEN) { + name_len = lttng_strnlen(event->name, sizeof(event->name)); + if (name_len == sizeof(event->name)) { /* Event name is not NULL-terminated. */ ret = -1; goto end; @@ -1203,9 +1205,9 @@ static int lttng_event_context_perf_counter_serialize( comm.config = context->config; comm.type = context->type; - comm.name_len = lttng_strnlen(context->name, LTTNG_SYMBOL_NAME_LEN); + comm.name_len = lttng_strnlen(context->name, sizeof(context->name)); - if (comm.name_len == LTTNG_SYMBOL_NAME_LEN) { + if (comm.name_len == sizeof(context->name)) { ret = -1; goto end; } @@ -1394,7 +1396,8 @@ ssize_t lttng_event_field_create_from_payload( assert(name); assert(event); - if (lttng_strncpy(local_event_field->field_name, name , LTTNG_SYMBOL_NAME_LEN)) { + if (lttng_strncpy(local_event_field->field_name, name, + sizeof(local_event_field->field_name))) { ret = -1; goto end; } @@ -1426,8 +1429,8 @@ int lttng_event_field_serialize(const struct lttng_event_field *field, /* Save the header location for later in-place header update. */ header_offset = payload->buffer.size; - name_len = strnlen(field->field_name, LTTNG_SYMBOL_NAME_LEN); - if (name_len == LTTNG_SYMBOL_NAME_LEN) { + name_len = strnlen(field->field_name, sizeof(field->field_name)); + if (name_len == sizeof(field->field_name)) { /* Event name is not NULL-terminated. */ ret = -1; goto end; -- 2.34.1