From b52a3044aa62c80bf3abcd5caef4a69c2ee94622 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Thu, 24 Oct 2024 18:18:47 +0200 Subject: [PATCH] parser: Use counted_by attribute if supported We only have a single struct with a flexible array member. --- libxml.h | 7 +++++++ parser.c | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/libxml.h b/libxml.h index 6065d822..ee3d57c7 100644 --- a/libxml.h +++ b/libxml.h @@ -57,6 +57,13 @@ #define ATTRIBUTE_DESTRUCTOR __attribute__((destructor)) #endif +#if (defined(__clang__) && __clang_major__ >= 18) || \ + (defined(__GNUC__) && __GNUC__ >= 15) + #define ATTRIBUTE_COUNTED_BY(c) __attribute__((__counted_by__(c))) +#else + #define ATTRIBUTE_COUNTED_BY(c) +#endif + #if defined(__clang__) || \ (defined(__GNUC__) && (__GNUC__ >= 8) && !defined(__EDG__)) #define ATTRIBUTE_NO_SANITIZE(arg) __attribute__((no_sanitize(arg))) diff --git a/parser.c b/parser.c index 945e80b0..9c5a02e0 100644 --- a/parser.c +++ b/parser.c @@ -993,7 +993,7 @@ struct _xmlDefAttrs { int maxAttrs; /* the size of the array */ #if __STDC_VERSION__ >= 199901L /* Using a C99 flexible array member avoids UBSan errors. */ - xmlDefAttr attrs[]; /* array of localname/prefix/values/external */ + xmlDefAttr attrs[] ATTRIBUTE_COUNTED_BY(maxAttrs); #else xmlDefAttr attrs[1]; #endif