Use os_memdup()

This leads to cleaner code overall, and also reduces the size
of the hostapd and wpa_supplicant binaries (in hwsim test build
on x86_64) by about 2.5 and 3.5KiB respectively.

The mechanical conversions all over the code were done with
the following spatch:

    @@
    expression SIZE, SRC;
    expression a;
    @@
    -a = os_malloc(SIZE);
    +a = os_memdup(SRC, SIZE);
    <...
    if (!a) {...}
    ...>
    -os_memcpy(a, SRC, SIZE);

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
This commit is contained in:
Johannes Berg 2017-03-07 10:17:23 +01:00 committed by Jouni Malinen
parent dbdda355d0
commit a1f11e34c4
73 changed files with 201 additions and 376 deletions

View file

@ -71,11 +71,10 @@ static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
}
if (eap_user->password) {
user->password = os_malloc(eap_user->password_len);
user->password = os_memdup(eap_user->password,
eap_user->password_len);
if (user->password == NULL)
goto out;
os_memcpy(user->password, eap_user->password,
eap_user->password_len);
user->password_len = eap_user->password_len;
user->password_hash = eap_user->password_hash;
}

View file

@ -2953,60 +2953,52 @@ static int hostapd_build_beacon_data(struct hostapd_data *hapd,
goto free_ap_params;
ret = -1;
beacon->head = os_malloc(params.head_len);
beacon->head = os_memdup(params.head, params.head_len);
if (!beacon->head)
goto free_ap_extra_ies;
os_memcpy(beacon->head, params.head, params.head_len);
beacon->head_len = params.head_len;
beacon->tail = os_malloc(params.tail_len);
beacon->tail = os_memdup(params.tail, params.tail_len);
if (!beacon->tail)
goto free_beacon;
os_memcpy(beacon->tail, params.tail, params.tail_len);
beacon->tail_len = params.tail_len;
if (params.proberesp != NULL) {
beacon->probe_resp = os_malloc(params.proberesp_len);
beacon->probe_resp = os_memdup(params.proberesp,
params.proberesp_len);
if (!beacon->probe_resp)
goto free_beacon;
os_memcpy(beacon->probe_resp, params.proberesp,
params.proberesp_len);
beacon->probe_resp_len = params.proberesp_len;
}
/* copy the extra ies */
if (beacon_extra) {
beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
beacon->beacon_ies = os_memdup(beacon_extra->buf,
wpabuf_len(beacon_extra));
if (!beacon->beacon_ies)
goto free_beacon;
os_memcpy(beacon->beacon_ies,
beacon_extra->buf, wpabuf_len(beacon_extra));
beacon->beacon_ies_len = wpabuf_len(beacon_extra);
}
if (proberesp_extra) {
beacon->proberesp_ies =
os_malloc(wpabuf_len(proberesp_extra));
beacon->proberesp_ies = os_memdup(proberesp_extra->buf,
wpabuf_len(proberesp_extra));
if (!beacon->proberesp_ies)
goto free_beacon;
os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
wpabuf_len(proberesp_extra));
beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
}
if (assocresp_extra) {
beacon->assocresp_ies =
os_malloc(wpabuf_len(assocresp_extra));
beacon->assocresp_ies = os_memdup(assocresp_extra->buf,
wpabuf_len(assocresp_extra));
if (!beacon->assocresp_ies)
goto free_beacon;
os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
wpabuf_len(assocresp_extra));
beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
}

View file

@ -2715,12 +2715,11 @@ static void handle_assoc(struct hostapd_data *hapd,
/* The end of the payload is encrypted. Need to decrypt it
* before parsing. */
tmp = os_malloc(left);
tmp = os_memdup(pos, left);
if (!tmp) {
resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
goto fail;
}
os_memcpy(tmp, pos, left);
left = fils_decrypt_assoc(sta->wpa_sm, sta->fils_session, mgmt,
len, tmp, left);
@ -3188,10 +3187,9 @@ static int handle_action(struct hostapd_data *hapd,
*/
wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
"frame back to sender");
resp = os_malloc(len);
resp = os_memdup(mgmt, len);
if (resp == NULL)
return 0;
os_memcpy(resp, mgmt, len);
os_memcpy(resp->da, resp->sa, ETH_ALEN);
os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);

View file

@ -327,14 +327,13 @@ int hostapd_allowed_address(struct hostapd_data *hapd, const u8 *addr,
return HOSTAPD_ACL_REJECT;
}
query->auth_msg = os_malloc(len);
query->auth_msg = os_memdup(msg, len);
if (query->auth_msg == NULL) {
wpa_printf(MSG_ERROR, "Failed to allocate memory for "
"auth frame.");
hostapd_acl_query_free(query);
return HOSTAPD_ACL_REJECT;
}
os_memcpy(query->auth_msg, msg, len);
query->auth_msg_len = len;
query->next = hapd->acl_queries;
hapd->acl_queries = query;

View file

@ -1420,11 +1420,10 @@ static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
}
} while (class_len < 1);
nclass[nclass_count].data = os_malloc(class_len);
nclass[nclass_count].data = os_memdup(attr_class, class_len);
if (nclass[nclass_count].data == NULL)
break;
os_memcpy(nclass[nclass_count].data, attr_class, class_len);
nclass[nclass_count].len = class_len;
nclass_count++;
}
@ -2072,11 +2071,10 @@ static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
}
if (eap_user->password) {
user->password = os_malloc(eap_user->password_len);
user->password = os_memdup(eap_user->password,
eap_user->password_len);
if (user->password == NULL)
goto out;
os_memcpy(user->password, eap_user->password,
eap_user->password_len);
user->password_len = eap_user->password_len;
user->password_hash = eap_user->password_hash;
}

View file

@ -1335,10 +1335,9 @@ continue_processing:
#endif /* CONFIG_PEERKEY */
os_free(sm->last_rx_eapol_key);
sm->last_rx_eapol_key = os_malloc(data_len);
sm->last_rx_eapol_key = os_memdup(data, data_len);
if (sm->last_rx_eapol_key == NULL)
return;
os_memcpy(sm->last_rx_eapol_key, data, data_len);
sm->last_rx_eapol_key_len = data_len;
sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);