Re: gcc 12.1.0 warning
Andres Freund <andres@anarazel.de>
From: Andres Freund <andres@anarazel.de>
To: Nazir Bilal Yavuz <byavuz81@gmail.com>, Tom Lane <tgl@sss.pgh.pa.us>
Cc: Erik Rijkers <er@xs4all.nl>, pgsql-hackers@lists.postgresql.org, Thomas Munro <thomas.munro@gmail.com>
Date: 2024-04-23T16:59:39Z
Lists: pgsql-hackers
Hi,
On 2024-04-15 11:25:05 +0300, Nazir Bilal Yavuz wrote:
> I am able to reproduce this. I regenerated the debian bookworm image
> and ran CI on REL_15_STABLE with this image.
>
> CI Run: https://cirrus-ci.com/task/4978799442395136
Hm, not sure why I wasn't able to repro - now I can.
It actually seems like a legitimate warning: The caller allocates the key as
static struct config_generic *
find_option(const char *name, bool create_placeholders, bool skip_errors,
int elevel)
{
const char **key = &name;
and then does
res = (struct config_generic **) bsearch((void *) &key,
(void *) guc_variables,
num_guc_variables,
sizeof(struct config_generic *),
guc_var_compare);
while guc_var_compare() assume it's being passed a full config_generic:
static int
guc_var_compare(const void *a, const void *b)
{
const struct config_generic *confa = *(struct config_generic *const *) a;
const struct config_generic *confb = *(struct config_generic *const *) b;
return guc_name_compare(confa->name, confb->name);
}
which several versions of gcc then complain about:
In function ‘guc_var_compare’,
inlined from ‘bsearch’ at /usr/include/x86_64-linux-gnu/bits/stdlib-bsearch.h:33:23,
inlined from ‘find_option’ at /home/andres/src/postgresql-15/src/backend/utils/misc/guc.c:5640:35:
/home/andres/src/postgresql-15/src/backend/utils/misc/guc.c:5727:38: warning: array subscript ‘const struct config_generic[0]’ is partly outside array bounds of ‘const char[8]’ [-Warray-bounds=]
5727 | return guc_name_compare(confa->name, confb->name);
| ~~~~~^~~~~~
/home/andres/src/postgresql-15/src/backend/utils/misc/guc.c: In function ‘find_option’:
/home/andres/src/postgresql-15/src/backend/utils/misc/guc.c:5627:25: note: object ‘name’ of size 8
5627 | find_option(const char *name, bool create_placeholders, bool skip_errors,
Which seems entirely legitimate. ISTM that guc_var_compare() ought to only
cast the pointers to the key type, i.e. char *. And incidentally that does
prevent the warning.
The reason it doesn't happen in newer versions of postgres is that we aren't
using guc_var_compare() in the relevant places anymore...
Greetings,
Andres Freund
Commits
-
Fix bad indentation introduced in 43cd30bcd1c
- ff3cae4875d3 17.0 landed
- 212b0262d60e 16.4 landed
- 11441ad48d09 15.8 landed
- 3006fd8e6ece 14.13 landed
- 9d3a2b3cdfbd 13.16 landed
- 69bdee12e443 12.20 landed
- 47ecbfdfcc71 18.0 landed
-
Fix type confusion in guc_var_compare()
- ee89c4fa4b18 14.13 landed
- 92f02c39c0fa 12.20 landed
- 2cf9bda76cfd 13.16 landed
- b9f3db23b7b5 15.8 landed
- 2ad3b9350f73 16.4 landed
- 9b047cc0b2c2 17.0 landed
- 43cd30bcd1cd 18.0 landed