Skip to content

Commit 9468cb6

Browse files
vanwinkeljanjukkar
authored andcommitted
tests: net: hostname: Corrected unique hostname conversion
Replaced net_bytes_from_str in hostname_get test by a new function that converts the unique part of the hostname in a byte array. net_bytes_from_str can not be used as it assumes that the string input is of the format "xx:xx:xx:xx:xx:xx" where as the unique part of the hostname is a MAC address string without colons. If net_bytes_from_str is used this could result in buffer overrun on the input string. Signed-off-by: Jan Van Winkel <[email protected]>
1 parent 391076c commit 9468cb6

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

tests/net/hostname/src/main.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,40 @@ static void iface_setup(void)
269269
test_started = true;
270270
}
271271

272+
static int bytes_from_hostname_unique(u8_t *buf, int buf_len, const char *src)
273+
{
274+
unsigned int i;
275+
276+
(void)memset(buf, 0, buf_len);
277+
278+
if ((2 * buf_len) < strlen(src)) {
279+
return -ENOMEM;
280+
}
281+
282+
for (i = 0U; i < strlen(src); i++) {
283+
buf[i/2] <<= 4;
284+
285+
if (src[i] >= '0' && src[i] <= '9') {
286+
buf[i/2] += (src[i] - '0');
287+
continue;
288+
}
289+
290+
if (src[i] >= 'A' && src[i] <= 'F') {
291+
buf[i/2] += (10 + (src[i] - 'A'));
292+
continue;
293+
}
294+
295+
if (src[i] >= 'a' && src[i] <= 'f') {
296+
buf[i/2] += (10 + (src[i] - 'a'));
297+
continue;
298+
}
299+
300+
return -EINVAL;
301+
}
302+
303+
return 0;
304+
}
305+
272306
static void hostname_get(void)
273307
{
274308
const char *hostname;
@@ -280,10 +314,10 @@ static void hostname_get(void)
280314
sizeof(CONFIG_NET_HOSTNAME) - 1, "");
281315

282316
if (IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE)) {
283-
char mac[8];
317+
char mac[6];
284318
int ret;
285319

286-
ret = net_bytes_from_str(mac, sizeof(mac),
320+
ret = bytes_from_hostname_unique(mac, sizeof(mac),
287321
hostname + sizeof(CONFIG_NET_HOSTNAME) - 1);
288322
zassert_equal(ret, 0, "");
289323

0 commit comments

Comments
 (0)