Skip to content

Commit

Permalink
Cleaned up implementations of libnet_get_hwaddr(), some leaked memory,
Browse files Browse the repository at this point in the history
one returned a pointer to data on the stack, and the others return a
pointer to static data. I'm settling on the non-reentrant static data
form.
  • Loading branch information
sam-github committed May 3, 2010
1 parent 933c8a0 commit 56e3b06
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
14 changes: 4 additions & 10 deletions libnet/src/libnet_link_bpf.c
Expand Up @@ -261,7 +261,8 @@ libnet_get_hwaddr(libnet_t *l)
int8_t *buf, *next, *end;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
struct libnet_ether_addr *ea = NULL;
/* This implementation is not-reentrant. */
static struct libnet_ether_addr ea;

mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
Expand Down Expand Up @@ -319,20 +320,13 @@ libnet_get_hwaddr(libnet_t *l)
continue;
if (strncmp(&sdl->sdl_data[0], l->device, sdl->sdl_nlen) == 0)
{
if (!(ea = malloc(sizeof(struct libnet_ether_addr))))
{
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"%s(): malloc(): %s", __func__, strerror(errno));
free(buf);
return (NULL);
}
memcpy(ea->ether_addr_octet, LLADDR(sdl), ETHER_ADDR_LEN);
memcpy(ea.ether_addr_octet, LLADDR(sdl), ETHER_ADDR_LEN);
break;
}
}
}
free(buf);
return (ea);
return (&ea);
}


Expand Down
3 changes: 2 additions & 1 deletion libnet/src/libnet_link_dlpi.c
Expand Up @@ -735,7 +735,8 @@ libnet_write_link(libnet_t *l, const uint8_t *packet, uint32_t size)
struct libnet_ether_addr *
libnet_get_hwaddr(libnet_t *l)
{
int8_t buf[2048];
/* This implementation is not-reentrant. */
static int8_t buf[2048];
union DL_primitives *dlp;
struct libnet_ether_addr *eap;

Expand Down

0 comments on commit 56e3b06

Please sign in to comment.