Collin Funk <[email protected]> writes:

> Bruno Haible <[email protected]> writes:
>
>> Right. It sounds like one needs to define this macro as
>>
>>   #  define _GL_ATTRIBUTE_ACCESS_READ_ONLY(args) \
>>        _GL_ATTRIBUTE_ACCESS_READ_ONLY_AUX args
>>   #  define _GL_ATTRIBUTE_ACCESS_READ_ONLY_AUX(...) \
>>        __attribute__ ((__access__ (__read_only__, __VA_ARGS__)))
>
> I wrote the attached patch before I saw your message. I assume the AUX
> macro would allow the double paranetheses that look more consistent with
> other macros?

Nice, your idea works better Bruno. This one seems to work with both gcc
and clang, in the 1 and 2 argument forms.

I'll wait a bit before pushing the attached patch.

BTW, I mentioned in my original email:

> Note that the access attribute also allows you to provide a size
> argument index to associate with a pointer. The GCC documentation
> mentions that it is intended to help object size checking [1] [2].
> Whether that is implemented yet, I am not sure.

Martin Uecker made me aware that this object size checking for the
access attribute has been implemented since 2022 [1]. I figured that was
worth sharing here.

Collin

[1] https://mastodon.social/@uecker/117574312111797357

>From e7e6ce8c312aefd6f0217beb15e9c0c7ab2fc5fc Mon Sep 17 00:00:00 2001
Message-ID: <e7e6ce8c312aefd6f0217beb15e9c0c7ab2fc5fc.1794273667.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Mon, 9 Nov 2026 16:40:18 -0800
Subject: [PATCH v2] attribute: Fix optional size argument with access
 attributes.

Reported by Basil L. Contovounesios <[email protected]> in:
<https://lists.gnu.org/r/bug-gnulib/2026-11/msg00046.html>

* m4/gnulib-common.m4 (gl_COMMON_BODY): Define
_GL_ATTRIBUTE_ACCESS_READ_ONLY_AUX, _GL_ATTRIBUTE_ACCESS_WRITE_ONLY_AUX,
_GL_ATTRIBUTE_ACCESS_READ_WRITE_AUX, and _GL_ATTRIBUTE_ACCESS_NONE_AUX,
using __VA_ARGS__. Define _GL_ATTRIBUTE_ACCESS_READ_ONLY,
_GL_ATTRIBUTE_ACCESS_WRITE_ONLY, _GL_ATTRIBUTE_ACCESS_READ_WRITE, and
_GL_ATTRIBUTE_ACCESS_NONE using them.
---
 ChangeLog           | 12 ++++++++++++
 m4/gnulib-common.m4 | 36 ++++++++++++++++++++++--------------
 2 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 858ab5d7dd..0e05fb9888 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2026-11-09  Collin Funk  <[email protected]>
+
+	attribute: Fix optional size argument with access attributes.
+	Reported by Basil L. Contovounesios <[email protected]> in:
+	<https://lists.gnu.org/r/bug-gnulib/2026-11/msg00046.html>
+	* m4/gnulib-common.m4 (gl_COMMON_BODY): Define
+	_GL_ATTRIBUTE_ACCESS_READ_ONLY_AUX, _GL_ATTRIBUTE_ACCESS_WRITE_ONLY_AUX,
+	_GL_ATTRIBUTE_ACCESS_READ_WRITE_AUX, and _GL_ATTRIBUTE_ACCESS_NONE_AUX,
+	using __VA_ARGS__. Define _GL_ATTRIBUTE_ACCESS_READ_ONLY,
+	_GL_ATTRIBUTE_ACCESS_WRITE_ONLY, _GL_ATTRIBUTE_ACCESS_READ_WRITE, and
+	_GL_ATTRIBUTE_ACCESS_NONE using them.
+
 2026-11-09  Bruno Haible  <[email protected]>
 
 	doc: More motivation to use the string-desc module.
diff --git a/m4/gnulib-common.m4 b/m4/gnulib-common.m4
index 119c638e66..ed64031dfe 100644
--- a/m4/gnulib-common.m4
+++ b/m4/gnulib-common.m4
@@ -1,5 +1,5 @@
 # gnulib-common.m4
-# serial 126
+# serial 127
 dnl Copyright (C) 2007-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -380,24 +380,32 @@ AC_DEFUN([gl_COMMON_BODY]
 /* Applies to: functions.  */
 /* Clang does not support these attributes.  */
 #ifndef _GL_ATTRIBUTE_ACCESS_READ_ONLY
+# define _GL_ATTRIBUTE_ACCESS_READ_ONLY(args) \
+    _GL_ATTRIBUTE_ACCESS_READ_ONLY_AUX args
+# define _GL_ATTRIBUTE_ACCESS_WRITE_ONLY(args) \
+    _GL_ATTRIBUTE_ACCESS_WRITE_ONLY_AUX args
+# define _GL_ATTRIBUTE_ACCESS_READ_WRITE(args) \
+    _GL_ATTRIBUTE_ACCESS_READ_WRITE_AUX args
+# define _GL_ATTRIBUTE_ACCESS_NONE(args) \
+    _GL_ATTRIBUTE_ACCESS_NONE_AUX args
 # if _GL_HAS_ATTRIBUTE (access) && ! defined __clang__
-#  define _GL_ATTRIBUTE_ACCESS_READ_ONLY(args) \
-     __attribute__ ((__access__ (__read_only__, args)))
-#  define _GL_ATTRIBUTE_ACCESS_WRITE_ONLY(args) \
-     __attribute__ ((__access__ (__write_only__, args)))
-#  define _GL_ATTRIBUTE_ACCESS_READ_WRITE(args) \
-     __attribute__ ((__access__ (__read_write__, args)))
+#  define _GL_ATTRIBUTE_ACCESS_READ_ONLY_AUX(...) \
+     __attribute__ ((__access__ (__read_only__, __VA_ARGS__)))
+#  define _GL_ATTRIBUTE_ACCESS_WRITE_ONLY_AUX(...) \
+     __attribute__ ((__access__ (__write_only__, __VA_ARGS__)))
+#  define _GL_ATTRIBUTE_ACCESS_READ_WRITE_AUX(...) \
+     __attribute__ ((__access__ (__read_write__, __VA_ARGS__)))
 #  if _GL_GNUC_PREREQ (11, 1)
-#   define _GL_ATTRIBUTE_ACCESS_NONE(args) \
-      __attribute__ ((__access__ (__none__, args)))
+#   define _GL_ATTRIBUTE_ACCESS_NONE_AUX(...) \
+      __attribute__ ((__access__ (__none__, __VA_ARGS__)))
 #  else
-#   define _GL_ATTRIBUTE_ACCESS_NONE(args)
+#   define _GL_ATTRIBUTE_ACCESS_NONE_AUX(...)
 #  endif
 # else
-#  define _GL_ATTRIBUTE_ACCESS_READ_ONLY(args)
-#  define _GL_ATTRIBUTE_ACCESS_WRITE_ONLY(args)
-#  define _GL_ATTRIBUTE_ACCESS_READ_WRITE(args)
-#  define _GL_ATTRIBUTE_ACCESS_NONE(args)
+#  define _GL_ATTRIBUTE_ACCESS_READ_ONLY_AUX(...)
+#  define _GL_ATTRIBUTE_ACCESS_WRITE_ONLY_AUX(...)
+#  define _GL_ATTRIBUTE_ACCESS_READ_WRITE_AUX(...)
+#  define _GL_ATTRIBUTE_ACCESS_NONE_AUX(...)
 # endif
 #endif
 
-- 
2.56.0

Reply via email to