Call NULL by its name

In variadic functions we still do the cast.  In POSIX, it's not
necessary, since NULL is required to be of type 'void *', and 'void *'
is guaranteed to have the same alignment and representation as 'char *'.
However, since ISO C still doesn't mandate that, and moreover they're
doing dubious stuff by adding nullptr, let's be on the cautious side.
Also, C++ requires that NULL is _not_ 'void *', but either plain 0 or
some magic stuff.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2023-02-01 02:50:14 +01:00
committed by Serge Hallyn
parent 1482224c54
commit 62172f6fb5
31 changed files with 84 additions and 84 deletions

View File

@@ -510,28 +510,28 @@ static void get_old_fields (const char *gecos)
* Now get the full name. It is the first comma separated field in
* the GECOS field.
*/
cp = copy_field (old_gecos, fflg ? (char *) 0 : fullnm, slop);
cp = copy_field (old_gecos, fflg ? NULL : fullnm, slop);
/*
* Now get the room number. It is the next comma separated field,
* if there is indeed one.
*/
if (NULL != cp) {
cp = copy_field (cp, rflg ? (char *) 0 : roomno, slop);
cp = copy_field (cp, rflg ? NULL : roomno, slop);
}
/*
* Now get the work phone number. It is the third field.
*/
if (NULL != cp) {
cp = copy_field (cp, wflg ? (char *) 0 : workph, slop);
cp = copy_field (cp, wflg ? NULL : workph, slop);
}
/*
* Now get the home phone number. It is the fourth field.
*/
if (NULL != cp) {
cp = copy_field (cp, hflg ? (char *) 0 : homeph, slop);
cp = copy_field (cp, hflg ? NULL : homeph, slop);
}
/*

View File

@@ -441,7 +441,7 @@ int main (int argc, char **argv)
* group entry for each group will be looked up in the appropriate
* file (gshadow or group) and the password changed.
*/
while (fgets (buf, (int) sizeof buf, stdin) != (char *) 0) {
while (fgets (buf, (int) sizeof buf, stdin) != NULL) {
line++;
cp = strrchr (buf, '\n');
if (NULL != cp) {

View File

@@ -482,7 +482,7 @@ int main (int argc, char **argv)
* last change date is set in the age only if aging information is
* present.
*/
while (fgets (buf, (int) sizeof buf, stdin) != (char *) 0) {
while (fgets (buf, (int) sizeof buf, stdin) != NULL) {
line++;
cp = strrchr (buf, '\n');
if (NULL != cp) {
@@ -491,7 +491,7 @@ int main (int argc, char **argv)
if (feof (stdin) == 0) {
// Drop all remaining characters on this line.
while (fgets (buf, (int) sizeof buf, stdin) != (char *) 0) {
while (fgets (buf, (int) sizeof buf, stdin) != NULL) {
cp = strchr (buf, '\n');
if (cp != NULL) {
break;

View File

@@ -57,7 +57,7 @@ static bool rflg = false; /* reset the counters of login failures */
static struct stat statbuf; /* fstat buffer for file size */
#define NOW (time((time_t *) 0))
#define NOW time(NULL)
static /*@noreturn@*/void usage (int status)
{

View File

@@ -721,7 +721,7 @@ static void check_perms (const struct group *gr)
* --marekm
*/
if (!amroot) {
if (gr->gr_mem[0] == (char *) 0) {
if (gr->gr_mem[0] == NULL) {
failure ();
}

View File

@@ -275,7 +275,7 @@ static void group_busy (gid_t gid)
* If pwd isn't NULL, it stopped because the gid's matched.
*/
if (pwd == (struct passwd *) 0) {
if (pwd == NULL) {
return;
}

View File

@@ -248,7 +248,7 @@ static void grp_update (void)
if (NULL != grp.gr_mem[0])
gr_free_members(&grp);
grp.gr_mem = (char **)xmalloc(sizeof(char *));
grp.gr_mem[0] = (char *)0;
grp.gr_mem[0] = NULL;
} else {
// append to existing groups
if (NULL != grp.gr_mem[0])

View File

@@ -56,7 +56,7 @@ static bool bflg = false; /* print excludes most recent days */
static bool Cflg = false; /* clear record for user */
static bool Sflg = false; /* set record for user */
#define NOW (time ((time_t *) 0))
#define NOW time(NULL)
static /*@noreturn@*/void usage (int status)
{

View File

@@ -974,7 +974,7 @@ int main (int argc, char **argv)
goto auth_ok;
}
if (pw_auth (user_passwd, username, reason, (char *) 0) == 0) {
if (pw_auth (user_passwd, username, reason, NULL) == 0) {
goto auth_ok;
}
@@ -1041,7 +1041,7 @@ int main (int argc, char **argv)
* all). --marekm
*/
if (user_passwd[0] == '\0') {
pw_auth ("!", username, reason, (char *) 0);
pw_auth ("!", username, reason, NULL);
}
/*
@@ -1081,7 +1081,7 @@ int main (int argc, char **argv)
* by Ivan Nejgebauer <ian@unsux.ns.ac.yu>. --marekm
*/
if ( getdef_bool ("PORTTIME_CHECKS_ENAB")
&& !isttytime (username, tty, time ((time_t *) 0))) {
&& !isttytime (username, tty, time (NULL))) {
SYSLOG ((LOG_WARN, "invalid login time for '%s'%s",
username, fromhost));
closelog ();
@@ -1308,7 +1308,7 @@ int main (int argc, char **argv)
err = shell (tmp, pwd->pw_shell, newenvp); /* fake shell */
} else {
/* exec the shell finally */
err = shell (pwd->pw_shell, (char *) 0, newenvp);
err = shell (pwd->pw_shell, NULL, newenvp);
}
return ((err == ENOENT) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);

View File

@@ -117,9 +117,9 @@ int login_access (const char *user, const char *from)
continue;
}
if ( ((perm = strtok (line, fs)) == NULL)
|| ((users = strtok ((char *) 0, fs)) == NULL)
|| ((froms = strtok ((char *) 0, fs)) == NULL)
|| (strtok ((char *) 0, fs) != NULL)) {
|| ((users = strtok (NULL, fs)) == NULL)
|| ((froms = strtok (NULL, fs)) == NULL)
|| (strtok (NULL, fs) != NULL)) {
SYSLOG ((LOG_ERR,
"%s: line %d: bad field count",
TABLE, lineno));
@@ -154,7 +154,7 @@ static bool list_match (char *list, const char *item, bool (*match_fn) (const ch
* a match, look for an "EXCEPT" list and recurse to determine whether
* the match is affected by any exceptions.
*/
for (tok = strtok (list, sep); tok != NULL; tok = strtok ((char *) 0, sep)) {
for (tok = strtok (list, sep); tok != NULL; tok = strtok (NULL, sep)) {
if (strcasecmp (tok, "EXCEPT") == 0) { /* EXCEPT: give up */
break;
}
@@ -166,10 +166,10 @@ static bool list_match (char *list, const char *item, bool (*match_fn) (const ch
/* Process exceptions to matches. */
if (match) {
while ( ((tok = strtok ((char *) 0, sep)) != NULL)
while ( ((tok = strtok (NULL, sep)) != NULL)
&& (strcasecmp (tok, "EXCEPT") != 0))
/* VOID */ ;
if (tok == 0 || !list_match ((char *) 0, item, match_fn)) {
if (tok == 0 || !list_match (NULL, item, match_fn)) {
return (match);
}
}
@@ -193,9 +193,9 @@ static char *myhostname (void)
static bool
netgroup_match (const char *group, const char *machine, const char *user)
{
static char *mydomain = (char *)0;
static char *mydomain = NULL;
if (mydomain == (char *)0) {
if (mydomain == NULL) {
static char domain[MAXHOSTNAMELEN + 1];
getdomainname (domain, MAXHOSTNAMELEN);
@@ -228,7 +228,7 @@ static bool user_match (const char *tok, const char *string)
&& from_match (at + 1, myhostname ()));
#if HAVE_INNETGR
} else if (tok[0] == '@') { /* netgroup */
return (netgroup_match (tok + 1, (char *) 0, string));
return (netgroup_match (tok + 1, NULL, string));
#endif
} else if (string_match (tok, string)) { /* ALL or exact match */
return true;
@@ -303,7 +303,7 @@ static bool from_match (const char *tok, const char *string)
*/
#if HAVE_INNETGR
if (tok[0] == '@') { /* netgroup */
return (netgroup_match (tok + 1, string, (char *) 0));
return (netgroup_match (tok + 1, string, NULL));
} else
#endif
if (string_match (tok, string)) { /* ALL or exact match */

View File

@@ -504,7 +504,7 @@ int main (int argc, char **argv)
if ((argc > 0) && (argv[0][0] == '-')) {
usage ();
goto failure;
} else if (argv[0] != (char *) 0) {
} else if (argv[0] != NULL) {
group = argv[0];
} else {
/*
@@ -740,7 +740,7 @@ int main (int argc, char **argv)
*/
if (cflag) {
closelog ();
execl (SHELL, "sh", "-c", command, (char *) 0);
execl (SHELL, "sh", "-c", command, (char *) NULL);
#ifdef WITH_AUDIT
snprintf (audit_buf, sizeof(audit_buf),
"changing new-gid=%lu", (unsigned long) gid);
@@ -820,7 +820,7 @@ int main (int argc, char **argv)
* Exec the login shell and go away. We are trying to get back to
* the previous environment which should be the user's login shell.
*/
err = shell (prog, initflag ? (char *) 0 : progbase, newenvp);
err = shell (prog, initflag ? NULL : progbase, newenvp);
exit ((err == ENOENT) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
/*@notreached@*/
failure:

View File

@@ -1088,7 +1088,7 @@ int main (int argc, char **argv)
* over 100 is allocated. The pw_gid field will be updated with that
* value.
*/
while (fgets (buf, (int) sizeof buf, stdin) != (char *) 0) {
while (fgets (buf, (int) sizeof buf, stdin) != NULL) {
line++;
cp = strrchr (buf, '\n');
if (NULL != cp) {

View File

@@ -812,7 +812,7 @@ static void check_spw_file (int *errors, bool *changed)
* Warn if last password change in the future. --marekm
*/
if (!quiet) {
time_t t = time ((time_t *) 0);
time_t t = time (NULL);
if ( (t != 0)
&& (spw->sp_lstchg > (long) t / SCALE)) {
printf (_("user %s: last password change in the future\n"),

View File

@@ -575,7 +575,7 @@ static void check_perms_nopam (const struct passwd *pw)
* The first character of an administrator defined method is an '@'
* character.
*/
if (pw_auth (password, name, PW_SU, (char *) 0) != 0) {
if (pw_auth (password, name, PW_SU, NULL) != 0) {
SYSLOG (((pw->pw_uid != 0)? LOG_NOTICE : LOG_WARN,
"Authentication failed for %s", name));
fprintf(stderr, _("%s: Authentication failure\n"), Prog);
@@ -598,7 +598,7 @@ static void check_perms_nopam (const struct passwd *pw)
* there is a "SU" entry in the /etc/porttime file denying access to
* the account.
*/
if (!isttytime (name, "SU", time ((time_t *) 0))) {
if (!isttytime (name, "SU", time (NULL))) {
SYSLOG (((0 != pw->pw_uid) ? LOG_WARN : LOG_CRIT,
"SU by %s to restricted account %s",
caller_name, name));
@@ -1130,7 +1130,7 @@ int main (int argc, char **argv)
int fd = open ("/dev/tty", O_RDWR);
if (fd >= 0) {
err = ioctl (fd, TIOCNOTTY, (char *) 0);
err = ioctl (fd, TIOCNOTTY, (char *) NULL);
(void) close (fd);
} else if (ENXIO == errno) {
/* There are no controlling terminal already */

View File

@@ -127,7 +127,7 @@ static void catch_signals (unused int sig)
while (true) { /* repeatedly get login/password pairs */
char *cp;
pw_entry (name, &pwent); /* get entry from password file */
if (pwent.pw_name == (char *) 0) {
if (pwent.pw_name == NULL) {
/*
* Fail secure
*/
@@ -155,7 +155,7 @@ static void catch_signals (unused int sig)
erase_pass (cp);
(void) puts ("");
#ifdef TELINIT
execl (PATH_TELINIT, "telinit", RUNLEVEL, (char *) 0);
execl (PATH_TELINIT, "telinit", RUNLEVEL, (char *) NULL);
#endif
exit (0);
}
@@ -177,7 +177,7 @@ static void catch_signals (unused int sig)
(void) puts (_("Entering System Maintenance Mode"));
/* exec the shell finally. */
err = shell (pwent.pw_shell, (char *) 0, environ);
err = shell (pwent.pw_shell, NULL, environ);
return ((err == ENOENT) ? E_CMD_NOTFOUND : E_CMD_NOEXEC);
}

View File

@@ -894,7 +894,7 @@ static int get_groups (char *list)
close_group_files ();
unlock_group_files ();
user_groups[ngroups] = (char *) 0;
user_groups[ngroups] = NULL;
/*
* Any errors in finding group names are fatal
@@ -2546,7 +2546,7 @@ int main (int argc, char **argv)
/*
* Initialize the list to be empty
*/
user_groups[0] = (char *) 0;
user_groups[0] = NULL;
is_shadow_pwd = spw_file_present ();

View File

@@ -763,7 +763,7 @@ static void user_cancel (const char *user)
}
argv[0] = cmd;
argv[1] = user;
argv[2] = (char *)0;
argv[2] = NULL;
(void) run_command (cmd, argv, NULL, &status);
}

View File

@@ -205,7 +205,7 @@ static int get_groups (char *list)
/*
* Initialize the list to be empty
*/
user_groups[0] = (char *) 0;
user_groups[0] = NULL;
if ('\0' == *list) {
return 0;
@@ -280,7 +280,7 @@ static int get_groups (char *list)
gr_free ((struct group *)grp);
} while (NULL != list);
user_groups[ngroups] = (char *) 0;
user_groups[ngroups] = NULL;
/*
* Any errors in finding group names are fatal
@@ -2154,7 +2154,7 @@ int main (int argc, char **argv)
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
user_groups = (char **) malloc (sizeof (char *) * (1 + sys_ngroups));
user_groups[0] = (char *) 0;
user_groups[0] = NULL;
is_shadow_pwd = spw_file_present ();
#ifdef SHADOWGRP