lib: Fix a few bugs in trailing_strtoln()
At present this has a minor bug in that it reads the byte before the start of the string, if it is empty. Also it doesn't handle a non-numeric prefix which is only one character long. Fix these bugs with a reworked implementation. Add a test for the second case. The first one is hard to test. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
11
lib/strto.c
11
lib/strto.c
@@ -189,11 +189,12 @@ long trailing_strtoln(const char *str, const char *end)
|
||||
|
||||
if (!end)
|
||||
end = str + strlen(str);
|
||||
if (isdigit(end[-1])) {
|
||||
for (p = end - 1; p > str; p--) {
|
||||
if (!isdigit(*p))
|
||||
return dectoul(p + 1, NULL);
|
||||
}
|
||||
p = end - 1;
|
||||
if (p > str && isdigit(*p)) {
|
||||
do {
|
||||
if (!isdigit(p[-1]))
|
||||
return dectoul(p, NULL);
|
||||
} while (--p > str);
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user