Skip to content

Conversation

@alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Dec 6, 2025

Cc: @kees


Revisions:

v1b
  • Rebase
$ git rd 
1:  010f20f53 = 1:  15e0d871f lib/, src/: Use countof() instead of sizeof() with fgets(3)
2:  e04a90503 = 2:  d986b5e82 lib/fields.c: change_field(): Use a VLA
3:  3a95ddaed ! 3:  a6444d771 lib/: Use countof() instead of hardcoded lengths
    @@ lib/limits.c
      #include "string/strcmp/strprefix.h"
     @@ lib/limits.c: static int setup_user_limits (const char *uname)
         *
    -    * FIXME: A better (smarter) checking should be done
    +    * FIXME: a better (smarter) checking should be done
         */
     -  while (fgets (buf, 1024, fil) != NULL) {
     +  while (fgets(buf, countof(buf), fil) != NULL) {
4:  e46110570 = 4:  a5f2be58d lib/io/: fgets_a(): Add macro
5:  6cff3df10 ! 5:  9ad8d91f4 lib/, src/: Use fgets_a() instead of its pattern
    @@ lib/limits.c
      #include "string/strcmp/strprefix.h"
     @@ lib/limits.c: static int setup_user_limits (const char *uname)
         *
    -    * FIXME: A better (smarter) checking should be done
    +    * FIXME: a better (smarter) checking should be done
         */
     -  while (fgets(buf, countof(buf), fil) != NULL) {
     +  while (fgets_a(buf, fil) != NULL) {

@alejandro-colomar alejandro-colomar changed the title Use countof() instead of sizeof() with fgets(3) Add fgets_a(), and use it instead of its pattern Dec 6, 2025
@alejandro-colomar alejandro-colomar force-pushed the fgc branch 7 times, most recently from 384138a to 6cff3df Compare December 6, 2025 20:57
@alejandro-colomar alejandro-colomar self-assigned this Dec 11, 2025
Except for the added includes, this patch has been scripted with a
semantic patch:

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

	- fgets(a, sizeof(b), c)
	+ fgets(a, countof(b), c)

	@@
	expression a, b, c;
	@@

	- fgets(a, sizeof b, c)
	+ fgets(a, countof(b), c)

Applied as:

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

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This allows using countof(), which is safer than a decoupled use of
the variable containing the length of the array.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Except for the updated includes, this patch has been scripted with a
semantic patch:

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

	- fgets(a, countof(a), b)
	+ fgets_a(a, b)

Applied as:

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

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant