Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 18 Sep 2025 20:17:41 +0900
From:      Nakayama Kenjiro <nakayamakenjiro@gmail.com>
To:        freebsd-hackers@freebsd.org
Cc:        freebsd-net@freebsd.org
Subject:   Build failure with Clang/LLVM 22 due to alloc-size diagnostic
Message-ID:  <CAA_ZtA_184k892=5v0OtZsjweWSgSCX%2BWWR6se_aVCJCAh-apQ@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
--0000000000000587f9063f11812c
Content-Type: text/plain; charset="UTF-8"

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...

--0000000000000587f9063f11812c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Hi,</div><div><br></div><div>There is a new diagnosti=
c, 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 h=
ref=3D"https://github.com/llvm/llvm-project/pull/150028">https://github.com=
/llvm/llvm-project/pull/150028</a><br>When we enable this option, in_mcast.=
c triggers this diagnostic, causing the build to fail.<br><br>```<br>freebs=
d/sys/netinet/in_mcast.c:749:10: error: allocation of insufficient size &#3=
9;40&#39; for type &#39;struct ip_msource&#39; with size &#39;48&#39; [-Wer=
ror,-Walloc-size]<br>=C2=A0 749 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 nims =3D malloc(sizeof(struct in_msource), M_INMFILTER,<br>=
=C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0^<br>```<br><br><a href=3D"https://github.co=
m/freebsd/freebsd-src/blob/stable/15/sys/netinet/in_mcast.c#L749">https://g=
ithub.com/freebsd/freebsd-src/blob/stable/15/sys/netinet/in_mcast.c#L749</a=
><br>```<br>static int<br>imf_get_source(struct in_mfilter *imf, const stru=
ct sockaddr_in *psin,<br>=C2=A0 =C2=A0 struct in_msource **plims)<br>{<br>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ...<br>	struct ip_msource	*ims, *nims;<b=
r>	 =C2=A0 ...<br>		nims =3D malloc(sizeof(struct in_msource), M_INMFILTER,=
<br>		 =C2=A0 =C2=A0M_NOWAIT | M_ZERO);<br>```<br><br>As the error message =
explained, the mismatch between struct ip_msource * and malloc(sizeof(struc=
t in_msource)) triggers the error.<br><br>However, when reading the source =
code carefully, it seems that *nims is intentionally of type ip_msource ins=
tead of in_msource.<br>I would like to build with LLVM&#39;s alloc-size opt=
ion enabled, but does anyone have any good ideas on how to address this pro=
blem? Or would it be better to report it as a false positive to LLVM? Thoug=
h, I am aware that there is a workaround to partially disable this option..=
.</div></div>

--0000000000000587f9063f11812c--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAA_ZtA_184k892=5v0OtZsjweWSgSCX%2BWWR6se_aVCJCAh-apQ>