From 12e20085f59d50add0eab0191159173f80708e6e Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 3 May 2024 14:34:17 +0300
Subject: [PATCH 44/44] Add nonnull attributes for capability.[ch] functions

See RM #531

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 utility/capability.c | 22 ++++++++++++----------
 utility/capability.h | 10 ++++++----
 2 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/utility/capability.c b/utility/capability.c
index 6d434f50b9..07513ef7bd 100644
--- a/utility/capability.c
+++ b/utility/capability.c
@@ -20,21 +20,25 @@
 #include <string.h>
 
 /* utility */
-#include "shared.h"		/* TRUE, FALSE */
-#include "support.h"		/* fc_is* */
+#include "shared.h"             /* TRUE, FALSE */
+#include "support.h"            /* fc_is* */
 
 #include "capability.h"
 
+static bool fc_has_capability(const char *cap, const char *capstr,
+                              const size_t cap_len)
+  fc__attribute((nonnull (1, 2)));
+
 #define GET_TOKEN(start, end)                                               \
   {                                                                         \
-    /* skip leading whitespace */                                           \
+    /* Skip leading whitespace */                                           \
     while (fc_isspace(*start)) {                                            \
       start++;                                                              \
     }                                                                       \
-    /* skip to end of token */                                              \
+    /* Skip to end of token */                                              \
     for (end = start; *end != '\0' && !fc_isspace(*end) && *end != ',';     \
          end++) {                                                           \
-      /* nothing */                                                         \
+      /* Nothing */                                                         \
     }                                                                       \
   }
 
@@ -48,8 +52,6 @@ static bool fc_has_capability(const char *cap, const char *capstr,
 {
   const char *next;
 
-  fc_assert_ret_val(capstr != NULL, FALSE);
-
   for (;;) {
     GET_TOKEN(capstr, next);
 
@@ -72,7 +74,7 @@ static bool fc_has_capability(const char *cap, const char *capstr,
 }
 
 /***********************************************************************//**
-  Wrapper for fc_has_capability() for NULL terminated strings.
+  Wrapper for fc_has_capability() for nullptr terminated strings.
 ***************************************************************************/
 bool has_capability(const char *cap, const char *capstr)
 {
@@ -90,13 +92,13 @@ bool has_capabilities(const char *us, const char *them)
   for (;;) {
     GET_TOKEN(us, next);
 
-    if (*us == '+' && !fc_has_capability(us+1, them, next-(us+1))) {
+    if (*us == '+' && !fc_has_capability(us + 1, them, next - (us + 1))) {
       return FALSE;
     }
     if (*next == '\0') {
       return TRUE;
     }
 
-    us = next+1;
+    us = next + 1;
   }
 }
diff --git a/utility/capability.h b/utility/capability.h
index 26211038e5..79b4c74c4a 100644
--- a/utility/capability.h
+++ b/utility/capability.h
@@ -1,4 +1,4 @@
-/********************************************************************** 
+/***********************************************************************
  Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -20,11 +20,13 @@ extern "C" {
 
 #include "support.h"            /* bool type */
 
-bool has_capability(const char *cap, const char *capstr);
-bool has_capabilities(const char *us, const char *them);
+bool has_capability(const char *cap, const char *capstr)
+  fc__attribute((nonnull (1, 2)));
+bool has_capabilities(const char *us, const char *them)
+  fc__attribute((nonnull (1, 2)));
 
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
 
-#endif  /* FC__CAPABILITY_H */
+#endif /* FC__CAPABILITY_H */
-- 
2.43.0

