public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Russ Allbery <eagle@eyrie.org>
To: Alejandro Colomar <alx@kernel.org>
Cc: libc-alpha@sourceware.org
Subject: Re: free(3) const void *
Date: Fri, 26 Jan 2024 10:40:23 -0800	[thread overview]
Message-ID: <87ttn0q6p4.fsf@hope.eyrie.org> (raw)
In-Reply-To: <ZbP4tUfXbOzsnHjX@debian> (Alejandro Colomar's message of "Fri, 26 Jan 2024 19:23:40 +0100")

Alejandro Colomar <alx@kernel.org> writes:

> Unless I'm missing something, you wouldn't get a warning either if you
> use a pointer after free(3), if that free() is hidden in a function
> call.

> 	extern void my_free(void *p);

> 	my_free(p);
> 	my_free(p);

> `const` doesn't make a difference there, regarding diagnostics about
> use-after-free.

Yes, const can't help you prevent problems where you don't use const.  :)

I'm not saying that using const catches every C programming bug.  But
using const currently does ensure that nothing in the call stack into
which you pass a const pointer frees that pointer, unless it uses an
explicit cast or otherwise breaks its interface contract.

(C in general, and by design, does not protect you from code that doesn't
follow its interface contract, instead erring on the side of providing
escape hatches to do odd things.  Nonetheless, interface contracts such as
const are still helpful in finding bugs within collaborating code, even if
they're not strictly enforced in every circumstance.)

> So, after Xi's reminder, I'll reformulate my suggestion to

> 	[[gnu::access(none)]]
> 	void free(const void *p);

> (or for compatibility reasons, do the const thing via a macro.)

Maybe I'm missing something, but I don't see how this addresses the
concerns that were raised.

-- 
Russ Allbery (eagle@eyrie.org)             <https://www.eyrie.org/~eagle/>

  parent reply	other threads:[~2024-01-26 18:40 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-26 13:21 Alejandro Colomar
2024-01-26 14:24 ` Arsen Arsenović
2024-01-26 15:35   ` Alejandro Colomar
2024-01-26 17:22     ` Arsen Arsenović
2024-01-26 17:55       ` Xi Ruoyao
2024-01-26 18:11         ` Alejandro Colomar
2024-01-26 20:04           ` Arsen Arsenović
2024-01-26 20:07         ` Arsen Arsenović
2024-01-26 17:40     ` Andreas Schwab
2024-01-26 19:45     ` Florian Weimer
2024-01-26 15:13 ` Andreas Schwab
2024-01-26 15:33   ` Alejandro Colomar
2024-01-26 18:09 ` Russ Allbery
2024-01-26 18:23   ` Alejandro Colomar
2024-01-26 18:36     ` Xi Ruoyao
2024-01-26 18:40       ` Alejandro Colomar
2024-01-26 18:49         ` Xi Ruoyao
2024-01-26 18:57           ` Alejandro Colomar
2024-01-26 18:40     ` Russ Allbery [this message]
2024-01-26 18:45       ` Alejandro Colomar
2024-01-26 19:41   ` Florian Weimer
2024-01-26 18:39 ` [PATCH] Use [[gnu::access(none)]] on free(3) Alejandro Colomar
2024-01-26 18:41   ` Alejandro Colomar
2024-01-26 21:23     ` Paul Eggert
2024-01-26 23:19       ` Alejandro Colomar
2024-01-27 13:21       ` Cristian Rodríguez
2024-02-13 15:19         ` Gabriel Ravier
2024-02-13 15:28           ` Alejandro Colomar
2024-01-26 21:11 ` free(3) const void * DJ Delorie
2024-01-26 21:30   ` Andreas Schwab
2024-01-26 21:47     ` DJ Delorie
2024-01-26 22:07       ` Andreas Schwab
2024-01-26 23:25       ` Alejandro Colomar

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=87ttn0q6p4.fsf@hope.eyrie.org \
    --to=eagle@eyrie.org \
    --cc=alx@kernel.org \
    --cc=libc-alpha@sourceware.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).