From 7bdd8981f7a62e4a5196254b9542b773e37d97cc Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Mon, 8 Dec 2014 11:18:39 +0200 Subject: [PATCH] Check os_snprintf() result more consistently - automatic 2 This converts os_snprintf() result validation cases to use os_snprintf_error() where the comparison was 'res > size' instead of 'res >= size'. These changes were done automatically with spatch using the following semantic patch: @@ identifier E1; expression E2,E3,E4,E5,E6; statement S1; @@ ( E1 = os_snprintf(E2, E3, ...); | int E1 = os_snprintf(E2, E3, ...); | if (E5) E1 = os_snprintf(E2, E3, ...); else E1 = os_snprintf(E2, E3, ...); | if (E5) E1 = os_snprintf(E2, E3, ...); else if (E6) E1 = os_snprintf(E2, E3, ...); else E1 = 0; | if (E5) { ... E1 = os_snprintf(E2, E3, ...); } else { ... return -1; } | if (E5) { ... E1 = os_snprintf(E2, E3, ...); } else if (E6) { ... E1 = os_snprintf(E2, E3, ...); } else { ... return -1; } | if (E5) { ... E1 = os_snprintf(E2, E3, ...); } else { ... E1 = os_snprintf(E2, E3, ...); } ) ? os_free(E4); - if (E1 < 0 || \( E1 > E3 \| (size_t) E1 > E3 \| E1 > (int) E3 \)) + if (os_snprintf_error(E3, E1)) ( S1 | { ... } ) Signed-off-by: Jouni Malinen --- src/utils/common.c | 2 +- wpa_supplicant/ctrl_iface.c | 10 +++++----- wpa_supplicant/dbus/dbus_dict_helpers.c | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/utils/common.c b/src/utils/common.c index 1517a487c..422b476c3 100644 --- a/src/utils/common.c +++ b/src/utils/common.c @@ -755,7 +755,7 @@ char * freq_range_list_str(const struct wpa_freq_range_list *list) res = os_snprintf(pos, end - pos, "%s%u-%u", i == 0 ? "" : ",", range->min, range->max); - if (res < 0 || res > end - pos) { + if (os_snprintf_error(end - pos, res)) { os_free(buf); return NULL; } diff --git a/wpa_supplicant/ctrl_iface.c b/wpa_supplicant/ctrl_iface.c index e95277526..c96052b66 100644 --- a/wpa_supplicant/ctrl_iface.c +++ b/wpa_supplicant/ctrl_iface.c @@ -642,7 +642,7 @@ static int ctrl_iface_get_capability_tdls( (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ? "EXTERNAL" : "INTERNAL") : "UNSUPPORTED"); - if (ret < 0 || (size_t) ret > buflen) + if (os_snprintf_error(buflen, ret)) return -1; return ret; } @@ -5780,14 +5780,14 @@ static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf, "NOISE=%d\nFREQUENCY=%u\n", si.current_signal, si.current_txrate / 1000, si.current_noise, si.frequency); - if (ret < 0 || ret > end - pos) + if (os_snprintf_error(end - pos, ret)) return -1; pos += ret; if (si.chanwidth != CHAN_WIDTH_UNKNOWN) { ret = os_snprintf(pos, end - pos, "WIDTH=%s\n", channel_width_to_string(si.chanwidth)); - if (ret < 0 || ret > end - pos) + if (os_snprintf_error(end - pos, ret)) return -1; pos += ret; } @@ -5796,7 +5796,7 @@ static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf, ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n", si.center_frq1, si.center_frq2); - if (ret < 0 || ret > end - pos) + if (os_snprintf_error(end - pos, ret)) return -1; pos += ret; } @@ -5825,7 +5825,7 @@ static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf, ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n", sta.tx_packets, sta.tx_retry_failed, sta.rx_packets); - if (ret < 0 || (size_t) ret > buflen) + if (os_snprintf_error(buflen, ret)) return -1; return ret; } diff --git a/wpa_supplicant/dbus/dbus_dict_helpers.c b/wpa_supplicant/dbus/dbus_dict_helpers.c index 949ce7c91..034b751c4 100644 --- a/wpa_supplicant/dbus/dbus_dict_helpers.c +++ b/wpa_supplicant/dbus/dbus_dict_helpers.c @@ -465,7 +465,7 @@ dbus_bool_t wpa_dbus_dict_begin_array(DBusMessageIter *iter_dict, err = os_snprintf(array_type, sizeof(array_type), DBUS_TYPE_ARRAY_AS_STRING "%s", type); - if (err < 0 || err > (int) sizeof(array_type)) + if (os_snprintf_error(sizeof(array_type), err)) return FALSE; if (!iter_dict || !iter_dict_entry || !iter_dict_val || !iter_array)