Check os_snprintf() result more consistently
authorJouni Malinen <j@w1.fi>
Sat, 6 Dec 2014 19:40:31 +0000 (21:40 +0200)
committerJouni Malinen <j@w1.fi>
Mon, 8 Dec 2014 09:42:07 +0000 (11:42 +0200)
While these are using practically large enoungh buffer sizes, it is
better to be more consistent with checking os_snprintf() return value.

Signed-off-by: Jouni Malinen <j@w1.fi>
wpa_supplicant/config.c
wpa_supplicant/ctrl_iface.c
wpa_supplicant/p2p_supplicant.c

index 786d268..bd80b29 100644 (file)
@@ -358,9 +358,15 @@ static char * wpa_config_write_psk(const struct parse_data *data,
        if (ssid->ext_psk) {
                size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
                char *buf = os_malloc(len);
+               int res;
+
                if (buf == NULL)
                        return NULL;
-               os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
+               res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
+               if (os_snprintf_error(len, res)) {
+                       os_free(buf);
+                       buf = NULL;
+               }
                return buf;
        }
 #endif /* CONFIG_EXT_PASSWORD */
@@ -2995,12 +3001,18 @@ int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
 
 static char * alloc_int_str(int val)
 {
+       const unsigned int bufsize = 20;
        char *buf;
+       int res;
 
-       buf = os_malloc(20);
+       buf = os_malloc(bufsize);
        if (buf == NULL)
                return NULL;
-       os_snprintf(buf, 20, "%d", val);
+       res = os_snprintf(buf, bufsize, "%d", val);
+       if (os_snprintf_error(bufsize, res)) {
+               os_free(buf);
+               buf = NULL;
+       }
        return buf;
 }
 
index ae65897..9e1fb29 100644 (file)
@@ -2973,9 +2973,13 @@ static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
        ssid = wpa_s->conf->ssid;
        while (ssid) {
                if (ssid->parent_cred == cred) {
+                       int res;
+
                        wpa_printf(MSG_DEBUG, "Remove network id %d since it "
                                   "used the removed credential", ssid->id);
-                       os_snprintf(str, sizeof(str), "%d", ssid->id);
+                       res = os_snprintf(str, sizeof(str), "%d", ssid->id);
+                       if (os_snprintf_error(sizeof(str), res))
+                               str[sizeof(str) - 1] = '\0';
                        ssid = ssid->next;
                        wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
                } else
@@ -3965,7 +3969,7 @@ static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
        struct dl_list *next;
        int ret = 0;
        int len;
-       char *ctmp;
+       char *ctmp, *end = buf + buflen;
        unsigned long mask = WPA_BSS_MASK_ALL;
 
        if (os_strncmp(cmd, "RANGE=", 6) == 0) {
@@ -4074,8 +4078,16 @@ static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
                if (bss == bsslast) {
                        if ((mask & WPA_BSS_MASK_DELIM) && len &&
                            (bss == dl_list_last(&wpa_s->bss_id,
-                                                struct wpa_bss, list_id)))
-                               os_snprintf(buf - 5, 5, "####\n");
+                                                struct wpa_bss, list_id))) {
+                               int res;
+
+                               res = os_snprintf(buf - 5, end - buf + 5,
+                                                 "####\n");
+                               if (os_snprintf_error(end - buf + 5, res)) {
+                                       wpa_printf(MSG_DEBUG,
+                                                  "Could not add end delim");
+                               }
+                       }
                        break;
                }
                next = bss->list_id.next;
@@ -6684,8 +6696,13 @@ static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
 
        for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
                if (wpa_s->vendor_elem[i]) {
-                       os_snprintf(buf, sizeof(buf), "frame[%u]", i);
-                       wpa_hexdump_buf(MSG_DEBUG, buf, wpa_s->vendor_elem[i]);
+                       int res;
+
+                       res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
+                       if (!os_snprintf_error(sizeof(buf), res)) {
+                               wpa_hexdump_buf(MSG_DEBUG, buf,
+                                               wpa_s->vendor_elem[i]);
+                       }
                }
        }
 
