Re: [PATCH] CodeSamples/tree: Fix compiler warning on free

Leonardo Bras <[email protected]>
Newsgroups org.kernel.vger.perfbook
Message-ID <[email protected]>
On Fri, 2023-06-16 at 03:39 -0300, Leonardo Bras wrote:
> While building the CodeSamples/datastruct/Issaquah/ directory, I can
> see
> a couple instances of this warning:
> 
> In function ‘free_treenode_cache’,
>     inlined from ‘tree_remove_all’ at tree.c:102:2,
>     inlined from ‘tree_free’ at tree.c:128:2:
> tree.c:251:9: warning: ‘free’ called on pointer ‘trp’ with nonzero
> offset 96 [-Wfree-nonheap-object]
>   251 |         free(tnp);
>       |         ^~~~~~~~~
> 
> I took a look and tried to understand what was happening:
> - tree_remove_all() calls free_treenode_cache() on it's input, which
> ends
>   up free()'ing it (!BAD_MALLOC)
> - It makes sense in most treenodes, since they are allocated with
>   alloc_treenode_cache() and the malloc() output is the same as the
> free()
>   input.
> - tree_free() calls tree_remove_all() on &trp->max, which ends up
> trying
>   to free() this same address.
> - trp is a struct treeroot, which is composed of 2 treenodes: min &
> max
> - The output of malloc() for trp ends up being different from the
> address
>   used for free(), since &trp->max is used instead, and there is an
> offset
>   since max is the second element of struct treeroot.
> 
> To solve this while keeping the tree_remove_all() generic, move
> struct traceroot->max to be the first element, and guarantee the
> address
> used for free() is the same returned by malloc().

Extra info:

Bug reproduction:
https://gitlab.com/linux-kernel/perfbook/-/jobs/4501216686#L212

With bugfix provided in this patch:
https://gitlab.com/linux-kernel/perfbook/-/jobs/4485986705


Thanks,
Leo

> 
> Signed-off-by: Leonardo Bras <[email protected]>
> ---
>  CodeSamples/datastruct/Issaquah/tree.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/CodeSamples/datastruct/Issaquah/tree.h
> b/CodeSamples/datastruct/Issaquah/tree.h
> index f007558a..bbe5e7c1 100644
> --- a/CodeSamples/datastruct/Issaquah/tree.h
> +++ b/CodeSamples/datastruct/Issaquah/tree.h
> @@ -48,8 +48,8 @@ struct treenode {
>   * Root of a tree.
>   */
>  struct treeroot {
> -       struct treenode min;
>         struct treenode max;
> +       struct treenode min;
>  } __attribute__((__aligned__(CACHE_LINE_SIZE)));
>  
>  void treenode_wire_call_rcu(void);
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEENubJWuDxEcxbb00uaIwz+KDFsMUFAmSRC0wACgkQaIwz+KDF
sMXS1A/8DKrcq7V4h0zPuld7puKjWt5jdqjc8CZZz9uhVxkTptsdd0VmncSAMKnh
h9G9GI3WWprSsO2qrvpzp9sCy+Cne9PTSvPnCb13hy0T6JDzMvKd30e3xSv01UdX
67Zk7mg83vjlJwjicAEhc4sXnuaedPCf7GgVN9wIkJJAAA4HYYuF9vcIexJxi7ou
K1jGRo46HTLhQlKDCHy1+8oObKvnNzf1XzfLN4G7BTxYchmShawKN3ScUfMqKkY7
9I54g7DO6RQi/O5vs/exLUPaWwC5F6VNaGm5GEoeenAUBh3wdsAODqTJgAdnhDZj
pQ1IqtC7uePAirSN8uY+ZAa4kpQDJbIev/vfFUbewmHWfWSSUFrBmtRvdM1Xz70o
d3cUVVJnOdffxNNyGTBxLbZ3r6zq0W1D+5wdZu3d9KnemYNHirZ5ccaq9iT0cVLC
8DSFMrfgXiiJBvfTtYtH+eCbGaUOkABwmJguWbvrPvD5/lNTLhNYzCHPDRemFTd7
9KFxYswSPkZr6prHUyGDtZhqlpjl4qNMkvH2vpn7rSYs/JX3B1+6Q/Uf1Z15wx2P
Nsepo3ebkW1pptAIjJbxa6bQrIN+7hwqzJfmKYFSCHhf6bbmRGg8qm733GYtI1kP
ax0eo6MLm8plaL5lEGoOSx72ou/+d1FmDLvt28KvuEPxzJ0VrqI=
=jkXa
-----END PGP SIGNATURE-----
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.