Re: Build failure with Clang/LLVM 22 due to alloc-size diagnostic
Date: Thu, 18 Sep 2025 14:29:36 UTC
> On Sep 18, 2025, at 7:17 PM, Nakayama Kenjiro <nakayamakenjiro@gmail.com> wrote:
>
> 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 <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 <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);
The following lines has this
```
lims = (struct in_msource *)nims;
```
So probably assign the alloced memory directly to lims would make Clang happy, say
```
lims = malloc(sizeof( .... ;
nims = (struct ip_mfilter *)lims;
```
You can have a try with that. Good luck with you !
> ```
>
> 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...
Best regards,
Zhenlei