Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #1835

Re: Tracking and Reporting Effective Type

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.std.c
Subject Re: Tracking and Reporting Effective Type
Date 2013-01-14 01:03 -0500
Organization A noiseless patient Spider
Message-ID <kd0732$q7v$1@dont-email.me> (permalink)
References <kcovdt$1ef$1@dont-email.me> <alhgt2Fme55U1@mid.individual.net>

Show all headers | View raw


On 1/13/2013 23:48, Wojtek Lerch wrote:
> On 11/01/2013 7:09 AM, Shao Miller wrote:
>> I'm planning on writing implementation code that tracks and reports
>> effective type, whenever possible, as a "bonus" feature.  I've run into
>> a challenge:
>
> I have another challenge for you:
>
> union u {
>    struct sl { int16_t s; int32_t l; } sl;
>    struct ls { int32_t l; int16_t s; } ls;
>    uint32_t u;
>    float f;
> };
>
> int main(void) {
>    void *p;
>    if ( sizeof(union u) == 6 // no padding
>      && ( m = malloc(6) ) != NULL ) {
>      union u u;
>      memset( u, 0, 6 );
>      memcpy( p, (void*)&u.sl, 6 );
>      ...
>    }
> }
>
> At the "...", does the 6-byte object that p points to have an effective
> type?  Is it union u, struct sl, or perhaps struct ls?  Would the answer
> change if the second argument of the memcpy were (void*)&u instead of
> (void*)&u.sl?  Or (void*)&u.f?
>

Assuming you meant 'memset( &u, 0, 6 );', I think I saw something like 
this in Defect Report #219.

I think that the effective type is indeed 'struct sl' due to the 
declared type of the only lvalue in sight in the 'memcpy': 'u.sl'.  I do 
think the answer would change.

> Does the four-byte sub-object at the beginning of "u" have an effective
> type?

I'd say each of the 4 bytes would be 'unsigned char', due to the 
'memset' copying as an array of 'unsigned char', but the effective type 
of the whole would be 'union u', due to the lvalue '&u' in the 'memset', 
and the fact that all bytes were populated.

> Is it int32_t, uint32_t, or perhaps (if sizeof(float)==4) float?

If I understand a point from DR #219, the bytes "can be treated" as 
these others, but could be a trap representation for a 'float'.

> Or somehow all of the above?

I don't think so, for tracking and reporting purposes.

> What about the four-byte object starting
> at the byte that p points to?

I'd say no.  The 4 bytes so happen to be correctly aligned and contain a 
correct representation for some of the other members, though.

But if "provenance" was tracked, I'd track it to 'u.sl'; there'd be an 
'int16_t' and an 'int32_t'.  So I might trap '((union u *) p)->ls.l', 
for example, but not '((union u *) p)->sl.s' or '((struct sl *) p)->l'.

> And the two-byte object starting at the
> same byte?
>

I'd say yes, as that'd have the effective type of 'u.sl.s'; 'int16_t'. 
What do you think?

-- 
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily,"  -- Richard Harter

Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Tracking and Reporting Effective Type Shao Miller <sha0.miller@gmail.com> - 2013-01-11 07:09 -0500
  Re: Tracking and Reporting Effective Type Wojtek Lerch <wojtek_l@yahoo.ca> - 2013-01-13 23:48 -0500
    Re: Tracking and Reporting Effective Type Shao Miller <sha0.miller@gmail.com> - 2013-01-14 01:03 -0500
      Re: Tracking and Reporting Effective Type Wojtek Lerch <wojtek_l@yahoo.ca> - 2013-01-15 00:28 -0500
        Re: Tracking and Reporting Effective Type Shao Miller <sha0.miller@gmail.com> - 2013-01-15 05:39 -0500
          Re: Tracking and Reporting Effective Type Wojtek Lerch <wojtek_l@yahoo.ca> - 2013-01-17 00:32 -0500
            Re: Tracking and Reporting Effective Type Wojtek Lerch <wojtek_l@yahoo.ca> - 2013-01-17 01:23 -0500
            Re: Tracking and Reporting Effective Type Shao Miller <sha0.miller@gmail.com> - 2013-01-17 22:47 -0500
              Re: Tracking and Reporting Effective Type Wojtek Lerch <wojtek_l@yahoo.ca> - 2013-01-18 16:14 -0500
                Re: Tracking and Reporting Effective Type Wojtek Lerch <wojtek_l@yahoo.ca> - 2013-01-18 16:21 -0500

csiph-web