diff --git a/src/basic/alloc-util.c b/src/basic/alloc-util.c index 2584cb07fce..cdde4f28599 100644 --- a/src/basic/alloc-util.c +++ b/src/basic/alloc-util.c @@ -38,7 +38,7 @@ void* memdup(const void *p, size_t l) { return ret; } -void* memdup_suffix0(const void*p, size_t l) { +void* memdup_suffix0(const void *p, size_t l) { void *ret; assert(l == 0 || p); diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index 38d1546b551..02dee37d36d 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -55,7 +55,7 @@ static inline void *mfree(void *memory) { }) void* memdup(const void *p, size_t l) _alloc_(2); -void* memdup_suffix0(const void*p, size_t l) _alloc_(2); +void* memdup_suffix0(const void *p, size_t l) _alloc_(2); static inline void freep(void *p) { free(*(void**) p); diff --git a/src/basic/cap-list.c b/src/basic/cap-list.c index 52b2ce800fd..c4557666efd 100644 --- a/src/basic/cap-list.c +++ b/src/basic/cap-list.c @@ -54,8 +54,12 @@ int capability_from_name(const char *name) { /* Try to parse numeric capability */ r = safe_atoi(name, &i); - if (r >= 0 && i >= 0) - return i; + if (r >= 0) { + if (i >= 0 && i < (int) ELEMENTSOF(capability_names)) + return i; + else + return -EINVAL; + } /* Try to parse string capability */ sc = lookup_capability(name, strlen(name)); diff --git a/src/test/test-af-list.c b/src/test/test-af-list.c index 69345492871..2d42dc2c54b 100644 --- a/src/test/test-af-list.c +++ b/src/test/test-af-list.c @@ -46,6 +46,7 @@ int main(int argc, const char *argv[]) { assert_se(af_to_name(af_max()) == NULL); assert_se(af_to_name(-1) == NULL); assert_se(af_from_name("huddlduddl") == AF_UNSPEC); + assert_se(af_from_name("") == AF_UNSPEC); return 0; } diff --git a/src/test/test-alloc-util.c b/src/test/test-alloc-util.c index 7e779ac5611..ed598e106c3 100644 --- a/src/test/test-alloc-util.c +++ b/src/test/test-alloc-util.c @@ -18,6 +18,8 @@ along with systemd; If not, see . ***/ +#include + #include "alloc-util.h" #include "macro.h" #include "util.h" @@ -35,22 +37,42 @@ static void test_alloca(void) { assert_se(!memcmp(t, zero, 997)); } -static void test_memdup_multiply(void) { +static void test_memdup_multiply_and_greedy_realloc(void) { int org[] = {1, 2, 3}; - int *dup; - - dup = (int*)memdup_multiply(org, sizeof(int), 3); + _cleanup_free_ int *dup; + int *p; + size_t i, allocated = 3; + dup = (int*) memdup_suffix0_multiply(org, sizeof(int), 3); assert_se(dup); assert_se(dup[0] == 1); assert_se(dup[1] == 2); assert_se(dup[2] == 3); + assert_se(*(uint8_t*) (dup + 3) == (uint8_t) 0); free(dup); + + dup = (int*) memdup_multiply(org, sizeof(int), 3); + assert_se(dup); + assert_se(dup[0] == 1); + assert_se(dup[1] == 2); + assert_se(dup[2] == 3); + + p = dup; + assert_se(greedy_realloc0((void**) &dup, &allocated, 2, sizeof(int)) == p); + + p = (int *) greedy_realloc0((void**) &dup, &allocated, 10, sizeof(int)); + assert_se(p == dup); + assert_se(allocated >= 10); + assert_se(p[0] == 1); + assert_se(p[1] == 2); + assert_se(p[2] == 3); + for (i = 3; i < allocated; i++) + assert_se(p[i] == 0); } int main(int argc, char *argv[]) { test_alloca(); - test_memdup_multiply(); + test_memdup_multiply_and_greedy_realloc(); return 0; } diff --git a/src/test/test-architecture.c b/src/test/test-architecture.c index e81799539d5..586d54b1409 100644 --- a/src/test/test-architecture.c +++ b/src/test/test-architecture.c @@ -25,6 +25,14 @@ int main(int argc, char *argv[]) { int a, v; + const char *p; + + assert_se(architecture_from_string("") < 0); + assert_se(architecture_from_string(NULL) < 0); + assert_se(architecture_from_string("hoge") < 0); + assert_se(architecture_to_string(-1) == NULL); + assert_se(architecture_from_string(architecture_to_string(0)) == 0); + assert_se(architecture_from_string(architecture_to_string(1)) == 1); v = detect_virtualization(); if (IN_SET(v, -EPERM, -EACCES)) @@ -40,12 +48,18 @@ int main(int argc, char *argv[]) { a = uname_architecture(); assert_se(a >= 0); - log_info("uname architecture=%s", architecture_to_string(a)); + p = architecture_to_string(a); + assert_se(p); + log_info("uname architecture=%s", p); + assert_se(architecture_from_string(p) == a); a = native_architecture(); assert_se(a >= 0); - log_info("native architecture=%s", architecture_to_string(a)); + p = architecture_to_string(a); + assert_se(p); + log_info("native architecture=%s", p); + assert_se(architecture_from_string(p) == a); log_info("primary library architecture=" LIB_ARCH_TUPLE); diff --git a/src/test/test-arphrd-list.c b/src/test/test-arphrd-list.c index 2949a8aae0e..e5035828209 100644 --- a/src/test/test-arphrd-list.c +++ b/src/test/test-arphrd-list.c @@ -46,6 +46,7 @@ int main(int argc, const char *argv[]) { assert_se(arphrd_to_name(arphrd_max()) == NULL); assert_se(arphrd_to_name(0) == NULL); assert_se(arphrd_from_name("huddlduddl") == 0); + assert_se(arphrd_from_name("") == 0); return 0; } diff --git a/src/test/test-bitmap.c b/src/test/test-bitmap.c index 6bc88a45664..fa380561264 100644 --- a/src/test/test-bitmap.c +++ b/src/test/test-bitmap.c @@ -97,8 +97,19 @@ int main(int argc, const char *argv[]) { assert_se(i == (unsigned) -1); + b2 = bitmap_copy(b); + assert_se(b2); + assert_se(bitmap_equal(b, b2) == true); + assert_se(bitmap_equal(b, b) == true); + assert_se(bitmap_equal(b, NULL) == false); + assert_se(bitmap_equal(NULL, b) == false); + assert_se(bitmap_equal(NULL, NULL) == true); + bitmap_clear(b); assert_se(bitmap_isclear(b) == true); + assert_se(bitmap_equal(b, b2) == false); + bitmap_free(b2); + b2 = NULL; assert_se(bitmap_set(b, (unsigned) -1) == -ERANGE); diff --git a/src/test/test-cap-list.c b/src/test/test-cap-list.c index 9c82e618f5e..935567cc233 100644 --- a/src/test/test-cap-list.c +++ b/src/test/test-cap-list.c @@ -23,7 +23,6 @@ #include "alloc-util.h" #include "cap-list.h" #include "capability-util.h" -#include "fileio.h" #include "parse-util.h" #include "string-util.h" #include "util.h" @@ -71,57 +70,72 @@ static void test_cap_list(void) { } } -/* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */ -static void test_last_cap_file(void) { - _cleanup_free_ char *content = NULL; - unsigned long val = 0; - int r; +static void test_capability_set_one(uint64_t c, const char *t) { + _cleanup_free_ char *t1 = NULL; + uint64_t c1, c_masked = c & ((UINT64_C(1) << capability_list_length()) - 1); - r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content); - assert_se(r >= 0); + assert_se(capability_set_to_string_alloc(c, &t1) == 0); + assert_se(streq(t1, t)); - r = safe_atolu(content, &val); - assert_se(r >= 0); - assert_se(val != 0); - assert_se(val == cap_last_cap()); + assert_se(capability_set_from_string(t1, &c1) == 0); + assert_se(c1 == c_masked); + + free(t1); + assert_se(t1 = strjoin("'cap_chown cap_dac_override' \"cap_setgid cap_setuid\"", t, + " hogehoge foobar 12345 3.14 -3 ", t)); + assert_se(capability_set_from_string(t1, &c1) == 0); + assert_se(c1 == c_masked); } -/* verify cap_last_cap() against syscall probing */ -static void test_last_cap_probe(void) { - unsigned long p = (unsigned long)CAP_LAST_CAP; +static void test_capability_set(void) { + uint64_t c; - if (prctl(PR_CAPBSET_READ, p) < 0) { - for (p--; p > 0; p --) - if (prctl(PR_CAPBSET_READ, p) >= 0) - break; - } else { - for (;; p++) - if (prctl(PR_CAPBSET_READ, p+1) < 0) - break; - } + assert_se(capability_set_from_string(NULL, &c) == 0); + assert_se(c == 0); - assert_se(p != 0); - assert_se(p == cap_last_cap()); -} + assert_se(capability_set_from_string("", &c) == 0); + assert_se(c == 0); -static void test_capability_set_to_string_alloc(void) { - _cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL; + assert_se(capability_set_from_string("0", &c) == 0); + assert_se(c == UINT64_C(1)); - assert_se(capability_set_to_string_alloc(0u, &t1) == 0); - assert_se(streq(t1, "")); + assert_se(capability_set_from_string("1", &c) == 0); + assert_se(c == UINT64_C(1) << 1); - assert_se(capability_set_to_string_alloc(1u< #include +#include "alloc-util.h" #include "capability-util.h" #include "fd-util.h" +#include "fileio.h" #include "macro.h" +#include "parse-util.h" #include "util.h" static uid_t test_uid = -1; @@ -37,6 +40,39 @@ static gid_t test_gid = -1; /* We keep CAP_DAC_OVERRIDE to avoid errors with gcov when doing test coverage */ static uint64_t test_flags = 1ULL << CAP_DAC_OVERRIDE; +/* verify cap_last_cap() against /proc/sys/kernel/cap_last_cap */ +static void test_last_cap_file(void) { + _cleanup_free_ char *content = NULL; + unsigned long val = 0; + int r; + + r = read_one_line_file("/proc/sys/kernel/cap_last_cap", &content); + assert_se(r >= 0); + + r = safe_atolu(content, &val); + assert_se(r >= 0); + assert_se(val != 0); + assert_se(val == cap_last_cap()); +} + +/* verify cap_last_cap() against syscall probing */ +static void test_last_cap_probe(void) { + unsigned long p = (unsigned long)CAP_LAST_CAP; + + if (prctl(PR_CAPBSET_READ, p) < 0) { + for (p--; p > 0; p --) + if (prctl(PR_CAPBSET_READ, p) >= 0) + break; + } else { + for (;; p++) + if (prctl(PR_CAPBSET_READ, p+1) < 0) + break; + } + + assert_se(p != 0); + assert_se(p == cap_last_cap()); +} + static void fork_test(void (*test_func)(void)) { pid_t pid = 0; @@ -203,6 +239,9 @@ int main(int argc, char *argv[]) { int r; bool run_ambient; + test_last_cap_file(); + test_last_cap_probe(); + log_parse_environment(); log_open();