Skip to content

Commit

Permalink
MAC parsing protection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Nicosia committed Dec 8, 2022
1 parent b5b691d commit 58834a0
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions sources/parse_option_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static int invalid_ip(char *str)
k++;
}

if (check != IP_ADDR_LEN-1) {
if (check != IP_ADDR_LEN - 1) {
fprintf(stderr, "Invalid IP address %s (invalid number of bytes)\n", str);
return 1;
}
Expand Down Expand Up @@ -76,10 +76,23 @@ static int invalid_ip(char *str)

static int ft_atom(char *str, uint8_t *dest)
{
char **split = ft_strsplit(str, ':');
char **split;
int i = 0;
int ret = 0;

int k = 0, check = 0;
while (str[k]) {
if (str[k] == ':')
check++;
k++;
}

if (check != ETH_ADDR_LEN - 1) {
fprintf(stderr, "Invalid mac address %s (invalid number of bytes)\n", str);
return 1;
}

split = ft_strsplit(str, ':');
if (!split) {
fprintf(stderr, "ft_strsplit fail\n");
return 1;
Expand All @@ -93,11 +106,6 @@ static int ft_atom(char *str, uint8_t *dest)
ret = 1;
break;
}
if (byte >= ETH_ADDR_LEN) {
fprintf(stderr, "Invalid mac address %s (too many bytes)\n", str);
ret = 1;
break;
}
int j = 0;
while ((*tmp)[j]) {
(*tmp)[j] = ft_toupper((*tmp)[j]);
Expand All @@ -115,6 +123,11 @@ static int ft_atom(char *str, uint8_t *dest)
tmp++;
}

if (!ret && byte != ETH_ADDR_LEN) {
fprintf(stderr, "Invalid mac address %s (invalid number of bytes)\n", str);
ret = 1;
}

while (split[i])
free(split[i++]);
free(split);
Expand Down

0 comments on commit 58834a0

Please sign in to comment.