https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123500
Bug ID: 123500
Summary: Warning from -Wbad-function-cast when using counted_by
attribute
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: b.buschinski at googlemail dot com
Target Milestone: ---
With recent GCC git version.
```
#include <stdint.h>
struct buffer {
uint8_t * ptr __attribute__((counted_by(len)));
int len;
};
uintptr_t foo(struct buffer * b) {
return (uintptr_t)b->ptr;
}
```
gcc-16 -Werror -Wbad-function-cast test.c
```
test.c: In function ‘foo’:
test.c:9:12: error: cast from function call of type ‘uint8_t *’ {aka ‘unsigned
char *’} to non-matching type ‘long unsigned int’ [-Werror=bad-function-cast]
9 | return (uintptr_t)b->ptr;
| ^
cc1: all warnings being treated as errors
```
but without the attribute, it compiles fine.
Also tested it in the compiler explorer: https://godbolt.org/z/Yo6hbf1vc
GCC:
```
$ cat gcc.txt
Using built-in specs.
COLLECT_GCC=gcc-16
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/16/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-pc-linux-gnu
Configured with:
/var/tmp/portage/sys-devel/gcc-16.0.9999/work/gcc-16.0.9999/configure
--host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr
--bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/16
--includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/16/include
--datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/16
--mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/16/man
--infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/16/info
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/16/include/g++-v16
--disable-silent-rules --disable-dependency-tracking
--with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/16/python
--enable-languages=c,c++,fortran --enable-obsolete --enable-secureplt
--disable-werror --with-system-zlib --enable-nls --without-included-gettext
--disable-libunwind-exceptions --enable-checking=yes,extra
--with-bugurl=https://bugs.gentoo.org/ --with-pkgversion='Gentoo 16.0.9999 p,
commit 199ec9abeb96b37eb2b3b8974ec4a39b2525e231' --with-gcc-major-version-only
--enable-libstdcxx-time --enable-lto --disable-libstdcxx-pch --enable-shared
--enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu
--with-tls=gnu2 --enable-multilib --with-multilib-list=m32,m64
--disable-fixed-point --enable-targets=all --enable-offload-defaulted
--enable-offload-targets=nvptx-none --enable-libgomp --disable-libssp
--disable-libada --enable-cet --disable-systemtap
--disable-valgrind-annotations --disable-valgrind-interop
--disable-vtable-verify --disable-libvtv --with-zstd --without-isl
--enable-default-pie --enable-host-pie --enable-host-bind-now
--enable-default-ssp --disable-fixincludes
--with-gxx-libcxx-include-dir=/usr/include/c++/v1
--with-build-config=bootstrap-cet
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.0.0 20260108 (experimental)
4fbc0bbc03162f3962ea79bac29d36952867c90f (Gentoo 16.0.9999 p, commit
199ec9abeb96b37eb2b3b8974ec4a39b2525e231)
```