Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Shao Miller <sha0.miller@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: partial struct usage |
| Date | 2012-02-01 00:42 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jgajcc$ovp$1@dont-email.me> (permalink) |
| References | <2011092410234736215-madler@alumnicaltechedu> <87vcshy8wu.fsf@bazspaz.fatphil.org> <kfnty3kejt6.fsf@x-alumni2.alumni.caltech.edu> <jfo0l5$jgd$1@dont-email.me> <kfnlion1kge.fsf@x-alumni2.alumni.caltech.edu> |
On 1/31/2012 19:11, Tim Rentsch wrote:
> Shao Miller<sha0.miller@gmail.com> writes:
>
>> On 1/24/2012 18:56, Tim Rentsch wrote:
>>>
>>> Using container_of() should be just as portable as this.
>>>
>>
>> For the special case where the pointer points to an object at the
>> beginning of any containing sub-object... If a pointer carries bounds
>> information, subtracting from a pointer needn't be defined
>> (apparently).
>>
>> struct s_foo {
>> int i[1];
>> double d[1];
>> } * foo = malloc(sizeof *foo);
>> double * dp;
>> assert(foo);
>> dp = foo->d;
>> /* dp bounds might != foo bounds. Uh oh */
>> foo = container_of(dp, struct s_foo, d);
>
> The same argument applies to members that aren't arrays,
> which shows the argument must be wrong, since
The argument _does_ apply to members that aren't arrays, but I've seen
especial attention regarding "arrays" with "bounds-checking," so the
example highlights that.
Why would a bounds-checking implementation _not_ be allowed to
continually reduce the possible bounds, but never increase them? Say if
pointer representation went something like:
struct s_ptr {
unsigned char * first_byte;
size_t size;
size_t current_position;
};
"internally" (C pseudo-code for the implementation of the abstract
machine). So imagine, in the program for consideration, we have:
struct s_foo {
int i;
double d;
};
struct s_foo foo = { 0 };
double * d = &foo.d;
"Internally," 'd' might have the members populated with values
equivalent to '(unsigned char *) &foo + offsetof(struct s_foo, d)',
'sizeof foo.d', '0', respectively.
Suppose we cast:
unsigned char * bytes_of_d = (unsigned char *) d;
Suppose this 'bytes_of_d' now has the same "internal" member values as
'd'. If you try to:
unsigned char * backwards = bytes_of_d - 1;
You've gone out-of-bounds. If, instead, you'd been using an 'unsigned
char *' cast from a pointer to the whole of 'foo', then the bounds would
be sufficient to point anywhere within it.
> the whole
> point of offsetof() is to be able to convert back and
> forth between pointers to member objects and a pointer
> to the struct as a whole.
I'm not sure why you suggest this. The whole point of 'offsetof' is to
provide an integer constant expression giving the offset of a 'struct'
member, as far as the standard text goes. You might have some Committee
experiences that grant you knowledge of motives beyond that, but those
aren't widely accessible.
It's possible that you simply meant to type 'container_of' rather than
'offsetof', there.
Following the absurd-but-seemingly-permitted example bounds-checking
implementation example above, if we had:
struct s_baz {
unsigned char c;
double d;
};
struct s_baz baz;
unsigned char * whole = (unsigned char *) &baz;
unsigned char * part = &baz.c;
The bounds for 'whole' could be greater than the bounds for 'part'. Now
if we cast:
part = *((unsigned char (*)[sizeof baz]) part);
Then "internally," the implementation could say "they must know that
there's more data than the current bounds" and adjust the "internal"
'size' member value accordingly. Then 'part' and 'whole' would be the
same. But there doesn't appear to be a way to say "take this pointer
value and cast it as though its 'current_position' is part-way along;
I'd expect that if anything, such a spiteful implementation would always
reset 'current_position' to 0... "Hey, you said this is what we've got
here."
But seriously, in general, how might a bounds-checking implementation
deal with:
int mumble(int * i) {
i = i - 1;
return *i;
}
where you might pass a pointer to the xth element of an array of 'int'
but you might not? What's safe to assume? What kinds of bounds might
be encoded or associated with the pointer value? Would the 'beginning'
map all the way back to some declared type somewhere? Some effective
type? Or is bounds-checking a special feature of using 'a[i]' notation
and 's.m' or 'sp->m' notation and "raw" points are immune?
--
"The stationery store has moved. Aaargh!"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
partial struct usage Mark Adler <madler@alumni.caltech.edu> - 2011-09-24 10:23 -0700
Re: partial struct usage Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-24 13:44 -0400
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 14:08 -0800
Re: partial struct usage BGB <cr88192@hotmail.com> - 2011-09-24 11:08 -0700
Re: partial struct usage Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-24 23:36 +0300
Re: partial struct usage Mark Adler <madler@alumni.caltech.edu> - 2011-09-25 13:23 -0700
Re: partial struct usage Gene <gene.ressler@gmail.com> - 2011-09-26 01:14 -0700
Re: partial struct usage Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-26 12:37 -0400
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 16:04 -0800
Re: partial struct usage Mark Adler <madler@alumni.caltech.edu> - 2011-09-26 18:52 -0700
Re: partial struct usage Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-27 12:14 +0100
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 15:56 -0800
Re: partial struct usage Shao Miller <sha0.miller@gmail.com> - 2012-01-24 23:30 -0500
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 16:11 -0800
Re: partial struct usage Shao Miller <sha0.miller@gmail.com> - 2012-02-01 00:42 -0500
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 09:33 -0800
Re: partial struct usage Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-12 02:10 +0200
Re: partial struct usage Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 09:38 -0800
Re: partial struct usage Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-03-13 18:22 +0200
Re: partial struct usage blp@cs.stanford.edu (Ben Pfaff) - 2012-03-13 09:55 -0700
Re: partial struct usage Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-24 17:56 -0400
csiph-web