]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
capability-list: make capability_list_length() return unsigned
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 23 Oct 2025 13:38:35 +0000 (22:38 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 23 Oct 2025 14:46:49 +0000 (23:46 +0900)
src/basic/capability-list.c
src/basic/capability-list.h
src/nspawn/nspawn.c
src/test/test-capability-list.c

index 804dbcb112a8be5d3e969c047ff1e750764f1894..6c60888327c31046f46decb004f0b5ab11e04d46 100644 (file)
@@ -20,7 +20,7 @@ static const struct capability_name* lookup_capability(register const char *str,
 const char* capability_to_name(int id) {
         if (id < 0)
                 return NULL;
-        if (id >= capability_list_length())
+        if ((unsigned) id >= capability_list_length())
                 return NULL;
 
         return capability_names[id];
@@ -65,13 +65,13 @@ int capability_from_name(const char *name) {
         return sc->id;
 }
 
-/* This is the number of capability names we are *compiled* with.  For the max capability number of the
+/* This is the number of capability names we are *compiled* with. For the max capability number of the
  * currently-running kernel, use cap_last_cap(). Note that this one returns the size of the array, i.e. one
  * value larger than the last known capability. This is different from cap_last_cap() which returns the
  * highest supported capability. Hence with everyone agreeing on the same capabilities list, this function
  * will return one higher than cap_last_cap(). */
-int capability_list_length(void) {
-        return MIN((int) ELEMENTSOF(capability_names), CAP_LIMIT + 1);
+unsigned capability_list_length(void) {
+        return MIN((unsigned) ELEMENTSOF(capability_names), (unsigned) (CAP_LIMIT + 1));
 }
 
 int capability_set_to_string(uint64_t set, char **ret) {
index e588e87dee0ea6b92357e1f03c5cac63a398f4b1..2f73c74cdec75ec6a1687083af8627ad3de510d1 100644 (file)
@@ -13,7 +13,7 @@ const char* capability_to_string(int id, char buf[static CAPABILITY_TO_STRING_MA
 #define CAPABILITY_TO_STRING(id) capability_to_string(id, (char[CAPABILITY_TO_STRING_MAX]) {})
 
 int capability_from_name(const char *name);
-int capability_list_length(void);
+unsigned capability_list_length(void);
 
 int capability_set_to_string(uint64_t set, char **ret);
 int capability_set_to_string_negative(uint64_t set, char **ret);
index a47fcb22949c3e77f256ac20ccc46206ea55ebbc..bc1ed4c9eb5407d9bd68697e908e815484685701 100644 (file)
@@ -480,7 +480,7 @@ static int parse_capability_spec(const char *spec, uint64_t *ret_mask) {
                         break;
 
                 if (streq(t, "help")) {
-                        for (int i = 0; i < capability_list_length(); i++) {
+                        for (unsigned i = 0; i < capability_list_length(); i++) {
                                 const char *name;
 
                                 name = capability_to_name(i);
index c72c241683ac134864472cdfd9c91afb9e395f00..0f5d53276db5005a762aaecc8495d5a6759379f7 100644 (file)
@@ -28,12 +28,12 @@ TEST(cap_list) {
                 ASSERT_STREQ(CAPABILITY_TO_STRING(62), "0x3e");
         assert_se(!CAPABILITY_TO_STRING(64));
 
-        for (int i = 0; i < capability_list_length(); i++) {
+        for (unsigned i = 0; i < capability_list_length(); i++) {
                 const char *n;
 
-                assert_se(n = capability_to_name(i));
-                assert_se(capability_from_name(n) == i);
-                printf("%s = %i\n", n, i);
+                ASSERT_NOT_NULL(n = capability_to_name(i));
+                ASSERT_OK_EQ(capability_from_name(n), (int) i);
+                printf("%s = %u\n", n, i);
 
                 ASSERT_STREQ(CAPABILITY_TO_STRING(i), n);
         }
@@ -49,7 +49,7 @@ TEST(cap_list) {
         assert_se(capability_from_name("64") == -EINVAL);
         assert_se(capability_from_name("-1") == -EINVAL);
 
-        for (int i = 0; i < capability_list_length(); i++) {
+        for (unsigned i = 0; i < capability_list_length(); i++) {
                 _cleanup_(cap_free_charpp) char *a = NULL;
                 const char *b;
                 unsigned u;