Check snprintf() return values to silence compiler warnings
Some compilers warn about potential buffer truncation if the snprintf()
return value is not checked.
Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
diff --git a/sta.c b/sta.c
index ec257c0..e4d26d2 100644
--- a/sta.c
+++ b/sta.c
@@ -322,6 +322,7 @@
char buf[128], fname[128];
size_t towrite, written;
FILE *f;
+ int res;
if (length > WIL_WMI_MAX_PAYLOAD) {
sigma_dut_print(dut, DUT_MSG_ERROR,
@@ -340,7 +341,9 @@
return -1;
}
- snprintf(fname, sizeof(fname), "%s/wmi_send", buf);
+ res = snprintf(fname, sizeof(fname), "%s/wmi_send", buf);
+ if (res < 0 || res >= sizeof(fname))
+ return -1;
f = fopen(fname, "wb");
if (!f) {
sigma_dut_print(dut, DUT_MSG_ERROR,
@@ -368,7 +371,7 @@
FILE *f;
regex_t re;
regmatch_t m[2];
- int rc, ret = -1;
+ int rc, ret = -1, res;
if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
sigma_dut_print(dut, DUT_MSG_ERROR,
@@ -376,7 +379,9 @@
return -1;
}
- snprintf(fname, sizeof(fname), "%s/stations", buf);
+ res = snprintf(fname, sizeof(fname), "%s/stations", buf);
+ if (res < 0 || res >= sizeof(fname))
+ return -1;
f = fopen(fname, "r");
if (!f) {
sigma_dut_print(dut, DUT_MSG_ERROR,
@@ -593,7 +598,7 @@
{
char buf[128], fname[128];
FILE *f;
- int res = 0;
+ int res = 0, r;
size_t written;
if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
@@ -601,7 +606,9 @@
"failed to get wil6210 debugfs dir");
return -1;
}
- snprintf(fname, sizeof(fname), "%s/tx_mgmt", buf);
+ r = snprintf(fname, sizeof(fname), "%s/tx_mgmt", buf);
+ if (r < 0 || r >= sizeof(fname))
+ return -1;
if (wil6210_remain_on_channel(dut, freq)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
@@ -5190,7 +5197,8 @@
{
const char *val;
int ampdu = -1, addbareject = -1;
- char buf[30];
+ char buf[128];
+ int res;
val = get_param(cmd, "40_INTOLERANT");
if (val) {
@@ -5426,9 +5434,10 @@
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to set RTS_FORCE 64");
}
- snprintf(buf, sizeof(buf),
- "wifitool %s beeliner_fw_test 100 1", intf);
- if (system(buf) != 0) {
+ res = snprintf(buf, sizeof(buf),
+ "wifitool %s beeliner_fw_test 100 1",
+ intf);
+ if (res < 0 || res >= sizeof(buf) || system(buf) != 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"wifitool beeliner_fw_test 100 1 failed");
}
@@ -5745,6 +5754,7 @@
{
char buf[128], fname[128];
FILE *f;
+ int res;
if (wil6210_get_debugfs_dir(dut, buf, sizeof(buf))) {
sigma_dut_print(dut, DUT_MSG_ERROR,
@@ -5752,7 +5762,9 @@
return -1;
}
- snprintf(fname, sizeof(fname), "%s/abft_len", buf);
+ res = snprintf(fname, sizeof(fname), "%s/abft_len", buf);
+ if (res < 0 || res >= sizeof(fname))
+ return -1;
f = fopen(fname, "w");
if (!f) {
sigma_dut_print(dut, DUT_MSG_ERROR,
@@ -8693,7 +8705,7 @@
FILE *f;
regex_t re;
regmatch_t m[2];
- int rc, ret = -1, vring_id, found;
+ int rc, ret = -1, vring_id, found, res;
if (wil6210_get_debugfs_dir(dut, dir, sizeof(dir))) {
sigma_dut_print(dut, DUT_MSG_ERROR,
@@ -8701,12 +8713,16 @@
return -1;
}
- snprintf(buf, sizeof(buf), "%s/vrings", dir);
+ res = snprintf(buf, sizeof(buf), "%s/vrings", dir);
+ if (res < 0 || res >= sizeof(buf))
+ return -1;
f = fopen(buf, "r");
if (!f) {
sigma_dut_print(dut, DUT_MSG_ERROR, "failed to open: %s", buf);
/* newer wil6210 driver renamed file to "rings" */
- snprintf(buf, sizeof(buf), "%s/rings", dir);
+ res = snprintf(buf, sizeof(buf), "%s/rings", dir);
+ if (res < 0 || res >= sizeof(buf))
+ return -1;
f = fopen(buf, "r");
if (!f) {
sigma_dut_print(dut, DUT_MSG_ERROR,
@@ -8755,7 +8771,9 @@
/* send the addba command */
fclose(f);
- snprintf(buf, sizeof(buf), "%s/back", dir);
+ res = snprintf(buf, sizeof(buf), "%s/back", dir);
+ if (res < 0 || res >= sizeof(buf))
+ return -1;
f = fopen(buf, "w");
if (!f) {
sigma_dut_print(dut, DUT_MSG_ERROR,
@@ -11780,7 +11798,7 @@
const char *val = get_param(cmd, "Ignore_blacklist");
const char *band = get_param(cmd, "Band");
struct wpa_ctrl *ctrl;
- int res;
+ int res, r;
char bssid[20], ssid[40], resp[100], buf[100], blacklisted[100];
int tries = 0;
int ignore_blacklist = 0;
@@ -11847,9 +11865,9 @@
*end = '\0';
sigma_dut_print(dut, DUT_MSG_DEBUG, "Try to connect to a blacklisted network: %s",
blacklisted);
- snprintf(buf, sizeof(buf), "INTERWORKING_CONNECT %s",
- blacklisted);
- if (wpa_command(intf, buf)) {
+ r = snprintf(buf, sizeof(buf), "INTERWORKING_CONNECT %s",
+ blacklisted);
+ if (r < 0 || r >= sizeof(buf) || wpa_command(intf, buf)) {
send_resp(dut, conn, SIGMA_ERROR, "errorCode,Failed to start Interworking connection to blacklisted network");
wpa_ctrl_detach(ctrl);
wpa_ctrl_close(ctrl);