libc: replace 0 with NULL for pointers.

While here also cleanup some surrounding code; particularly
drop some malloc() casts.

Found with devel/coccinelle.

Reviewed by:	bde (previous version - all new bugs are mine)
This commit is contained in:
Pedro F. Giffuni
2016-04-10 19:33:58 +00:00
parent 6e5bbb486c
commit 513004a23d
22 changed files with 49 additions and 52 deletions
+1 -1
View File
@@ -771,7 +771,7 @@ hash_seq(const DB *dbp, DBT *key, DBT *data, u_int32_t flag)
if (__big_keydata(hashp, bufp, key, data, 1)) if (__big_keydata(hashp, bufp, key, data, 1))
return (ERROR); return (ERROR);
} else { } else {
if (hashp->cpage == 0) if (hashp->cpage == NULL)
return (ERROR); return (ERROR);
key->data = (u_char *)hashp->cpage->page + bp[ndx]; key->data = (u_char *)hashp->cpage->page + bp[ndx];
key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx]; key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
+2 -2
View File
@@ -245,7 +245,7 @@ newbuf(HTAB *hashp, u_int32_t addr, BUFHEAD *prev_bp)
*/ */
for (xbp = bp; xbp->ovfl;) { for (xbp = bp; xbp->ovfl;) {
next_xbp = xbp->ovfl; next_xbp = xbp->ovfl;
xbp->ovfl = 0; xbp->ovfl = NULL;
xbp = next_xbp; xbp = next_xbp;
/* Check that ovfl pointer is up date. */ /* Check that ovfl pointer is up date. */
@@ -350,7 +350,7 @@ __buf_free(HTAB *hashp, int do_free, int to_disk)
void void
__reclaim_buf(HTAB *hashp, BUFHEAD *bp) __reclaim_buf(HTAB *hashp, BUFHEAD *bp)
{ {
bp->ovfl = 0; bp->ovfl = NULL;
bp->addr = 0; bp->addr = 0;
bp->flags = 0; bp->flags = 0;
BUF_REMOVE(bp); BUF_REMOVE(bp);
+4 -4
View File
@@ -96,7 +96,7 @@ errc(int eval, int code, const char *fmt, ...)
void void
verrc(int eval, int code, const char *fmt, va_list ap) verrc(int eval, int code, const char *fmt, va_list ap)
{ {
if (err_file == 0) if (err_file == NULL)
err_set_file((FILE *)0); err_set_file((FILE *)0);
fprintf(err_file, "%s: ", _getprogname()); fprintf(err_file, "%s: ", _getprogname());
if (fmt != NULL) { if (fmt != NULL) {
@@ -121,7 +121,7 @@ errx(int eval, const char *fmt, ...)
void void
verrx(int eval, const char *fmt, va_list ap) verrx(int eval, const char *fmt, va_list ap)
{ {
if (err_file == 0) if (err_file == NULL)
err_set_file((FILE *)0); err_set_file((FILE *)0);
fprintf(err_file, "%s: ", _getprogname()); fprintf(err_file, "%s: ", _getprogname());
if (fmt != NULL) if (fmt != NULL)
@@ -161,7 +161,7 @@ warnc(int code, const char *fmt, ...)
void void
vwarnc(int code, const char *fmt, va_list ap) vwarnc(int code, const char *fmt, va_list ap)
{ {
if (err_file == 0) if (err_file == NULL)
err_set_file((FILE *)0); err_set_file((FILE *)0);
fprintf(err_file, "%s: ", _getprogname()); fprintf(err_file, "%s: ", _getprogname());
if (fmt != NULL) { if (fmt != NULL) {
@@ -183,7 +183,7 @@ warnx(const char *fmt, ...)
void void
vwarnx(const char *fmt, va_list ap) vwarnx(const char *fmt, va_list ap)
{ {
if (err_file == 0) if (err_file == NULL)
err_set_file((FILE *)0); err_set_file((FILE *)0);
fprintf(err_file, "%s: ", _getprogname()); fprintf(err_file, "%s: ", _getprogname());
if (fmt != NULL) if (fmt != NULL)
+1 -1
View File
@@ -56,7 +56,7 @@ getmntinfo(struct statfs **mntbufp, int flags)
if (mntbuf) if (mntbuf)
free(mntbuf); free(mntbuf);
bufsize = (mntsize + 1) * sizeof(struct statfs); bufsize = (mntsize + 1) * sizeof(struct statfs);
if ((mntbuf = (struct statfs *)malloc(bufsize)) == 0) if ((mntbuf = malloc(bufsize)) == NULL)
return (0); return (0);
if ((mntsize = getfsstat(mntbuf, bufsize, flags)) < 0) if ((mntsize = getfsstat(mntbuf, bufsize, flags)) < 0)
return (0); return (0);
+1 -1
View File
@@ -209,7 +209,7 @@ _filldir(DIR *dirp, bool use_current_pos)
* On the second pass, save pointers to each one. * On the second pass, save pointers to each one.
* Then sort the pointers and remove duplicate names. * Then sort the pointers and remove duplicate names.
*/ */
for (dpv = 0;;) { for (dpv = NULL;;) {
n = 0; n = 0;
ddptr = buf; ddptr = buf;
while (ddptr < ddeptr) { while (ddptr < ddeptr) {
+2 -2
View File
@@ -285,7 +285,7 @@ _init_tls(void)
while (*sp++ != 0) while (*sp++ != 0)
; ;
aux = (Elf_Auxinfo *) sp; aux = (Elf_Auxinfo *) sp;
phdr = 0; phdr = NULL;
phent = phnum = 0; phent = phnum = 0;
for (auxp = aux; auxp->a_type != AT_NULL; auxp++) { for (auxp = aux; auxp->a_type != AT_NULL; auxp++) {
switch (auxp->a_type) { switch (auxp->a_type) {
@@ -302,7 +302,7 @@ _init_tls(void)
break; break;
} }
} }
if (phdr == 0 || phent != sizeof(Elf_Phdr) || phnum == 0) if (phdr == NULL || phent != sizeof(Elf_Phdr) || phnum == 0)
return; return;
for (i = 0; (unsigned) i < phnum; i++) { for (i = 0; (unsigned) i < phnum; i++) {
+4 -5
View File
@@ -155,12 +155,11 @@ __attribute__((unused)) static void
xlocale_release(void *val) xlocale_release(void *val)
{ {
struct xlocale_refcounted *obj = val; struct xlocale_refcounted *obj = val;
long count = atomic_fetchadd_long(&(obj->retain_count), -1) - 1; long count;
if (count < 0) {
if (0 != obj->destructor) { count = atomic_fetchadd_long(&(obj->retain_count), -1) - 1;
if (count < 0 && obj->destructor != NULL)
obj->destructor(obj); obj->destructor(obj);
}
}
} }
/** /**
+1 -1
View File
@@ -210,7 +210,7 @@ b64_pton(const char *src, u_char *target, size_t targsize)
break; break;
pos = strchr(Base64, ch); pos = strchr(Base64, ch);
if (pos == 0) /* A non-base64 character. */ if (pos == NULL) /* A non-base64 character. */
return (-1); return (-1);
switch (state) { switch (state) {
+2 -1
View File
@@ -85,7 +85,7 @@ getifaddrs(struct ifaddrs **pif)
size_t needed; size_t needed;
char *buf; char *buf;
char *next; char *next;
struct ifaddrs *cif = 0; struct ifaddrs *cif;
char *p, *p0; char *p, *p0;
struct rt_msghdr *rtm; struct rt_msghdr *rtm;
struct if_msghdrl *ifm; struct if_msghdrl *ifm;
@@ -214,6 +214,7 @@ getifaddrs(struct ifaddrs **pif)
ift = ifa; ift = ifa;
idx = 0; idx = 0;
cif = NULL;
for (next = buf; next < buf + needed; next += rtm->rtm_msglen) { for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)(void *)next; rtm = (struct rt_msghdr *)(void *)next;
if (rtm->rtm_version != RTM_VERSION) if (rtm->rtm_version != RTM_VERSION)
+2 -2
View File
@@ -406,14 +406,14 @@ files_servent(void *retval, void *mdata, va_list ap)
continue; continue;
gotname: gotname:
if (proto == 0 || strcmp(serv->s_proto, proto) == 0) if (proto == NULL || strcmp(serv->s_proto, proto) == 0)
rv = NS_SUCCESS; rv = NS_SUCCESS;
break; break;
case nss_lt_id: case nss_lt_id:
if (port != serv->s_port) if (port != serv->s_port)
continue; continue;
if (proto == 0 || strcmp(serv->s_proto, proto) == 0) if (proto == NULL || strcmp(serv->s_proto, proto) == 0)
rv = NS_SUCCESS; rv = NS_SUCCESS;
break; break;
case nss_lt_all: case nss_lt_all:
+1 -1
View File
@@ -207,7 +207,7 @@ rcmd_af(char **ahost, int rport, const char *locuser, const char *remuser,
} }
} }
lport--; lport--;
if (fd2p == 0) { if (fd2p == NULL) {
_write(s, "", 1); _write(s, "", 1);
lport = 0; lport = 0;
} else { } else {
+1 -1
View File
@@ -81,7 +81,7 @@ static const char *
format_flag(uint32_t *var, const struct flagnames_struct *flags) format_flag(uint32_t *var, const struct flagnames_struct *flags)
{ {
for (; flags->name != 0; flags++) { for (; flags->name != NULL; flags++) {
if ((flags->flag & *var) == 0) if ((flags->flag & *var) == 0)
continue; continue;
+4 -7
View File
@@ -75,7 +75,7 @@ __res_init_ctx(void) {
return (0); return (0);
} }
if ((mt = malloc(sizeof (mtctxres_t))) == 0) { if ((mt = malloc(sizeof(mtctxres_t))) == NULL) {
errno = ENOMEM; errno = ENOMEM;
return (-1); return (-1);
} }
@@ -94,10 +94,7 @@ __res_init_ctx(void) {
static void static void
__res_destroy_ctx(void *value) { __res_destroy_ctx(void *value) {
mtctxres_t *mt = (mtctxres_t *)value; free(value);
if (mt != 0)
free(mt);
} }
#endif #endif
@@ -130,9 +127,9 @@ ___mtctxres(void) {
* that fails return a global context. * that fails return a global context.
*/ */
if (mt_key_initialized) { if (mt_key_initialized) {
if (((mt = pthread_getspecific(key)) != 0) || if (((mt = pthread_getspecific(key)) != NULL) ||
(__res_init_ctx() == 0 && (__res_init_ctx() == 0 &&
(mt = pthread_getspecific(key)) != 0)) { (mt = pthread_getspecific(key)) != NULL)) {
return (mt); return (mt);
} }
} }
+2 -2
View File
@@ -315,7 +315,7 @@ __res_vinit(res_state statp, int preinit) {
while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
cp++; cp++;
*cp = '\0'; *cp = '\0';
*pp++ = 0; *pp++ = NULL;
} }
#define MATCH(line, name) \ #define MATCH(line, name) \
@@ -391,7 +391,7 @@ __res_vinit(res_state statp, int preinit) {
while (*cp != '\0' && *cp != ' ' && *cp != '\t') while (*cp != '\0' && *cp != ' ' && *cp != '\t')
cp++; cp++;
*cp = '\0'; *cp = '\0';
*pp++ = 0; *pp++ = NULL;
havesearch = 1; havesearch = 1;
continue; continue;
} }
+2 -2
View File
@@ -1175,7 +1175,7 @@ res_protocolname(int num) {
if (protolist == (struct valuelist *)0) if (protolist == (struct valuelist *)0)
res_buildprotolist(); res_buildprotolist();
pp = cgetprotobynumber(num); pp = cgetprotobynumber(num);
if (pp == 0) { if (pp == NULL) {
(void) sprintf(number, "%d", num); (void) sprintf(number, "%d", num);
return (number); return (number);
} }
@@ -1190,7 +1190,7 @@ res_servicename(u_int16_t port, const char *proto) { /*%< Host byte order. */
if (servicelist == (struct valuelist *)0) if (servicelist == (struct valuelist *)0)
res_buildservicelist(); res_buildservicelist();
ss = cgetservbyport(htons(port), proto); ss = cgetservbyport(htons(port), proto);
if (ss == 0) { if (ss == NULL) {
(void) sprintf(number, "%d", port); (void) sprintf(number, "%d", port);
return (number); return (number);
} }
+3 -3
View File
@@ -83,9 +83,9 @@ authnone_create(void)
XDR *xdrs; XDR *xdrs;
mutex_lock(&authnone_lock); mutex_lock(&authnone_lock);
if (ap == 0) { if (ap == NULL) {
ap = (struct authnone_private *)calloc(1, sizeof (*ap)); ap = calloc(1, sizeof (*ap));
if (ap == 0) { if (ap == NULL) {
mutex_unlock(&authnone_lock); mutex_unlock(&authnone_lock);
return (0); return (0);
} }
+4 -4
View File
@@ -64,8 +64,8 @@ static char *
_buf(void) _buf(void)
{ {
if (buf == 0) if (buf == NULL)
buf = (char *)malloc(CLNT_PERROR_BUFLEN); buf = malloc(CLNT_PERROR_BUFLEN);
return (buf); return (buf);
} }
@@ -85,7 +85,7 @@ clnt_sperror(CLIENT *rpch, const char *s)
assert(s != NULL); assert(s != NULL);
str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */ str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
if (str == 0) if (str == NULL)
return (0); return (0);
len = CLNT_PERROR_BUFLEN; len = CLNT_PERROR_BUFLEN;
strstart = str; strstart = str;
@@ -240,7 +240,7 @@ clnt_spcreateerror(const char *s)
assert(s != NULL); assert(s != NULL);
str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */ str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
if (str == 0) if (str == NULL)
return(0); return(0);
len = CLNT_PERROR_BUFLEN; len = CLNT_PERROR_BUFLEN;
i = snprintf(str, len, "%s: ", s); i = snprintf(str, len, "%s: ", s);
+1 -1
View File
@@ -95,7 +95,7 @@ rce_key_init(void)
struct rpc_createerr * struct rpc_createerr *
__rpc_createerr(void) __rpc_createerr(void)
{ {
struct rpc_createerr *rce_addr = 0; struct rpc_createerr *rce_addr = NULL;
if (thr_main()) if (thr_main())
return (&rpc_createerr); return (&rpc_createerr);
+5 -5
View File
@@ -43,20 +43,20 @@ __FBSDID("$FreeBSD$");
#include <string.h> #include <string.h>
#include "un-namespace.h" #include "un-namespace.h"
static char *default_domain = 0; static char *default_domain;
static char * static char *
get_default_domain(void) get_default_domain(void)
{ {
char temp[256]; char temp[256];
if (default_domain) if (default_domain != NULL)
return (default_domain); return (default_domain);
if (getdomainname(temp, sizeof(temp)) < 0) if (getdomainname(temp, sizeof(temp)) < 0)
return (0); return (0);
if ((int) strlen(temp) > 0) { if ((int) strlen(temp) > 0) {
default_domain = (char *)malloc((strlen(temp)+(unsigned)1)); default_domain = malloc((strlen(temp) + (unsigned)1));
if (default_domain == 0) if (default_domain == NULL)
return (0); return (0);
(void) strcpy(default_domain, temp); (void) strcpy(default_domain, temp);
return (default_domain); return (default_domain);
@@ -73,7 +73,7 @@ get_default_domain(void)
int int
__rpc_get_default_domain(char **domain) __rpc_get_default_domain(char **domain)
{ {
if ((*domain = get_default_domain()) != 0) if ((*domain = get_default_domain()) != NULL)
return (0); return (0);
return (-1); return (-1);
} }
+3 -3
View File
@@ -2068,14 +2068,14 @@ static void
roundingPrecisionName = "80"; roundingPrecisionName = "80";
} }
else { else {
roundingPrecisionName = 0; roundingPrecisionName = NULL;
} }
#ifdef FLOATX80 #ifdef FLOATX80
floatx80_rounding_precision = roundingPrecision; floatx80_rounding_precision = roundingPrecision;
#endif #endif
switch ( roundingMode ) { switch ( roundingMode ) {
case 0: case 0:
roundingModeName = 0; roundingModeName = NULL;
roundingCode = float_round_nearest_even; roundingCode = float_round_nearest_even;
break; break;
case ROUND_NEAREST_EVEN: case ROUND_NEAREST_EVEN:
@@ -2098,7 +2098,7 @@ static void
float_rounding_mode = roundingCode; float_rounding_mode = roundingCode;
switch ( tininessMode ) { switch ( tininessMode ) {
case 0: case 0:
tininessModeName = 0; tininessModeName = NULL;
tininessCode = float_tininess_after_rounding; tininessCode = float_tininess_after_rounding;
break; break;
case TININESS_BEFORE_ROUNDING: case TININESS_BEFORE_ROUNDING:
+1 -1
View File
@@ -49,7 +49,7 @@ uuid_to_string(const uuid_t *u, char **s, uint32_t *status)
*status = uuid_s_ok; *status = uuid_s_ok;
/* Why allow a NULL-pointer here? */ /* Why allow a NULL-pointer here? */
if (s == 0) if (s == NULL)
return; return;
if (u == NULL) { if (u == NULL) {
+1 -1
View File
@@ -215,7 +215,7 @@ xdrmem_setpos(XDR *xdrs, u_int pos)
static int32_t * static int32_t *
xdrmem_inline_aligned(XDR *xdrs, u_int len) xdrmem_inline_aligned(XDR *xdrs, u_int len)
{ {
int32_t *buf = 0; int32_t *buf = NULL;
if (xdrs->x_handy >= len) { if (xdrs->x_handy >= len) {
xdrs->x_handy -= len; xdrs->x_handy -= len;