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

--- Comment #8 from Linus Torvalds <torva...@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.

Reply via email to