Skip to content

Commit

Permalink
Add sanity checking for malloc, etc
Browse files Browse the repository at this point in the history
This update adds sanity checking to malloc requests and replaces
deprecated bzero() function calls with memset() which is ISO/ANSI/Posix
compliant
  • Loading branch information
dogbert2 authored and mcr committed Sep 5, 2012
1 parent 3baa50d commit 1881cd7
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions pcap-sita.c
Expand Up @@ -214,6 +214,9 @@ static void empty_unit(int chassis, int geoslot) {
empty_unit_iface(u);
if (u->imsg) { /* then if an inbound message buffer exists */
u->imsg = (char *)realloc(u->imsg, 1); /* and re-allocate the old large buffer into a new small one */
if (u->imsg == NULL) { /* oops, realloc call failed */
fprintf(stderr, "Warning...call to realloc() failed, value of errno is %d\n", errno);

}
}

Expand Down Expand Up @@ -311,9 +314,17 @@ static int open_with_IOP(unit_t *u, int flag) {

if (u->serv_addr == NULL) {
u->serv_addr = malloc(sizeof(struct sockaddr_in));

/* since we called malloc(), lets check to see if we actually got the memory */
if (u->serv_addr == NULL) { /* oops, we didn't get the memory requested */
fprintf(stderr, "malloc() request for u->serv_addr failed, value of errno is: %d\n", errno);
return 0;
}

}
ip = u->ip;
bzero((char *)u->serv_addr, sizeof(struct sockaddr_in));
/* bzero() is deprecated, replaced with memset() */
memset((char *)u->serv_addr, 0, sizeof(struct sockaddr_in));
u->serv_addr->sin_family = AF_INET;
u->serv_addr->sin_addr.s_addr = inet_addr(ip);
u->serv_addr->sin_port = htons(IOP_SNIFFER_PORT);
Expand Down Expand Up @@ -417,11 +428,20 @@ static char *translate_IOP_to_pcap_name(unit_t *u, char *IOPname, bpf_u_int32 if
int IOPportnum = 0;

iface = malloc(sizeof(iface_t)); /* get memory for a structure */
bzero((char *)iface, sizeof(iface_t));
if (iface == NULL) { /* oops, we didn't get the memory requested */
fprintf(stderr, "Error...couldn't allocate memory for interface structure...value of errno is: %d\n", errno);
return NULL;
}
memset((char *)iface, 0, sizeof(iface_t)); /* bzero is deprecated(), replaced with memset() */

iface->iftype = iftype; /* remember the interface type of this interface */

name = malloc(strlen(IOPname) + 1); /* get memory for the IOP's name */
if (name == NULL) { /* oops, we didn't get the memory requested */
fprintf(stderr, "Error...couldn't allocate memory for IOPname...value of errno is: %d\n", errno);
return NULL;
}

strcpy(name, IOPname); /* and copy it in */
iface->IOPname = name; /* and stick it into the structure */

Expand All @@ -447,6 +467,11 @@ static char *translate_IOP_to_pcap_name(unit_t *u, char *IOPname, bpf_u_int32 if

sprintf(buf, "%s_%s", proto, port); /* compose the user's name for that IOP port name */
name = malloc(strlen(buf) + 1); /* get memory for that name */
if (name == NULL) { /* oops, we didn't get the memory requested */
fprintf(stderr, "Error...couldn't allocate memory for IOP port name...value of errno is: %d\n", errno);
return NULL;
}

strcpy(name, buf); /* and copy it in */
iface->name = name; /* and stick it into the structure */

Expand Down Expand Up @@ -548,7 +573,7 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0
snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
return -1;
}
bzero((char *)iff, sizeof(pcap_if_t));
memset((char *)iff, 0, sizeof(pcap_if_t)); /* bzero() is deprecated, replaced with memset() */
if (acn_if_list == 0) acn_if_list = iff; /* remember the head of the list */
if (prev_iff) prev_iff->next = iff; /* insert a forward link */

Expand Down Expand Up @@ -588,15 +613,15 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0
snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
return -1;
}
bzero((char *)addr, sizeof(pcap_addr_t));
+ memset((char *)addr, 0, sizeof(pcap_addr_t)); /* bzero() is deprecated, replaced with memset() */
if (iff->addresses == 0) iff->addresses = addr;
if (prev_addr) prev_addr->next = addr; /* insert a forward link */
if (*ptr) { /* if there is a count for the address */
if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { /* get that amount of space */
snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
return -1;
}
bzero((char *)s, sizeof(struct sockaddr_in));
memset((char *)s, 0, sizeof(struct sockaddr_in)); /* bzero() is deprecated, replaced with memset() */
addr->addr = (struct sockaddr *)s;
s->sin_family = AF_INET;
s->sin_addr.s_addr = *(bpf_u_int32 *)(ptr + 1); /* copy the address in */
Expand All @@ -608,7 +633,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0
snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
return -1;
}
bzero((char *)s, sizeof(struct sockaddr_in));
/* bzero() is deprecated, replaced with memset() */
memset((char *)s, 0, sizeof(struct sockaddr_in));

addr->netmask = (struct sockaddr *)s;
s->sin_family = AF_INET;
s->sin_addr.s_addr = *(bpf_u_int32*)(ptr + 1);
Expand All @@ -620,7 +647,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0
snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
return -1;
}
bzero((char *)s, sizeof(struct sockaddr_in));
/* bzero() is deprecated, replaced with memset() */
memset((char *)s, 0, sizeof(struct sockaddr_in));

addr->broadaddr = (struct sockaddr *)s;
s->sin_family = AF_INET;
s->sin_addr.s_addr = *(bpf_u_int32*)(ptr + 1);
Expand All @@ -632,7 +661,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0
snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno));
return -1;
}
bzero((char *)s, sizeof(struct sockaddr_in));
/* bzero() is deprecated, replaced with memset() */
memset((char *)s, 0, sizeof(struct sockaddr_in));

addr->dstaddr = (struct sockaddr *)s;
s->sin_family = AF_INET;
s->sin_addr.s_addr = *(bpf_u_int32*)(ptr + 1);
Expand Down

0 comments on commit 1881cd7

Please sign in to comment.