Skip to content

Commit

Permalink
lib/, src/: Consistently use NULL with fgets(3)
Browse files Browse the repository at this point in the history
fgets(3) returns either NULL or the input pointer.  Checking for NULL is
more explicit, and simpler.

<stddef.h> is the header that provides NULL; add it where appropriate.

The meat of this patch can be approximated with the following semantic
patch:

	$ cat ~/tmp/spatch/fgets_null.sp
	@@
	expression a, b, c;
	@@

	- fgets(a, b, c) == a
	+ fgets(a, b, c) != NULL

	@@
	expression a, b, c;
	@@

	- fgetsx(a, b, c) == a
	+ fgetsx(a, b, c) != NULL

	@@
	expression a, b, c, p;
	@@

	- p->fgets(a, b, c) == a
	+ p->fgets(a, b, c) != NULL

	@@
	expression a, b, c;
	@@

	- fgets(a, b, c) != a
	+ fgets(a, b, c) == NULL

	@@
	expression a, b, c;
	@@

	- fgetsx(a, b, c) != a
	+ fgetsx(a, b, c) == NULL

	@@
	expression a, b, c, p;
	@@

	- p->fgets(a, b, c) != a
	+ p->fgets(a, b, c) == NUL

Applied as

	$ find contrib/ lib* src/ -type f \
	| xargs spatch --sp-file ~/tmp/spatch/fgets_null.sp --in-place;

The differences between the actual patch and the approximation via the
semantic patch from above are includes, whitespace, braces, and a case
where there was an implicit pointer-to-bool comparison which I made
explicit.  When reviewing, it'll be useful to use git-diff(1) with '-w'
and '--color-words=.'.

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Oct 15, 2024
1 parent ca165cc commit 8929e11
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 23 deletions.
3 changes: 2 additions & 1 deletion lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -641,7 +642,7 @@ int commonio_open (struct commonio_db *db, int mode)
if (NULL == buf)
goto cleanup_errno;

