Replace one sscanf(3) call by conventional string parsing#1408
Open
alejandro-colomar wants to merge 5 commits intoshadow-maint:masterfrom
Open
Replace one sscanf(3) call by conventional string parsing#1408alejandro-colomar wants to merge 5 commits intoshadow-maint:masterfrom
alejandro-colomar wants to merge 5 commits intoshadow-maint:masterfrom
Conversation
a44624a to
67f545f
Compare
99792bc to
fb74180
Compare
fb74180 to
dfba6c4
Compare
Collaborator
Author
|
Cc: @kees |
f5d0a89 to
a9e0003
Compare
a9e0003 to
9f7660e
Compare
We only use them in the loop, and we write to them unconditionally in every iteration with sscanf(3), so we can reduce their lifetime, and skip the superfluous zeroing. Signed-off-by: Alejandro Colomar <alx@kernel.org>
And reject incomplete lines. Signed-off-by: Alejandro Colomar <alx@kernel.org>
sscanf(3) is quite brittle. Let's parse with regular string functions,
which are much more robust. This should silence a CodeQL false
positive, BTW.
This new parsing has a slight difference with the sscanf(3) call: the
first whitespace after the name is turned into a null delimiter byte,
while sscanf(3) was keeping it as part of the second string. However,
since we later skip leading white space --within do_user_limits()--,
this is irrelevant.
I've tested this patch with the following program:
$ cat sscanf.c
#include <err.h>
#include <stdio.h>
#include <string.h>
extern inline char *
stpsep(char *s, const char *delim)
{
strsep(&s, delim);
return s;
}
#define stpspn(s, accept) \
({ \
auto s_ = s; \
\
s_ + strspn(s_, accept); \
})
int
main(void)
{
char buf[1000];
char s1[1000] = "";
char s2[1000] = "";
char *p1, *p2;
int n;
if (fgets(buf, 1000, stdin) == NULL)
err(1, "fgets");
n = sscanf(buf, "%s%[ACDFIKLMNOPRSTUacdfiklmnoprstu0-9 \t-]", s1, s2);
if (n != 2)
warn("sscanf: %d", n);
printf("--- s1: |%s|\n", s1);
printf("--- s2: |%s|\n", s2);
if (stpsep(buf, "\n") == NULL)
warn("stpsep(\"\n\")");
p1 = buf;
p2 = stpsep(buf, " \t");
if (p2 == NULL)
warn("stpsep");
else
stpcpy(stpspn(p2, "ACDFIKLMNOPRSTUacdfiklmnoprstu0123456789 \t-"), "");
printf("+++ s1: |%s|\n", p1);
printf("+++ s2: |%s|\n", p2);
}
alx@devuan:~/tmp$ gcc -Wall -Wextra sscanf.c
alx@devuan:~/tmp$ ./a.out
qwe
a.out: sscanf: 1: Success
--- s1: |qwe|
--- s2: ||
a.out: stpsep: Success
+++ s1: |qwe|
+++ s2: |(null)|
alx@devuan:~/tmp$ ./a.out
qwe qwe
--- s1: |qwe|
--- s2: | |
+++ s1: |qwe|
+++ s2: ||
alx@devuan:~/tmp$ ./a.out
qwe acd acd 123456
--- s1: |qwe|
--- s2: | acd acd 123456|
+++ s1: |qwe|
+++ s2: |acd acd 123456|
alx@devuan:~/tmp$ ./a.out
qwe acd123 qwe
--- s1: |qwe|
--- s2: | acd123 |
+++ s1: |qwe|
+++ s2: |acd123 |
Interestingly, this patch makes it more obvious that we're silently
ignoring garbage after the limit. Should we be more strict in what we
parse? That'll be for another day.
BTW, we've removed a conditional, but keep the code indented. That will
keep this patch small. The next commit will fix that indentation.
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
We overwrite this in fgets(3), and even if it were to fail in the first call, we don't use the buffer after that, so the zeroing is superfluous. Signed-off-by: Alejandro Colomar <alx@kernel.org>
9f7660e to
12001dc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Revisions:
v1b
v1c
v1d
v2
v2b
v2c