Skip to content

Commit

Permalink
Check snprintf() result to confirm sufficient buffer size
Browse files Browse the repository at this point in the history
This avoids using truncated commands with unexpected input and compiler
warnings.

Signed-off-by: Jouni Malinen <quic_jouni@quicinc.com>
  • Loading branch information
Jouni Malinen committed Dec 17, 2024
1 parent 0d947e1 commit 343f9e0
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions ap.c
Original file line number Diff line number Diff line change
Expand Up @@ -3378,16 +3378,23 @@ static void owrt_ap_set_vap(struct sigma_dut *dut, int id, const char *key,
const char *val)
{
char buf[256];
int res;

if (val == NULL) {
snprintf(buf, sizeof(buf),
"uci delete wireless.@wifi-iface[%d].%s", id, key);
res = snprintf(buf, sizeof(buf),
"uci delete wireless.@wifi-iface[%d].%s",
id, key);
if (res < 0 || res >= sizeof(buf))
return;
run_system(dut, buf);
return;
}

snprintf(buf, sizeof(buf), "uci set wireless.@wifi-iface[%d].%s=%s",
id, key, val);
res = snprintf(buf, sizeof(buf),
"uci set wireless.@wifi-iface[%d].%s=%s",
id, key, val);
if (res < 0 || res >= sizeof(buf))
return;
run_system(dut, buf);
}

Expand All @@ -3396,17 +3403,23 @@ static void owrt_ap_set_list_vap(struct sigma_dut *dut, int id,
const char *key, const char *val)
{
char buf[1024];
int res;

if (val == NULL) {
snprintf(buf, sizeof(buf),
"uci del_list wireless.@wifi-iface[%d].%s", id, key);
res = snprintf(buf, sizeof(buf),
"uci del_list wireless.@wifi-iface[%d].%s",
id, key);
if (res < 0 || res >= sizeof(buf))
return;
run_system(dut, buf);
return;
}

snprintf(buf, sizeof(buf),
"uci add_list wireless.@wifi-iface[%d].%s=%s",
id, key, val);
res = snprintf(buf, sizeof(buf),
"uci add_list wireless.@wifi-iface[%d].%s=%s",
id, key, val);
if (res < 0 || res >= sizeof(buf))
return;
run_system(dut, buf);
}

Expand Down

0 comments on commit 343f9e0

Please sign in to comment.