Clean-up: use sizeof instead of repeating string length constant
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 16 Mar 2022 15:52:42 +0000 (11:52 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 16 Mar 2022 15:52:42 +0000 (11:52 -0400)
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 <jeremie.galarneau@efficios.com>
Change-Id: Ia74f43d3871fdce60affbde068401b58c84b09ad

src/bin/lttng-sessiond/trace-kernel.cpp
src/common/channel.cpp
src/common/event-rule/user-tracepoint.cpp
src/common/event.cpp

index cbe4c3c89e072f1cda7185b4607a23d19228ddce..1a1620aaa78605b195756dc05c269ef021f5180e 100644 (file)
@@ -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;
index c0a0299a4d4a31302dbb08e977f2a01ea573b61b..ac2f915d8ec4208b41145edc53c91471ad774193 100644 (file)
@@ -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;
        }
 
index 5158eeaa072c992a57aa777e4ee8fe0552ea40b5..cc76d229dba4872eb92409e468fd7e054d1e890f 100644 (file)
@@ -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;
index ad3e23d4d8a15679977e7048ed6d6001ac0e3029..7946d320975a08cdf54bc9bbd0d9a370f4e37687 100644 (file)
@@ -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;
This page took 0.031087 seconds and 5 git commands to generate.