Date: Thu, 18 Sep 2025 22:29:36 +0800 From: Zhenlei Huang <zlei@FreeBSD.org> To: Nakayama Kenjiro <nakayamakenjiro@gmail.com> Cc: freebsd-hackers@freebsd.org, freebsd-net@freebsd.org Subject: Re: Build failure with Clang/LLVM 22 due to alloc-size diagnostic Message-ID: <9B7C5718-5F5E-46FB-BB97-0F75FB5CD117@FreeBSD.org> In-Reply-To: <CAA_ZtA_184k892=5v0OtZsjweWSgSCX%2BWWR6se_aVCJCAh-apQ@mail.gmail.com> References: <CAA_ZtA_184k892=5v0OtZsjweWSgSCX%2BWWR6se_aVCJCAh-apQ@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
[-- Attachment #1 --] > 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 [-- Attachment #2 --] <html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Sep 18, 2025, at 7:17 PM, Nakayama Kenjiro <<a href="mailto:nakayamakenjiro@gmail.com" class="">nakayamakenjiro@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Hi,</div><div class=""><br class=""></div><div class="">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 - <a href="https://github.com/llvm/llvm-project/pull/150028" class="">https://github.com/llvm/llvm-project/pull/150028</a><br class="">When we enable this option, in_mcast.c triggers this diagnostic, causing the build to fail.<br class=""><br class="">```<br class="">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]<br class=""> 749 | nims = malloc(sizeof(struct in_msource), M_INMFILTER,<br class=""> | ^<br class="">```<br class=""><br class=""><a href="https://github.com/freebsd/freebsd-src/blob/stable/15/sys/netinet/in_mcast.c#L749" class="">https://github.com/freebsd/freebsd-src/blob/stable/15/sys/netinet/in_mcast.c#L749</a><br class="">```<br class="">static int<br class="">imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,<br class=""> struct in_msource **plims)<br class="">{<br class=""> ...<br class=""> struct ip_msource *ims, *nims;<br class=""> ...<br class=""> nims = malloc(sizeof(struct in_msource), M_INMFILTER,<br class=""> M_NOWAIT | M_ZERO);<br class=""></div></div></div></blockquote><div><br class=""></div><div>The following lines has this</div><div>```</div><div>lims = (struct in_msource *)nims;</div><div>```</div><div><br class=""></div><div>So probably assign the alloced memory directly to lims would make Clang happy, say</div><div>```</div><div>lims = malloc(sizeof( .... ;</div><div>nims = (struct ip_mfilter *)lims;</div><div>```</div><div><br class=""></div><div>You can have a try with that. Good luck with you !</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="">```<br class=""><br class="">As the error message explained, the mismatch between struct ip_msource * and malloc(sizeof(struct in_msource)) triggers the error.<br class=""><br class="">However, when reading the source code carefully, it seems that *nims is intentionally of type ip_msource instead of in_msource.<br class="">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...</div></div> </div></blockquote></div><br class=""><div class=""> <div>Best regards,</div><div>Zhenlei</div> </div> <br class=""></body></html>
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?9B7C5718-5F5E-46FB-BB97-0F75FB5CD117>
