IBM Support

LI74408: OFFSETOF RETURNS INCORRECT VALUE IN C++

Subscribe

You can track all active APARs for this component.

 

APAR status

  • Closed as program error.

Error description

  • When using the 'offsetof()' in C++ on an array data member, with
    a typedef type, it will inproperly evaluate to 0.
    ================================================================
    
    ===== TESTCASE:
    #include <stdio.h>
    #include <stddef.h>
    
    typedef unsigned char myarray[4];
    
    typedef struct
    {
      myarray mymem;
    }myTDStruct;
    
    int main()
    {
      printf( ("offset of mymem[0]=%u\n"), offsetof (myTDStruct,
      mymem[0] ));
    
      printf( ("offset of mymem[1]=%u\n"), offsetof (myTDStruct,
      mymem[1] ));
    
      printf( ("offset of mymem[2]=%u\n"), offsetof (myTDStruct,
      mymem[2] ));
    
      printf( ("offset of mymem[3]=%u\n"), offsetof (myTDStruct,
      mymem[3] ));
    }
    
    
    ==== ACTUAL OUTPUT:
    offset of mymem[0]=0
    offset of mymem[1]=0
    offset of mymem[2]=0
    offset of mymem[3]=0
    
    ==== EXPECTED OUTPUT:
    offset of mymem[0]=0
    offset of mymem[1]=1
    offset of mymem[2]=2
    offset of mymem[3]=3
    

Local fix

  • 1) Compile with C
    2) Remove the typedef
    ================================================================
    
    ===== TESTCASE:
    #include <stdio.h>
    #include <stddef.h>
    
    
    typedef struct
    {
      unsigned char mymem[4];
    }myUCStruct;
    
    int main()
    {
      printf( ("offset of mymem[0]=%u\n"), offsetof (myUCStruct,
      mymem[0] ));
    
      printf( ("offset of mymem[1]=%u\n"), offsetof (myUCStruct,
      mymem[1] ));
    
      printf( ("offset of mymem[2]=%u\n"), offsetof (myUCStruct,
      mymem[2] ));
    
      printf( ("offset of mymem[3]=%u\n"), offsetof (myUCStruct,
      mymem[3] ));
    }
    

Problem summary

  • offsetof on a array data member, with typedef type or an array
    type.
    
    
    The offset of expression is incorrectly evaluated to zero.
    

Problem conclusion

  • The compile time offset-of evaluation logic has been corrected
    to handle typedefs properly.
    

Temporary fix

Comments

APAR Information

  • APAR number

    LI74408

  • Reported component name

    XL C/C++ RHEL52

  • Reported component ID

    5724U8310

  • Reported release

    A10

  • Status

    CLOSED PER

  • PE

    NoPE

  • HIPER

    NoHIPER

  • Special Attention

    NoSpecatt

  • Submitted date

    2009-03-31

  • Closed date

    2009-03-31

  • Last modified date

    2009-03-31

  • APAR is sysrouted FROM one or more of the following:

    LI74343

  • APAR is sysrouted TO one or more of the following:

Fix information

  • Fixed component name

    XL C/C++ RHEL52

  • Fixed component ID

    5724U8310

Applicable component levels

[{"Business Unit":{"code":"BU054","label":"Systems w\/TPS"},"Product":{"code":"SSJT9L","label":"XL C\/C++"},"Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"A.1","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}}]

Document Information

Modified date:
16 October 2021