bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] hash: pacify gcc -Wsuggest-attribute=const


From: Pádraig Brady
Subject: Re: [PATCH] hash: pacify gcc -Wsuggest-attribute=const
Date: Fri, 19 May 2023 12:27:03 +0100
User-agent: Mozilla Thunderbird

On 19/05/2023 12:03, Bruno Haible wrote:
Hi Pádraig,

diff --git a/lib/hash.c b/lib/hash.c
index 918aa0d1c3..332cca6df9 100644
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -492,7 +492,7 @@ check_tuning (Hash_table *table)
     TUNING, or return 0 if there is no possible way to allocate that
     many entries.  */
-static size_t _GL_ATTRIBUTE_PURE
+static size_t _GL_ATTRIBUTE_CONST
  compute_bucket_size (size_t candidate, const Hash_tuning *tuning)
  {
    if (!tuning->is_n_buckets)


This looks wrong. Per the definition of the 'const' attribute [1]
   "The const attribute prohibits a function from reading objects that
    affect its return value between successive invocations. However,
    functions declared with the attribute can safely read objects that
    do not change their return value, such as non-volatile constants."
In this function's body, testing tuning->is_n_buckets affects the
return value.

Bruno

[1] 
https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Common-Function-Attributes.html

I was unsure about that TBH.

In coreutils usage tuning==NULL so the static const default_tuning is passed 
always.
So I guess _GL_ATTRIBUTE_CONST is fine there.

In general for any tuning though, since the tuning param struct is declared 
const,
can the compiler not assume successive invocations against the _same object_
returns the same value?

cheers,
Pádraig



reply via email to

[Prev in Thread] Current Thread [Next in Thread]