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->cio_fgets(a, b, c) == a
+ p->cio_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->cio_fgets(a, b, c) != a
+ p->cio_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 <alx@kernel.org>
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->cio_fgets(a, (int) (b), c)
+ p->cio_fgets(a, b, c)
@@
expression a, b, c, p;
@@
- p->cio_fgets(a, (int) b, c)
+ p->cio_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 <alx@kernel.org>
While the return value is a pointer, it can be interpreted as a boolean
value meaning "found". In general, we use explicit comparisons of
pointers to NULL, but in this specific case, let's use that
interpretation, and make an exception, using an implicit conversion to
boolean.
For negative matches, use
if (!strchr(...))
For positive matches, use
if (strchr(...))
For positive matches, when a variable is also set, use
while (NULL != (p = strchr(...)))
Signed-off-by: Alejandro Colomar <alx@kernel.org>
"config.h" is a locally generated header. It must be included as
'#include "config.h"'.
It is already included correctly in some sources files. This commit
unifies the way how "config.h" is included.
Signed-off-by: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
with the meaning "a character was found".
strpbrk(3) is just like strchr(3), but searches for multiple characters.
Both functions have a boolean-like return value, which evaluates to true
if a character was found.
A better name for strpbrk(3) would have been strchrs().
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Except for the added (and sorted) includes, the removal of redundant
parentheses, a few cases that have been refactored for readability, and
a couple of non-string cases that I've left out of the change, this
patch can be approximated with the following semantic patch:
$ cat ~/tmp/spatch/streq.sp
@@
expression s;
@@
- '\0' == *s
+ streq(s, "")
@@
expression s;
@@
- '\0' == s[0]
+ streq(s, "")
@@
expression s;
@@
- *s == '\0'
+ streq(s, "")
@@
expression s;
@@
- s[0] == '\0'
+ streq(s, "")
$ find contrib/ lib* src/ -type f \
| xargs spatch --in-place --sp-file ~/tmp/spatch/streq.sp;
Signed-off-by: Alejandro Colomar <alx@kernel.org>
asprintf(3) is non-standard, but is provided by GNU, the BSDs, and musl.
That makes it portable enough for us to use.
This function is much simpler than the burdensome code for allocating
the right size. Being simpler, it's thus safer.
I took the opportunity to fix the style to my preferred one in the
definitions of variables used in these calls, and also in the calls to
free(3) with these pointers. That isn't gratuituous, but has a reason:
it makes those appear in the diff for this patch, which helps review it.
Oh, well, I had an excuse :)
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
The separation was unnecessary, and caused build problems. Let's go
wild and obliterate the library. The files are moved to libshadow.
Scripted change:
$ find libmisc/ -type f \
| grep '\.[chy]$' \
| xargs mv -t lib;
Plus updating the Makefile and other references. While at it, I've
sorted the sources lists.
Link: <https://github.com/shadow-maint/shadow/pull/792>
Reported-by: David Seifert <soap@gentoo.org>
Cc: Sam James <sam@gentoo.org>
Cc: Christian Bricart <christian@bricart.de>
Cc: Michael Vetter <jubalh@iodoru.org>
Cc: Robert Förster <Dessa@gmake.de>
[ soap tested the Gentoo package ]
Tested-by: David Seifert <soap@gentoo.org>
Acked-by: David Seifert <soap@gentoo.org>
Acked-by: Serge Hallyn <serge@hallyn.com>
Acked-by: Iker Pedrosa <ipedrosa@redhat.com>
Acked-by: <lslebodn@fedoraproject.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>