Skip to content

Commit 8a8620e

Browse files
lib/, src/: Consistently use NULL with fgets(3)
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]>
1 parent 9f934b2 commit 8a8620e

File tree

12 files changed

+30
-23
lines changed

12 files changed

+30
-23
lines changed

lib/commonio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <fcntl.h>
1717
#include <limits.h>
1818
#include <signal.h>
19+
#include <stddef.h>
1920
#include <stdio.h>
2021
#include <string.h>
2122
#include <sys/stat.h>
@@ -641,7 +642,7 @@ int commonio_open (struct commonio_db *db, int mode)
641642
if (NULL == buf)
642643
goto cleanup_errno;
643644

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

647648
while ( (strrchr (buf, '\n') == NULL)

lib/fields.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
#ident "$Id$"
1313

1414
#include <ctype.h>
15-
#include <string.h>
15+
#include <stddef.h>
1616
#include <stdio.h>
17+
#include <string.h>
1718

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

8081
printf ("\t%s [%s]: ", prompt, buf);
8182
(void) fflush (stdout);
82-
if (fgets (newf, maxsize, stdin) != newf) {
83+
if (fgets(newf, maxsize, stdin) == NULL)
8384
return;
84-
}
8585

8686
if (stpsep(newf, "\n") == NULL)
8787
return;

lib/fputsx.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <config.h>
1111

12+
#include <stddef.h>
1213
#include <stdio.h>
1314
#include <string.h>
1415

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

2728
while (cnt > 0) {
28-
if (fgets (cp, cnt, f) != cp) {
29+
if (fgets(cp, cnt, f) == NULL) {
2930
if (cp == buf) {
30-
return 0;
31+
return NULL;
3132
} else {
3233
break;
3334
}

lib/getdate.y

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#endif
2626

2727
#include <ctype.h>
28+
#include <stddef.h>
2829
#include <stdio.h>
2930
#include <string.h>
3031
#include <time.h>
@@ -933,7 +934,7 @@ main(void)
933934
(void) fflush (stdout);
934935

935936
buff[MAX_BUFF_LEN] = 0;
936-
while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
937+
while (fgets(buff, MAX_BUFF_LEN, stdin) != NULL && buff[0])
937938
{
938939
d = get_date(buff, NULL);
939940
if (d == -1)

lib/gshadow.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,8 @@ void endsgent (void)
170170
buflen *= 2;
171171

172172
len = strlen (buf);
173-
if (fgetsx(&buf[len], buflen - len, fp) != &buf[len]) {
173+
if (fgetsx(&buf[len], buflen - len, fp) == NULL)
174174
return NULL;
175-
}
176175
}
177176
stpsep(buf, "\n");
178177
return (sgetsgent (buf));

lib/hushed.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ident "$Id$"
1414

1515
#include <pwd.h>
16+
#include <stddef.h>
1617
#include <stdio.h>
1718
#include <string.h>
1819
#include <sys/types.h>
@@ -72,7 +73,7 @@ bool hushed (const char *username)
7273
if (NULL == fp) {
7374
return false;
7475
}
75-
for (found = false; !found && (fgets(buf, sizeof(buf), fp) == buf);) {
76+
for (found = false; !found && (fgets(buf, sizeof(buf), fp) != NULL);) {
7677
stpsep(buf, "\n");
7778
found = (strcmp (buf, pw->pw_shell) == 0) ||
7879
(strcmp (buf, pw->pw_name) == 0);

lib/loginprompt.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
#ident "$Id$"
1313

1414
#include <assert.h>
15-
#include <stdio.h>
1615
#include <signal.h>
16+
#include <stddef.h>
17+
#include <stdio.h>
1718

1819
#include "attr.h"
1920
#include "defines.h"
@@ -83,9 +84,8 @@ login_prompt(char *name, int namesize)
8384
*/
8485

8586
MEMZERO(buf);
86-
if (fgets(buf, sizeof(buf), stdin) != buf) {
87+
if (fgets(buf, sizeof(buf), stdin) == NULL)
8788
exit (EXIT_FAILURE);
88-
}
8989

9090
if (stpsep(buf, "\n") == NULL)
9191
exit(EXIT_FAILURE);

lib/setupenv.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <assert.h>
1919
#include <ctype.h>
20+
#include <stddef.h>
2021
#include <stdio.h>
2122
#include <string.h>
2223
#include <sys/types.h>
@@ -54,7 +55,7 @@ static void read_env_file (const char *filename)
5455
if (NULL == fp) {
5556
return;
5657
}
57-
while (fgets(buf, sizeof(buf), fp) == buf) {
58+
while (fgets(buf, sizeof(buf), fp) != NULL) {
5859
if (stpsep(buf, "\n") == NULL)
5960
break;
6061

lib/ttytype.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#ident "$Id$"
1313

14+
#include <stddef.h>
1415
#include <stdio.h>
1516
#include <string.h>
1617

@@ -45,7 +46,7 @@ void ttytype (const char *line)
4546
perror (typefile);
4647
return;
4748
}
48-
while (fgets(buf, sizeof(buf), fp) == buf) {
49+
while (fgets(buf, sizeof(buf), fp) != NULL) {
4950
if (buf[0] == '#') {
5051
continue;
5152
}

lib/user_busy.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ident "$Id: $"
1313

1414
#include <assert.h>
15+
#include <stddef.h>
1516
#include <stdio.h>
1617
#include <sys/types.h>
1718
#include <dirent.h>
@@ -124,7 +125,7 @@ static int check_status (const char *name, const char *sname, uid_t uid)
124125
if (NULL == sfile) {
125126
return 0;
126127
}
127-
while (fgets(line, sizeof(line), sfile) == line) {
128+
while (fgets(line, sizeof(line), sfile) != NULL) {
128129
if (strncmp (line, "Uid:\t", 5) == 0) {
129130
unsigned long ruid, euid, suid;
130131

0 commit comments

Comments
 (0)