Check return codes everywhere
Brought to you by:
ksmathers
Some checks for return codes are missing.
Example:
Please add more error handling for null pointers from
"malloc" in the function "net_accept".
http://cvs.sourceforge.net/viewcvs.py/libbt/libbt/src/net.c?rev=1.8&view=markup
Logged In: YES
user_id=1339846
I've gone through net.c and added error checking to all the
malloc and memset calls. The diff patch is pasted below.
--- net.c 2005-08-15 15:17:01.000000000 -0500
+++ new.c 2006-02-23 17:15:19.814375000 -0500
@@ -38,10 +38,17 @@
{
int ret;
struct addrinfo hints;
- memset(&hints,0,sizeof(hints));
- hints.ai_family=PF_UNSPEC;
- hints.ai_socktype=SOCK_STREAM;
- hints.ai_flags=AI_PASSIVE;
+ if(memset(&hints,0,sizeof(hints)))
+ {
+ hints.ai_family=PF_UNSPEC;
+ hints.ai_socktype=SOCK_STREAM;
+ hints.ai_flags=AI_PASSIVE;
+ }
+ else
+ {
+ fprintf(stderr, "memset returns NULL in
net_build_addrinfo\n");
+ return 0;
+ }
ret=getaddrinfo(node, service, &hints, addr);
if(ret)
@@ -61,24 +68,52 @@
a=htonl(((struct
sockaddr_in*)addr->ai_addr)->sin_addr.s_addr);
p=htons(((struct sockaddr_in*)addr->ai_addr)->sin_port);
*node=malloc(16);
- snprintf(*node,16,"%hhu.%hhu.%hhu.%hhu", ((a>>24)&0xFF),
((a>>16)&0xFF), ((a>>8)&0xFF), (a&0xFF));
- /* get port from socket */
- *service=malloc(6);
- snprintf(*service,16,"%d", p);
+ if(node)
+ {
+ snprintf(*node,16,"%hhu.%hhu.%hhu.%hhu",
((a>>24)&0xFF), ((a>>16)&0xFF), ((a>>8)&0xFF), (a&0xFF));
+ /* get port from socket */
+ *service=malloc(6);
+ if(service)
+ {
+ snprintf(*service,16,"%d", p);
+ }
+ else
+ {
+ fprintf(stderr, "malloc(6) returns NULL in
net_get_from_addrinfo\n");
+ }
+ }
+ else
+ {
+ fprintf(stderr, "malloc(16) returns NULL in
net_get_from_addrinfo\n");
+ }
}
struct addrinfo* net_addrinfo_dup(struct addrinfo* addr)
{
struct addrinfo* res;
res=malloc(sizeof(struct addrinfo));
- memset(res, 0, sizeof(struct addrinfo));
- res->ai_family=addr->ai_family;
- res->ai_socktype=addr->ai_socktype;
- res->ai_protocol=addr->ai_protocol;
- res->ai_addrlen=addr->ai_addrlen;
- res->ai_addr=malloc(res->ai_addrlen);
- memcpy(res->ai_addr, addr->ai_addr, res->ai_addrlen);
- return res;
+ if(res)
+ {
+ if(memset(res, 0, sizeof(struct addrinfo)))
+ {
+ res->ai_family=addr->ai_family;
+ res->ai_socktype=addr->ai_socktype;
+ res->ai_protocol=addr->ai_protocol;
+ res->ai_addrlen=addr->ai_addrlen;
+ res->ai_addr=malloc(res->ai_addrlen);
+ memcpy(res->ai_addr, addr->ai_addr, res->ai_addrlen);
+ return res;
+ }
+ else
+ {
+ fprintf(stderr, "memset returns NULL in
net_addrinfo_dup\n");
+ }
+ }
+ else
+ {
+ fprintf(stderr, "malloc returns NULL in
net_addrinfo_dup\n");
+ }
+ return res; /*An error has occurred, returns an
unitialized struct*/
}
static void net_listener_free(file_cache_t* cache,
listen_t* listener)
@@ -153,22 +188,41 @@
fops_socket_t fd;
/* copy addr structure from owner */
*addr=malloc(sizeof(struct addrinfo));
- memset(*addr, 0, sizeof(struct addrinfo));
- (*addr)->ai_family=listener->addr->ai_family;
- (*addr)->ai_socktype=listener->addr->ai_socktype;
- (*addr)->ai_protocol=listener->addr->ai_protocol;
- (*addr)->ai_addrlen=listener->addr->ai_addrlen;
- (*addr)->ai_addr=malloc((*addr)->ai_addrlen);
- memset((*addr)->ai_addr,0,(*addr)->ai_addrlen);
- /* accept socket */
- fd=file_accept(cache, *addr, listener->fd, 0);
- if(fd==FILE_OPEN_FAILED)
- {
- free((*addr)->ai_addr);
- free(*addr);
- *addr=NULL;
+ if(addr)
+ {
+ if(memset(*addr, 0, sizeof(struct addrinfo)))
+ {
+ (*addr)->ai_family=listener->addr->ai_family;
+ (*addr)->ai_socktype=listener->addr->ai_socktype;
+ (*addr)->ai_protocol=listener->addr->ai_protocol;
+ (*addr)->ai_addrlen=listener->addr->ai_addrlen;
+ (*addr)->ai_addr=malloc((*addr)->ai_addrlen);
+ if(memset((*addr)->ai_addr,0,(*addr)->ai_addrlen))
+ {
+ /* accept socket */
+ fd=file_accept(cache, *addr, listener->fd, 0);
+ if(fd==FILE_OPEN_FAILED)
+ {
+ free((*addr)->ai_addr);
+ free(*addr);
+ *addr=NULL;
+ }
+ return fd;
+ }
+ else
+ {
+ fprintf(stderr, "second memset returns NULL in
net_accept\n");
+ }
+ }
+ else
+ {
+ fprintf(stderr, "first memset returns NULL in
net_accept\n");
+ }
+ }
+ else
+ {
+ fprintf(stderr, "malloc(sizeof(struct addrinf))
returns NULL in net_accept\n");
}
- return fd;
}
/* set non_blocking may not be necessary */
Logged In: YES
user_id=572001
Thanks for this notice.
I would prefer that the corrections will be integrated into
the version repository.
By the way: I guess that the long comment would be more
helpful as an attachment ...