RE: Why is the kfree() argument const?

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <torvalds@...>
Cc: Johannes Weiner <hannes@...>, Linux Kernel Mailing List <linux-kernel@...>, <clameter@...>, <penberg@...>
Date: Friday, January 18, 2008 - 4:55 pm

> On Thu, 17 Jan 2008, David Schwartz wrote:


You cannot unlink a file. Given a file, if you were to attempt to unlink it,
what directory would you remove it from?

Unlinking a file is an operation on the directory the file is in, not on the
directory itself. We do need write permissions to the directory.

If you had only a const pointer to the data in the file, you should
definitely not be able to use that pointer to find a directory the file is
in and remove it without clearly indicating you know *exactly* what you're
doing. Given just that 'const' pointer, you're not supposed to be modifying
the data and certainly using that pointer to modify anything logically above
it.


Right. It's an operation on the directory the file is in that might have
consequences for the file.


The file is gone if and only if the directory was the only thing that needed
the file to exist. A file that is only on one directory "belongs to" that
directory. So write permission to the directory is all that is needed.

What you are arguing is essentially that you should be able to remove a file
from any directory it is in just because you have write access to the file's
data.


Making something "not exist" is a modification operation on that thing.


I agree with you about that part. I can't understand why you keep thinking
this is where our disagreement lies when I've stated at least three times
that I agree about this. The issue has nothing to do with whether or not
'kfree' modifies the particular bytes pointed to. It has to do with whether
or not 'kfree' is the kind of operation one would normally want to allow on
a 'const' object.


Nevertheless, it's a modification operation on an object that's not supposed
to be modified.

By the way, I did think of one argument that supports your position: Suppose
you have a reference counted object. You have a 'release reference and free
if zero' function. Should it be 'const'? If not, how can a 'lookup and
reference for read' function return a const pointer to the object?

However, on balance, I think a 'release reference and free if zero' function
that operates on a const pointer is sufficiently unusual that a cast to show
you know what you're doing is not a bad thing. In this case, you know it's
safe to destroy the object through a const pointer because you *know* nobody
gave you the 'const' pointer trusting you not to destroy the object. A cast
to show you have that special knowledge is, IMO, reasonable.

DS


--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
RE: Why is the kfree() argument const?, Linus Torvalds, (Thu Jan 17, 5:25 pm)
Re: Why is the kfree() argument const?, Jakob Oestergaard, (Fri Jan 18, 5:48 am)
Re: Why is the kfree() argument const?, Björn, (Fri Jan 18, 9:31 am)
Re: Why is the kfree() argument const?, Jakob Oestergaard, (Fri Jan 18, 10:53 am)
Re: Why is the kfree() argument const?, Giacomo A. Catenazzi, (Fri Jan 18, 7:47 am)
Re: Why is the kfree() argument const?, Vadim Lobanov, (Fri Jan 18, 3:06 pm)
Re: Why is the kfree() argument const?, Jakob Oestergaard, (Fri Jan 18, 10:39 am)
RE: Why is the kfree() argument const?, David Schwartz, (Thu Jan 17, 6:28 pm)
RE: Why is the kfree() argument const?, Linus Torvalds, (Thu Jan 17, 7:10 pm)
RE: Why is the kfree() argument const?, David Schwartz, (Thu Jan 17, 8:56 pm)
RE: Why is the kfree() argument const?, Linus Torvalds, (Thu Jan 17, 9:15 pm)
Re: Why is the kfree() argument const?, Giacomo Catenazzi, (Fri Jan 18, 3:51 am)
Re: Why is the kfree() argument const?, Vadim Lobanov, (Fri Jan 18, 4:30 am)
Re: Why is the kfree() argument const?, Giacomo Catenazzi, (Fri Jan 18, 4:20 am)
Re: Why is the kfree() argument const?, Andy Lutomirski, (Fri Jan 18, 9:54 am)
Re: Why is the kfree() argument const?, Vadim Lobanov, (Fri Jan 18, 3:14 pm)
Re: Why is the kfree() argument const?, Zan Lynx, (Fri Jan 18, 3:31 pm)
Re: Why is the kfree() argument const?, Vadim Lobanov, (Fri Jan 18, 3:55 pm)
Re: Why is the kfree() argument const?, Andy Lutomirski, (Fri Jan 18, 9:53 am)
Re: Why is the kfree() argument const?, Olivier Galibert, (Fri Jan 18, 1:24 pm)
Re: Why is the kfree() argument const?, J.A. , (Fri Jan 18, 6:29 pm)
Re: Why is the kfree() argument const?, Krzysztof Halasa, (Fri Jan 18, 7:44 pm)
RE: Why is the kfree() argument const?, David Schwartz, (Fri Jan 18, 1:02 am)
Re: Why is the kfree() argument const?, DM, (Fri Jan 18, 2:06 pm)
Re: Why is the kfree() argument const?, Olivier Galibert, (Fri Jan 18, 1:37 pm)
RE: Why is the kfree() argument const?, Linus Torvalds, (Fri Jan 18, 12:10 pm)
RE: Why is the kfree() argument const?, David Schwartz, (Fri Jan 18, 4:55 pm)
Re: Why is the kfree() argument const?, Chris Friesen, (Fri Jan 18, 11:38 am)