Skip to content

Commit

Permalink
app/procinfo: use strlcpy for copying string
Browse files Browse the repository at this point in the history
[ upstream commit 7edbf7d ]

Replaced strncpy and strcpy with strlcpy.
Also replaced snprintf with strlcpy where applicable.
Using strlcpy is safe practice when copying strings, as it will include
a null terminator.

Fixes: 2deb6b5 ("app/procinfo: add collectd format and host id")
Fixes: 8a37f37 ("app/procinfo: add --show-port")

Reported-by: Reshma Pattan <reshma.pattan@intel.com>
Signed-off-by: Ciara Power <ciara.power@intel.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
  • Loading branch information
ciarapow authored and kevintraynor committed Dec 10, 2019
1 parent 035fed4 commit bc12c8f
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions app/proc-info/main.c
Expand Up @@ -167,7 +167,7 @@ proc_info_preparse_args(int argc, char **argv)
int err = gethostname(host_id, MAX_LONG_OPT_SZ-1);

if (err)
strcpy(host_id, "unknown");
strlcpy(host_id, "unknown", sizeof(host_id));
}

return 0;
Expand Down Expand Up @@ -334,50 +334,50 @@ static void collectd_resolve_cnt_type(char *cnt_type, size_t cnt_type_len,
if ((type_end != NULL) &&
(strncmp(cnt_name, "rx_", strlen("rx_")) == 0)) {
if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
strncpy(cnt_type, "if_rx_errors", cnt_type_len);
strlcpy(cnt_type, "if_rx_errors", cnt_type_len);
else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0)
strncpy(cnt_type, "if_rx_dropped", cnt_type_len);
strlcpy(cnt_type, "if_rx_dropped", cnt_type_len);
else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0)
strncpy(cnt_type, "if_rx_octets", cnt_type_len);
strlcpy(cnt_type, "if_rx_octets", cnt_type_len);
else if (strncmp(type_end, "_packets", strlen("_packets")) == 0)
strncpy(cnt_type, "if_rx_packets", cnt_type_len);
strlcpy(cnt_type, "if_rx_packets", cnt_type_len);
else if (strncmp(type_end, "_placement",
strlen("_placement")) == 0)
strncpy(cnt_type, "if_rx_errors", cnt_type_len);
strlcpy(cnt_type, "if_rx_errors", cnt_type_len);
else if (strncmp(type_end, "_buff", strlen("_buff")) == 0)
strncpy(cnt_type, "if_rx_errors", cnt_type_len);
strlcpy(cnt_type, "if_rx_errors", cnt_type_len);
else
/* Does not fit obvious type: use a more generic one */
strncpy(cnt_type, "derive", cnt_type_len);
strlcpy(cnt_type, "derive", cnt_type_len);
} else if ((type_end != NULL) &&
(strncmp(cnt_name, "tx_", strlen("tx_"))) == 0) {
if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
strncpy(cnt_type, "if_tx_errors", cnt_type_len);
strlcpy(cnt_type, "if_tx_errors", cnt_type_len);
else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0)
strncpy(cnt_type, "if_tx_dropped", cnt_type_len);
strlcpy(cnt_type, "if_tx_dropped", cnt_type_len);
else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0)
strncpy(cnt_type, "if_tx_octets", cnt_type_len);
strlcpy(cnt_type, "if_tx_octets", cnt_type_len);
else if (strncmp(type_end, "_packets", strlen("_packets")) == 0)
strncpy(cnt_type, "if_tx_packets", cnt_type_len);
strlcpy(cnt_type, "if_tx_packets", cnt_type_len);
else
/* Does not fit obvious type: use a more generic one */
strncpy(cnt_type, "derive", cnt_type_len);
strlcpy(cnt_type, "derive", cnt_type_len);
} else if ((type_end != NULL) &&
(strncmp(cnt_name, "flow_", strlen("flow_"))) == 0) {
if (strncmp(type_end, "_filters", strlen("_filters")) == 0)
strncpy(cnt_type, "operations", cnt_type_len);
strlcpy(cnt_type, "operations", cnt_type_len);
else if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
strncpy(cnt_type, "errors", cnt_type_len);
strlcpy(cnt_type, "errors", cnt_type_len);
else if (strncmp(type_end, "_filters", strlen("_filters")) == 0)
strncpy(cnt_type, "filter_result", cnt_type_len);
strlcpy(cnt_type, "filter_result", cnt_type_len);
} else if ((type_end != NULL) &&
(strncmp(cnt_name, "mac_", strlen("mac_"))) == 0) {
if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
strncpy(cnt_type, "errors", cnt_type_len);
strlcpy(cnt_type, "errors", cnt_type_len);
} else {
/* Does not fit obvious type, or strrchr error: */
/* use a more generic type */
strncpy(cnt_type, "derive", cnt_type_len);
strlcpy(cnt_type, "derive", cnt_type_len);
}
}

Expand Down

0 comments on commit bc12c8f

Please sign in to comment.