]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
lib/string_helpers: annotate struct strarray with __counted_by_ptr
authorThorsten Blum <thorsten.blum@linux.dev>
Wed, 15 Apr 2026 12:25:45 +0000 (14:25 +0200)
committerKees Cook <kees@kernel.org>
Wed, 13 May 2026 17:54:08 +0000 (10:54 -0700)
Add the __counted_by_ptr() compiler attribute to 'array' to improve
bounds checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Link: https://patch.msgid.link/20260415122542.370926-6-thorsten.blum@linux.dev
Signed-off-by: Kees Cook <kees@kernel.org>
lib/string_helpers.c

index 6a8db441b6fd173f87098e5b466c64397642e6dd..98d6ed0eaab7e9f3d54e420c404629a28b925bf4 100644 (file)
@@ -805,7 +805,7 @@ void kfree_strarray(char **array, size_t n)
 EXPORT_SYMBOL_GPL(kfree_strarray);
 
 struct strarray {
-       char **array;
+       char **array __counted_by_ptr(n);
        size_t n;
 };
 
@@ -824,13 +824,13 @@ char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n)
        if (!ptr)
                return ERR_PTR(-ENOMEM);
 
+       ptr->n = n;
        ptr->array = kasprintf_strarray(GFP_KERNEL, prefix, n);
        if (!ptr->array) {
                devres_free(ptr);
                return ERR_PTR(-ENOMEM);
        }
 
-       ptr->n = n;
        devres_add(dev, ptr);
 
        return ptr->array;