Opened 8 years ago

Closed 8 years ago

#6535 closed defect (fixed)

HDF4 type sizes not safe

Reported by: Kurt Schwehr Owned by: Kurt Schwehr
Priority: normal Milestone:
Component: default Version: svn-trunk
Severity: normal Keywords:
Cc:

Description (last modified by Kurt Schwehr)

The sizes of char, short, int and GBigInt are not a specific bit size, so this tries to make it a little bit more predictable for oddball build envs by using GByte, GInt16/GUInt16, GInt32/GUInt32. It also adds 2 static_asserts.

Thoughts?

  • hdf4dataset.cpp

     
    298298
    299299double HDF4Dataset::AnyTypeToDouble( int32 iNumType, void *pData )
    300300{
     301    CPL_STATIC_ASSERT(sizeof(GIntBig) == 8);
    301302    switch ( iNumType )
    302303    {
    303304        case DFNT_INT8:
    304305            return static_cast<double>(*reinterpret_cast<char *>(pData));
    305306        case DFNT_UINT8:
    306             return static_cast<double>
    307                 (*reinterpret_cast<unsigned char *>(pData));
     307            return static_cast<double>(*reinterpret_cast<GByte *>(pData));
    308308        case DFNT_INT16:
    309             return static_cast<double>(*reinterpret_cast<short *>(pData));
     309            return static_cast<double>(*reinterpret_cast<GInt16 *>(pData));
    310310        case DFNT_UINT16:
    311             return static_cast<double>
    312                 (*reinterpret_cast<unsigned short *>(pData));
     311            return static_cast<double>(*reinterpret_cast<GUInt16 *>(pData));
    313312        case DFNT_INT32:
    314             return static_cast<double>(*reinterpret_cast<int *>(pData));
     313            return static_cast<double>(*reinterpret_cast<GInt32 *>(pData));
    315314        case DFNT_UINT32:
    316             return static_cast<double>
    317                 (*reinterpret_cast<unsigned int *>(pData));
     315            return static_cast<double>(*reinterpret_cast<GUInt32 *>(pData));
    318316        case DFNT_INT64:
    319317            return static_cast<double>(*reinterpret_cast<GIntBig *>(pData));
    320318        case DFNT_UINT64:
    321319

Change History (4)

comment:1 by Even Rouault, 8 years ago

"sizeof(char) == 1" is guaranteed by the C/C++ standards as far as I recall.

As far as sizeof(GIntBig) == 8, I'm pretty sure there are a lot of places where this assumption would need to be true, so I'm not sure it's worth putting it in that particular place.

OK for other changes. (But GInt16 is just typedef short GInt16)

comment:2 by Kurt Schwehr, 8 years ago

Description: modified (diff)
Status: newassigned

comment:3 by Kurt Schwehr, 8 years ago

comment:4 by Even Rouault, 8 years ago

Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.