1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-30 23:21:22 +03:00

Return -EINVAL from _from_string() functions

We'd return -1 (-EPERM), even though we have a general rule to use real errno
values. The particular case that caught my attention was:

$ sudo udevadm control -l asdf
Failed to parse log priority 'asdf': Operation not permitted

... but "git grep 'r =.*_from_string' src/" return 110 hits. Confusingly, some
of the _from_string functions already return a proper errno value, so not all
of those are broken, but probably quite a few.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-01-18 16:26:31 +01:00
parent 2d93c20e5f
commit 751db3b4cd
5 changed files with 20 additions and 18 deletions

View File

@ -5,11 +5,11 @@
ssize_t string_table_lookup(const char * const *table, size_t len, const char *key) {
if (!key)
return -1;
return -EINVAL;
for (size_t i = 0; i < len; ++i)
if (streq_ptr(table[i], key))
return (ssize_t) i;
return -1;
return -EINVAL;
}

View File

@ -29,7 +29,7 @@ ssize_t string_table_lookup(const char * const *table, size_t len, const char *k
#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
scope type name##_from_string(const char *s) { \
if (!s) \
return -1; \
return -EINVAL; \
int b = parse_boolean(s); \
if (b == 0) \
return (type) 0; \
@ -60,14 +60,16 @@ ssize_t string_table_lookup(const char * const *table, size_t len, const char *k
unsigned u = 0; \
type i; \
if (!s) \
return (type) -1; \
return -EINVAL; \
i = (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
if (i >= 0) \
return i; \
if (safe_atou(s, &u) >= 0 && u <= max) \
return (type) u; \
return (type) -1; \
} \
if (safe_atou(s, &u) < 0) \
return -EINVAL; \
if (u > max) \
return -EINVAL; \
return (type) u; \
}
#define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
_DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \

View File

@ -33,11 +33,11 @@ static inline void _test_table(const char *name,
if (i >= 0 && i < size) {
if (sparse)
assert_se(rev == i || rev == -1);
assert_se(rev == i || rev == -EINVAL);
else
assert_se(val != NULL && rev == i);
} else
assert_se(val == NULL && rev == -1);
assert_se(val == NULL && rev == -EINVAL);
}
}

View File

@ -598,8 +598,8 @@ static void test_ioprio_class_from_to_string(void) {
test_ioprio_class_from_to_string_one("1", 1);
test_ioprio_class_from_to_string_one("7", 7);
test_ioprio_class_from_to_string_one("8", 8);
test_ioprio_class_from_to_string_one("9", -1);
test_ioprio_class_from_to_string_one("-1", -1);
test_ioprio_class_from_to_string_one("9", -EINVAL);
test_ioprio_class_from_to_string_one("-1", -EINVAL);
}
static void test_setpriority_closest(void) {

View File

@ -53,16 +53,16 @@ int main(int argc, char *argv[]) {
assert_se(setrlimit(RLIMIT_NOFILE, &new) >= 0);
assert_se(rlimit_from_string("NOFILE") == RLIMIT_NOFILE);
assert_se(rlimit_from_string("LimitNOFILE") == -1);
assert_se(rlimit_from_string("RLIMIT_NOFILE") == -1);
assert_se(rlimit_from_string("xxxNOFILE") == -1);
assert_se(rlimit_from_string("DefaultLimitNOFILE") == -1);
assert_se(rlimit_from_string("LimitNOFILE") == -EINVAL);
assert_se(rlimit_from_string("RLIMIT_NOFILE") == -EINVAL);
assert_se(rlimit_from_string("xxxNOFILE") == -EINVAL);
assert_se(rlimit_from_string("DefaultLimitNOFILE") == -EINVAL);
assert_se(rlimit_from_string_harder("NOFILE") == RLIMIT_NOFILE);
assert_se(rlimit_from_string_harder("LimitNOFILE") == RLIMIT_NOFILE);
assert_se(rlimit_from_string_harder("RLIMIT_NOFILE") == RLIMIT_NOFILE);
assert_se(rlimit_from_string_harder("xxxNOFILE") == -1);
assert_se(rlimit_from_string_harder("DefaultLimitNOFILE") == -1);
assert_se(rlimit_from_string_harder("xxxNOFILE") == -EINVAL);
assert_se(rlimit_from_string_harder("DefaultLimitNOFILE") == -EINVAL);
for (i = 0; i < _RLIMIT_MAX; i++) {
_cleanup_free_ char *prefixed = NULL;