Skip site navigation (1)Skip section navigation (2)
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

--Apple-Mail=_EB7F8416-2F5E-4DD6-A63A-B90158FE5A54
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii



> On Sep 18, 2025, at 7:17 PM, Nakayama Kenjiro =
<nakayamakenjiro@gmail.com> wrote:
>=20
> Hi,
>=20
> 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.
>=20
> ```
> 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 =3D malloc(sizeof(struct in_msource), =
M_INMFILTER,
>       |                        ^
> ```
>=20
> =
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_mcas=
t.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 =3D malloc(sizeof(struct in_msource), M_INMFILTER,
>    M_NOWAIT | M_ZERO);

The following lines has this
```
lims =3D (struct in_msource *)nims;
```

So probably assign the alloced memory directly to lims would make Clang =
happy, say
```
lims =3D malloc(sizeof( .... ;
nims =3D (struct ip_mfilter *)lims;
```

You can have a try with that. Good luck with you !

> ```
>=20
> As the error message explained, the mismatch between struct ip_msource =
* and malloc(sizeof(struct in_msource)) triggers the error.
>=20
> 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


--Apple-Mail=_EB7F8416-2F5E-4DD6-A63A-B90158FE5A54
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=us-ascii

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Dus-ascii"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;" class=3D""><br =
class=3D""><div><br class=3D""><blockquote type=3D"cite" class=3D""><div =
class=3D"">On Sep 18, 2025, at 7:17 PM, Nakayama Kenjiro &lt;<a =
href=3D"mailto:nakayamakenjiro@gmail.com" =
class=3D"">nakayamakenjiro@gmail.com</a>&gt; wrote:</div><br =
class=3D"Apple-interchange-newline"><div class=3D""><div dir=3D"ltr" =
class=3D""><div class=3D"">Hi,</div><div class=3D""><br =
class=3D""></div><div class=3D"">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=3D"https://github.com/llvm/llvm-project/pull/150028" =
class=3D"">https://github.com/llvm/llvm-project/pull/150028</a><br =
class=3D"">When we enable this option, in_mcast.c triggers this =
diagnostic, causing the build to fail.<br class=3D""><br class=3D"">```<br=
 class=3D"">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=3D"">&nbsp; 749 | &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nims =3D malloc(sizeof(struct =
in_msource), M_INMFILTER,<br class=3D"">&nbsp; &nbsp; &nbsp; | &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;^<br class=3D"">```<br class=3D""><br class=3D""><a =
href=3D"https://github.com/freebsd/freebsd-src/blob/stable/15/sys/netinet/=
in_mcast.c#L749" =
class=3D"">https://github.com/freebsd/freebsd-src/blob/stable/15/sys/netin=
et/in_mcast.c#L749</a><br class=3D"">```<br class=3D"">static int<br =
class=3D"">imf_get_source(struct in_mfilter *imf, const struct =
sockaddr_in *psin,<br class=3D"">&nbsp; &nbsp; struct in_msource =
**plims)<br class=3D"">{<br class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; ...<br class=3D"">	struct ip_msource	*ims, *nims;<br =
class=3D"">	 &nbsp; ...<br class=3D"">		nims =3D =
malloc(sizeof(struct in_msource), M_INMFILTER,<br class=3D"">		 =
&nbsp; &nbsp;M_NOWAIT | M_ZERO);<br =
class=3D""></div></div></div></blockquote><div><br =
class=3D""></div><div>The following lines has =
this</div><div>```</div><div>lims =3D (struct in_msource =
*)nims;</div><div>```</div><div><br class=3D""></div><div>So probably =
assign the alloced memory directly to lims would make Clang happy, =
say</div><div>```</div><div>lims =3D malloc(sizeof( .... =
;</div><div>nims =3D (struct ip_mfilter =
*)lims;</div><div>```</div><div><br class=3D""></div><div>You can have a =
try with that. Good luck with you !</div><br class=3D""><blockquote =
type=3D"cite" class=3D""><div class=3D""><div dir=3D"ltr" class=3D""><div =
class=3D"">```<br class=3D""><br class=3D"">As the error message =
explained, the mismatch between struct ip_msource * and =
malloc(sizeof(struct in_msource)) triggers the error.<br class=3D""><br =
class=3D"">However, when reading the source code carefully, it seems =
that *nims is intentionally of type ip_msource instead of in_msource.<br =
class=3D"">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=3D""><div class=3D"">
<div>Best regards,</div><div>Zhenlei</div>

</div>
<br class=3D""></body></html>=

--Apple-Mail=_EB7F8416-2F5E-4DD6-A63A-B90158FE5A54--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?9B7C5718-5F5E-46FB-BB97-0F75FB5CD117>