public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "torvalds@linux-foundation.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/94527] RFE: Add an __attribute__ that marks a function as freeing an object
Date: Wed, 08 Apr 2020 16:44:06 +0000	[thread overview]
Message-ID: <bug-94527-4-UrEqAlU6s6@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-94527-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94527

--- Comment #8 from Linus Torvalds <torvalds@linux-foundation.org> ---
(In reply to Jonathan Wakely from comment #6)
> I can see uses that aren't just "frees the memory", e.g. after fclose and
> close any further uses of their argument are probably errors. The close case
> is interesting because it's not a pointer argument.

I suspect you'll find that if it's not a pointer argument, it ends up being not
very useful.

To take your "close()" example: sure, after calling close, the fd isn't active
any more, and you can't use it for IO. So it's similar to free() in many
respects: the lifetime is over and done with as a file descriptor.

But it's very different in other ways. For example, it is _entirely_ valid to
do something like this:

     close(fd);
     mark_fd_freed(fd);

where the application keeps track of free fd ranges on its own (or it keeps
track of a set of file descriptors it is polling, or whatever).

So you can still validly use the 'fd' value even after you closed it, in ways
that is not really the case with pointers from memory allocators and free().

Dereferencing a pointer after freeing it is basically _always_ a bug, but using
a file descriptor after closing it might not be.

  parent reply	other threads:[~2020-04-08 16:44 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-07 22:44 [Bug tree-optimization/94527] New: " dhowells at redhat dot com
2020-04-07 22:46 ` [Bug tree-optimization/94527] " dhowells at redhat dot com
2020-04-07 22:48 ` law at redhat dot com
2020-04-07 23:49 ` torvalds@linux-foundation.org
2020-04-08  0:18 ` law at redhat dot com
2020-04-08  0:48 ` torvalds@linux-foundation.org
2020-04-08  6:45 ` [Bug middle-end/94527] " rguenth at gcc dot gnu.org
2020-04-08  7:53 ` redi at gcc dot gnu.org
2020-04-08 16:28 ` msebor at gcc dot gnu.org
2020-04-08 16:44 ` torvalds@linux-foundation.org [this message]
2020-10-06 13:04 ` dmalcolm at gcc dot gnu.org
2020-10-27  1:28 ` msebor at gcc dot gnu.org
2020-11-13 21:47 ` msebor at gcc dot gnu.org
2020-11-13 21:57 ` msebor at gcc dot gnu.org
2020-12-03 22:43 ` cvs-commit at gcc dot gnu.org
2020-12-03 22:45 ` msebor at gcc dot gnu.org

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=bug-94527-4-UrEqAlU6s6@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.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).