Skip to content

Commit

Permalink
lib/, src/: Remove useless casts in fgets(3)
Browse files Browse the repository at this point in the history
This patch can be replicated with the following semantic patch:

	$ cat fgets_cast.sp
	@@
	expression a, b, c;
	@@

	- fgets(a, (int) (b), c)
	+ fgets(a, b, c)

	@@
	expression a, b, c;
	@@

	- fgets(a, (int) b, c)
	+ fgets(a, b, c)

	@@
	expression a, b, c;
	@@

	- fgetsx(a, (int) (b), c)
	+ fgetsx(a, b, c)

	@@
	expression a, b, c;
	@@

	- fgetsx(a, (int) b, c)
	+ fgetsx(a, b, c)

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

	- p->fgets(a, (int) (b), c)
	+ p->fgets(a, b, c)

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

	- p->fgets(a, (int) b, c)
	+ p->fgets(a, b, c)

which is applied as:

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

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Oct 15, 2024
1 parent 61bc9a0 commit ca165cc
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 8 deletions.
4 changes: 1 addition & 3 deletions lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,7 @@ int commonio_open (struct commonio_db *db, int mode)
goto cleanup_errno;

len = strlen (buf);
if (db->ops->fgets (buf + len,
(int) (buflen - len),
db->fp) == NULL) {
if (db->ops->fgets(buf + len, buflen - len, db->fp) == NULL) {
goto cleanup_buf;
}
}
Expand Down
4 changes: 1 addition & 3 deletions lib/gshadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ void endsgent (void)
buflen *= 2;

len = strlen (buf);
if (fgetsx (&buf[len],
(int) (buflen - len),
fp) != &buf[len]) {
if (fgetsx(&buf[len], buflen - len, fp) != &buf[len]) {
return NULL;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/setupenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void read_env_file (const char *filename)
if (NULL == fp) {
return;
}
while (fgets(buf, (int) sizeof(buf), fp) == buf) {
while (fgets(buf, sizeof(buf), fp) == buf) {
if (stpsep(buf, "\n") == NULL)
break;

Expand Down
2 changes: 1 addition & 1 deletion src/chgpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,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) != NULL) {
while (fgets(buf, sizeof(buf), stdin) != NULL) {
line++;
if (stpsep(buf, "\n") == NULL) {
fprintf (stderr, _("%s: line %d: line too long\n"),
Expand Down

0 comments on commit ca165cc

Please sign in to comment.