A Discussion
of the HDF5 Library’s Handling of
Unsigned
Integer Overflow
Quincey Koziol and Raymond Lu
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.
None of the above solutions will cause a major change to the library. None of them will take much time to implement.