From: Leonardo Bras <leobras.c@gmail.com>
To: "Paul E . McKenney" <paulmck@kernel.org>
Cc: Leonardo Bras <leobras.c@gmail.com>, perfbook@vger.kernel.org
Subject: [PATCH] CodeSamples/tree: Fix compiler warning on free
Date: Fri, 16 Jun 2023 03:39:03 -0300 [thread overview]
Message-ID: <20230616063903.207676-3-leobras.c@gmail.com> (raw)
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().
Signed-off-by: Leonardo Bras <leobras.c@gmail.com>
---
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);
--
2.41.0
next reply other threads:[~2023-06-16 6:39 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-16 6:39 Leonardo Bras [this message]
2023-06-20 2:13 ` [PATCH] CodeSamples/tree: Fix compiler warning on free Leonardo Bras
2023-06-20 22:40 ` Paul E. McKenney
2023-06-21 4:26 ` Leonardo Brás
2023-06-21 4:36 ` Leonardo Brás
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230616063903.207676-3-leobras.c@gmail.com \
--to=leobras.c@gmail.com \
--cc=paulmck@kernel.org \
--cc=perfbook@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).