A Discussion of the HDF5 Library’s Handling of

Unsigned Integer Overflow

                                    Quincey Koziol and Raymond Lu

                                                July 6, 2005

 

The background information of this discussion can be found in the Data Conversion of Arithmetic Data Types document.

 

Introduction

 

During the development of the data conversion, the HDF5 library has adopted its own way to handle overflow and underflow when the original values cannot be represented by the destination.  For some data conversions, the C standard does not define the way to handle overflow or underflow.  But for some other conversions, it clearly states what values should be assigned to the destination. 

 

In the data conversion routines of the HDF5 library, the only significant difference from the C standard is converting an integer, either signed or unsigned, to an unsigned integer.  In this case, the library assigns the maximal value to the destination when overflow happens.  If the source is a negative value, the value 0 is assigned to the destination.

 

However, the C manual says, “If the result type is an unsigned type, then the result must be that unique value of the result type that is equal (congruent) mod 2n to the original value, where n is equal to the number of bits used in the representation of the result type.  If the destination type is longer than the source type, then the only case in which the source value will not be representable in the result type is when a negative signed value is converted to a longer, unsigned type.  In that case, the conversion must necessarily behave as if the source value were first converted to a longer signed type of the same size as the destination type and then converted to the destination type.

 

So we will need a discussion to find out a good way to cover this difference.    

 

Solutions

 

Three possible solutions are discussed below with no implication of preference in the order.

 

  1. Leaving the library as it is:  There will be no change to the library.  We will simply document this arrangement.  We have not received any complaint or question from our users.  It may be acceptable to them.

 

  1. Changing the library to comply with the C standard:  The library’s behavior will be different from before.  It is a question whether some users can accept this change.

 

  1. Offering the option of C standard overflow:  We can provide an exception handling function when this kind of overflow happens.  In this exception handling function, the C standard value will be assigned to the destination.  We will document this function to let our users know about it.

 

None of the above solutions will cause a major change to the library.  None of them will take much time to implement.