Nmap Development mailing list archives
[PATCH] size_t and safe_*alloc()
From: Kris Katterjohn <kjak () ispwest com>
Date: Thu, 02 Mar 2006 16:33:39 -0600
The attached patch changes int to size_t for the safe_*alloc() functions. K&R says that size_t is unsigned, so there's no need to check for negative values. safe_realloc() had it size_t before, and it was still checking for negatives! All the actual malloc(), realloc() and calloc() functions use size_t, anyway. nmap.h #includes nbase.h, so there's no need to have the extra prototype for safe_malloc() there. Thanks, Kris Katterjohn
--- nmap.h.orig 2006-03-02 16:17:44.000000000 -0600 +++ nmap.h 2006-03-02 16:17:49.000000000 -0600 @@ -454,7 +454,6 @@ int listen_icmp(int icmpsock, unsigned s int nmap_main(int argc, char *argv[]); /* general helper functions */ -void *safe_malloc(int size); char *grab_next_host_spec(FILE *inputfd, int argc, char **fakeargv); int parse_targets(struct targets *targets, char *h); char *statenum2str(int state); --- nbase/nbase.h.orig 2006-03-02 16:17:21.000000000 -0600 +++ nbase/nbase.h 2006-03-02 16:17:35.000000000 -0600 @@ -388,10 +388,10 @@ void replacenonprintable(char *str, int /* A few simple wrappers for the most common memory allocation routines which will exit() if the allocation fails, so you don't always have to check -- see nbase_memalloc.c */ -void *safe_malloc(int size); +void *safe_malloc(size_t size); void *safe_realloc(void *ptr, size_t size); /* Zero-initializing version of safe_malloc */ -void *safe_zalloc(int size); +void *safe_zalloc(size_t size); /* Some routines for obtaining simple (not secure on systems that lack /dev/random and friends' "random" numbers */ --- nbase/nbase_memalloc.c.orig 2006-03-02 16:16:32.000000000 -0600 +++ nbase/nbase_memalloc.c 2006-03-02 16:17:14.000000000 -0600 @@ -118,12 +118,9 @@ va_end(ap); exit(1); } -void *safe_malloc(int size) +void *safe_malloc(size_t size) { - void *mymem; - if (size < 0) - fatal("Tried to malloc negative amount of memory!!!"); - mymem = malloc(size); + void *mymem = malloc(size); if (mymem == NULL) fatal("Malloc Failed! Probably out of space."); return mymem; @@ -131,22 +128,16 @@ void *safe_malloc(int size) void *safe_realloc(void *ptr, size_t size) { - void *mymem; - if (size < 0) - fatal("Tried to realloc negative amount of memory!!!"); - mymem = realloc(ptr, size); + void *mymem = realloc(ptr, size); if (mymem == NULL) fatal("Realloc Failed! Probably out of space."); return mymem; } /* Zero-initializing version of safe_malloc */ -void *safe_zalloc(int size) +void *safe_zalloc(size_t size) { - void *mymem; - if (size < 0) - fatal("Tried to malloc negative amount of memory!!!"); - mymem = calloc(1, size); + void *mymem = calloc(1, size); if (mymem == NULL) fatal("Malloc Failed! Probably out of space."); return mymem;
_______________________________________________ Sent through the nmap-dev mailing list http://cgi.insecure.org/mailman/listinfo/nmap-dev
Current thread:
- [PATCH] size_t and safe_*alloc() Kris Katterjohn (Mar 02)
- Re: [PATCH] size_t and safe_*alloc() Fyodor (Mar 02)