while (db->ops->fgets (buf, buflen, db->fp) == buf) {
while (db->ops->fgets(buf, buflen, db->fp) != NULL) {
struct commonio_entry *p;

while ( (strrchr (buf, '\n') == NULL)
Expand Down
6 changes: 3 additions & 3 deletions lib/fields.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
#ident "$Id$"

#include <ctype.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

#include "prototypes.h"
#include "string/strchr/stpspn.h"
Expand Down Expand Up @@ -79,9 +80,8 @@ change_field(char *buf, size_t maxsize, const char *prompt)

printf ("\t%s [%s]: ", prompt, buf);
(void) fflush (stdout);
if (fgets (newf, maxsize, stdin) != newf) {
if (fgets(newf, maxsize, stdin) == NULL)
return;
}

if (stpsep(newf, "\n") == NULL)
return;
Expand Down
5 changes: 3 additions & 2 deletions lib/fputsx.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <config.h>

#include <stddef.h>
#include <stdio.h>
#include <string.h>

Expand All @@ -25,9 +26,9 @@ fgetsx(/*@returned@*/char *restrict buf, int cnt, FILE *restrict f)
char *ep;

while (cnt > 0) {
if (fgets (cp, cnt, f) != cp) {
if (fgets(cp, cnt, f) == NULL) {
if (cp == buf) {
return 0;
return NULL;
} else {
break;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/getdate.y
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#endif

#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
Expand Down Expand Up @@ -933,7 +934,7 @@ main(void)
(void) fflush (stdout);

buff[MAX_BUFF_LEN] = 0;
while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
while (fgets(buff, MAX_BUFF_LEN, stdin) != NULL && buff[0])
{
d = get_date(buff, NULL);
if (d == -1)
Expand Down
3 changes: 1 addition & 2 deletions lib/gshadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ void endsgent (void)
buflen *= 2;

len = strlen (buf);
if (fgetsx(&buf[len], buflen - len, fp) != &buf[len]) {
if (fgetsx(&buf[len], buflen - len, fp) == NULL)
return NULL;
}
}
stpsep(buf, "\n");
return (sgetsgent (buf));
Expand Down
3 changes: 2 additions & 1 deletion lib/hushed.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ident "$Id$"

#include <pwd.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
Expand Down Expand Up @@ -72,7 +73,7 @@ bool hushed (const char *username)
if (NULL == fp) {
return false;
}
for (found = false; !found && (fgets(buf, sizeof(buf), fp) == buf);) {
for (found = false; !found && (fgets(buf, sizeof(buf), fp) != NULL);) {
stpsep(buf, "\n");
found = (strcmp (buf, pw->pw_shell) == 0) ||
(strcmp (buf, pw->pw_name) == 0);
Expand Down
6 changes: 3 additions & 3 deletions lib/loginprompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
#ident "$Id$"

#include <assert.h>
#include <stdio.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>

#include "attr.h"
#include "defines.h"
Expand Down Expand Up @@ -82,9 +83,8 @@ void login_prompt (char *name, int namesize)
*/

MEMZERO(buf);
if (fgets(buf, sizeof(buf), stdin) != buf) {
if (fgets(buf, sizeof(buf), stdin) == NULL)
exit (EXIT_FAILURE);
}

if (stpsep(buf, "\n") == NULL)
exit(EXIT_FAILURE);
Expand Down
3 changes: 2 additions & 1 deletion lib/setupenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <assert.h>
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
Expand Down Expand Up @@ -54,7 +55,7 @@ static void read_env_file (const char *filename)
if (NULL == fp) {
return;
}
while (fgets(buf, sizeof(buf), fp) == buf) {
while (fgets(buf, sizeof(buf), fp) != NULL) {
if (stpsep(buf, "\n") == NULL)
break;

Expand Down
3 changes: 2 additions & 1 deletion lib/ttytype.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#ident "$Id$"

#include <stddef.h>
#include <stdio.h>
#include <string.h>

Expand Down Expand Up @@ -45,7 +46,7 @@ void ttytype (const char *line)
perror (typefile);
return;
}
while (fgets(buf, sizeof(buf), fp) == buf) {
while (fgets(buf, sizeof(buf), fp) != NULL) {
if (buf[0] == '#') {
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/user_busy.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ident "$Id: $"

#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
Expand Down Expand Up @@ -124,7 +125,7 @@ static int check_status (const char *name, const char *sname, uid_t uid)
if (NULL == sfile) {
return 0;
}
while (fgets(line, sizeof(line), sfile) == line) {
while (fgets(line, sizeof(line), sfile) != NULL) {
if (strncmp (line, "Uid:\t", 5) == 0) {
unsigned long ruid, euid, suid;

Expand Down
2 changes: 1 addition & 1 deletion src/login_nopam.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ login_access(const char *user, const char *from)
if (NULL != fp) {
int lineno = 0; /* for diagnostics */
while ( !match
&& (fgets(line, sizeof(line), fp) == line))
&& (fgets(line, sizeof(line), fp) != NULL))
{
char *p;

Expand Down
13 changes: 7 additions & 6 deletions src/useradd.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
#include <getopt.h>
#include <grp.h>
#ifdef ENABLE_LASTLOG
#include <lastlog.h>
# include <lastlog.h>
#endif /* ENABLE_LASTLOG */
#include <libgen.h>
#include <pwd.h>
#include <signal.h>
#ifdef ACCT_TOOLS_SETUID
#ifdef USE_PAM
#include "pam_defs.h"
#endif /* USE_PAM */
# ifdef USE_PAM
# include "pam_defs.h"
# endif /* USE_PAM */
#endif /* ACCT_TOOLS_SETUID */
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -360,7 +361,7 @@ get_defaults(void)
* Read the file a line at a time. Only the lines that have relevant
* values are used, everything else can be ignored.
*/
while (fgets(buf, sizeof(buf), fp) == buf) {
while (fgets(buf, sizeof(buf), fp) != NULL) {
stpsep(buf, "\n");

cp = stpsep(buf, "=");
Expand Down Expand Up @@ -600,7 +601,7 @@ set_defaults(void)
goto skip;
}

while (fgets(buf, sizeof(buf), ifp) == buf) {
while (fgets(buf, sizeof(buf), ifp) != NULL) {
char *val;

if (stpsep(buf, "\n") == NULL) {
Expand Down

0 comments on commit 8929e11

Please sign in to comment.