Build failure with Clang/LLVM 22 due to alloc-size diagnostic

Nakayama Kenjiro <[email protected]>
Newsgroups gmane.os.freebsd.devel.hackers,gmane.os.freebsd.devel.net
Message-ID <CAA_ZtA_184k892=5v0OtZsjweWSgSCX+WWR6se_aVCJCAh-apQ@mail.gmail.com>
Hi,

There is a new diagnostic, alloc-size, in clang of LLVM22 that warns if the
size given to a malloc is smaller than the size of the struct pointed to by
its destination - https://github.com/llvm/llvm-project/pull/150028
When we enable this option, in_mcast.c triggers this diagnostic, causing
the build to fail.

```
freebsd/sys/netinet/in_mcast.c:749:10: error: allocation of insufficient
size '40' for type 'struct ip_msource' with size '48' [-Werror,-Walloc-size]
  749 |                 nims = malloc(sizeof(struct in_msource),
M_INMFILTER,
      |                        ^
```

https://github.com/freebsd/freebsd-src/blob/stable/15/sys/netinet/in_mcast.c#L749
```
static int
imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
    struct in_msource **plims)
{
          ...
struct ip_msource *ims, *nims;
  ...
nims = malloc(sizeof(struct in_msource), M_INMFILTER,
   M_NOWAIT | M_ZERO);
```

As the error message explained, the mismatch between struct ip_msource *
and malloc(sizeof(struct in_msource)) triggers the error.

However, when reading the source code carefully, it seems that *nims is
intentionally of type ip_msource instead of in_msource.
I would like to build with LLVM's alloc-size option enabled, but does
anyone have any good ideas on how to address this problem? Or would it be
better to report it as a false positive to LLVM? Though, I am aware that
there is a workaround to partially disable this option...
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.