index aac3a10..267b59b 100644 (file)
@@ -1557,8 +1557,12 @@ static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
        os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
        if (os_strlen(ifname) >= IFNAMSIZ &&
            os_strlen(wpa_s->ifname) < IFNAMSIZ) {
+               int res;
+
                /* Try to avoid going over the IFNAMSIZ length limit */
-               os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
+               res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
+               if (os_snprintf_error(len, res) && len)
+                       ifname[len - 1] = '\0';
        }
 }
 
@@ -2948,6 +2952,7 @@ static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
        u8 empty_dev_type[8];
        unsigned int generated_pin = 0;
        struct wpa_supplicant *group = NULL;
+       int res;
 
        if (group_id) {
                for (group = wpa_s->global->ifaces; group; group = group->next)
@@ -2966,15 +2971,17 @@ static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
                os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
                pri_dev_type = empty_dev_type;
        }
-       os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
-                   " pri_dev_type=%s name='%s' config_methods=0x%x "
-                   "dev_capab=0x%x group_capab=0x%x%s%s",
-                   MAC2STR(dev_addr),
-                   wps_dev_type_bin2str(pri_dev_type, devtype,
-                                        sizeof(devtype)),
-                   dev_name, supp_config_methods, dev_capab, group_capab,
-                   group ? " group=" : "",
-                   group ? group->ifname : "");
+       res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
+                         " pri_dev_type=%s name='%s' config_methods=0x%x "
+                         "dev_capab=0x%x group_capab=0x%x%s%s",
+                         MAC2STR(dev_addr),
+                         wps_dev_type_bin2str(pri_dev_type, devtype,
+                                              sizeof(devtype)),
+                         dev_name, supp_config_methods, dev_capab, group_capab,
+                         group ? " group=" : "",
+                         group ? group->ifname : "");
+       if (os_snprintf_error(sizeof(params), res))
+               wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
        params[sizeof(params) - 1] = '\0';
 
        if (config_methods & WPS_CONFIG_DISPLAY) {
@@ -3010,10 +3017,14 @@ static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
        }
 
        if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
-           wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
-               os_snprintf(params, sizeof(params), " peer_go=%d",
-                           wpa_s->pending_pd_use == AUTO_PD_JOIN);
-       else
+           wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
+               int res;
+
+               res = os_snprintf(params, sizeof(params), " peer_go=%d",
+                                 wpa_s->pending_pd_use == AUTO_PD_JOIN);
+               if (os_snprintf_error(sizeof(params), res))
+                       params[sizeof(params) - 1] = '\0';
+       } else
                params[0] = '\0';
 
        if (config_methods & WPS_CONFIG_DISPLAY)
@@ -3921,8 +3932,10 @@ int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
        char force_name[100];
        int ret;
 
-       os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
-                   wpa_s->ifname);
+       ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
+                         wpa_s->ifname);
+       if (os_snprintf_error(sizeof(ifname), ret))
+               return -1;
        force_name[0] = '\0';
        wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
        ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
@@ -4968,8 +4981,10 @@ int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
                os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
        else if (wps_method == WPS_PIN_DISPLAY) {
                ret = wps_generate_pin();
-               os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
-                           ret);
+               res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
+                                 "%08d", ret);
+               if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
+                       wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
                wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
                           wpa_s->p2p_pin);
        } else
@@ -6268,11 +6283,16 @@ void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
 
        ip_addr[0] = '\0';
        if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
-               os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
-                           "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
-                           ip[0], ip[1], ip[2], ip[3],
-                           ip[4], ip[5], ip[6], ip[7],
-                           ip[8], ip[9], ip[10], ip[11]);
+               int res;
+
+               res = os_snprintf(ip_addr, sizeof(ip_addr),
+                                 " ip_addr=%u.%u.%u.%u "
+                                 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
+                                 ip[0], ip[1], ip[2], ip[3],
+                                 ip[4], ip[5], ip[6], ip[7],
+                                 ip[8], ip[9], ip[10], ip[11]);
+               if (os_snprintf_error(sizeof(ip_addr), res))
+                       ip_addr[0] = '\0';
        }
 
        wpas_p2p_group_started(wpa_s, 0, ssid, freq,