Add dup_binstr() to help common binary string tasks

There are quite a few places in the current implementation where a nul
terminated string is generated from binary data. Add a helper function
to simplify the code a bit.

Signed-hostap: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2013-04-27 23:44:59 +03:00
parent 8b44ad7e16
commit 5e24dc8a4b
18 changed files with 51 additions and 116 deletions

View file

@ -517,11 +517,9 @@ char * wpa_config_parse_string(const char *value, size_t *len)
if (pos == NULL || pos[1] != '\0')
return NULL;
*len = pos - value;
str = os_malloc(*len + 1);
str = dup_binstr(value, *len);
if (str == NULL)
return NULL;
os_memcpy(str, value, *len);
str[*len] = '\0';
return str;
} else if (*value == 'P' && value[1] == '"') {
const char *pos;
@ -532,11 +530,9 @@ char * wpa_config_parse_string(const char *value, size_t *len)
if (pos == NULL || pos[1] != '\0')
return NULL;
tlen = pos - value;
tstr = os_malloc(tlen + 1);
tstr = dup_binstr(value, tlen);
if (tstr == NULL)
return NULL;
os_memcpy(tstr, value, tlen);
tstr[tlen] = '\0';
str = os_malloc(tlen + 1);
if (str == NULL) {
@ -610,3 +606,19 @@ size_t merge_byte_arrays(u8 *res, size_t res_len,
return len;
}
char * dup_binstr(const void *src, size_t len)
{
char *res;
if (src == NULL)
return NULL;
res = os_malloc(len + 1);
if (res == NULL)
return NULL;
os_memcpy(res, src, len);
res[len] = '\0';
return res;
}

View file

@ -487,6 +487,7 @@ int is_hex(const u8 *data, size_t len);
size_t merge_byte_arrays(u8 *res, size_t res_len,
const u8 *src1, size_t src1_len,
const u8 *src2, size_t src2_len);
char * dup_binstr(const void *src, size_t len);
static inline int is_zero_ether_addr(const u8 *a)
{