Commit 6d81f014764e3bb9452d3697b84093bed0668f02
1 parent
8c34d67b
Don't assume char is signed in int conversion tests (fixes #361)
Showing
4 changed files
with
21 additions
and
5 deletions
ChangeLog
include/qpdf/QIntC.hh
| @@ -51,6 +51,13 @@ namespace QIntC // QIntC = qpdf Integer Conversion | @@ -51,6 +51,13 @@ namespace QIntC // QIntC = qpdf Integer Conversion | ||
| 51 | }; | 51 | }; |
| 52 | 52 | ||
| 53 | template <> | 53 | template <> |
| 54 | + class to_u<signed char> | ||
| 55 | + { | ||
| 56 | + public: | ||
| 57 | + typedef unsigned char type; | ||
| 58 | + }; | ||
| 59 | + | ||
| 60 | + template <> | ||
| 54 | class to_u<short> | 61 | class to_u<short> |
| 55 | { | 62 | { |
| 56 | public: | 63 | public: |
libtests/qintc.cc
| @@ -32,12 +32,13 @@ int main() | @@ -32,12 +32,13 @@ int main() | ||
| 32 | uint64_t ul1 = 1099511627776LL; // Too big for 32-bit | 32 | uint64_t ul1 = 1099511627776LL; // Too big for 32-bit |
| 33 | uint64_t ul2 = 12345; // Fits into 32-bit | 33 | uint64_t ul2 = 12345; // Fits into 32-bit |
| 34 | int32_t i2 = 81; // Fits in char and uchar | 34 | int32_t i2 = 81; // Fits in char and uchar |
| 35 | - char c1 = '\xf7'; // Signed value when char | 35 | + signed char c1 = static_cast<signed char>('\xf7'); // Signed value when char |
| 36 | + char c2 = 'W'; // char; may be signed or unsigned | ||
| 36 | 37 | ||
| 37 | // Verify i1 and u1 have same bit pattern | 38 | // Verify i1 and u1 have same bit pattern |
| 38 | assert(static_cast<uint32_t>(i1) == u1); | 39 | assert(static_cast<uint32_t>(i1) == u1); |
| 39 | - // Verify that we can unsafely convert between char and unsigned char | ||
| 40 | - assert(c1 == static_cast<char>(static_cast<unsigned char>(c1))); | 40 | + // Verify that we can unsafely convert between signed and unsigned char |
| 41 | + assert(c1 == static_cast<signed char>(static_cast<unsigned char>(c1))); | ||
| 41 | 42 | ||
| 42 | try_convert(true, QIntC::to_int<int32_t>, i1); | 43 | try_convert(true, QIntC::to_int<int32_t>, i1); |
| 43 | try_convert(true, QIntC::to_uint<uint32_t>, u1); | 44 | try_convert(true, QIntC::to_uint<uint32_t>, u1); |
| @@ -51,7 +52,9 @@ int main() | @@ -51,7 +52,9 @@ int main() | ||
| 51 | try_convert(false, QIntC::to_ulonglong<int32_t>, i1); | 52 | try_convert(false, QIntC::to_ulonglong<int32_t>, i1); |
| 52 | try_convert(true, QIntC::to_char<int32_t>, i2); | 53 | try_convert(true, QIntC::to_char<int32_t>, i2); |
| 53 | try_convert(true, QIntC::to_uchar<int32_t>, i2); | 54 | try_convert(true, QIntC::to_uchar<int32_t>, i2); |
| 54 | - try_convert(false, QIntC::to_uchar<char>, c1); | 55 | + try_convert(false, QIntC::to_uchar<signed char>, c1); |
| 56 | + try_convert(true, QIntC::to_uchar<char>, c2); | ||
| 57 | + try_convert(true, QIntC::to_char<char>, c2); | ||
| 55 | 58 | ||
| 56 | return 0; | 59 | return 0; |
| 57 | } | 60 | } |
libtests/qtest/qintc/qintc.out
| @@ -10,4 +10,6 @@ QIntC::to_offset<int32_t>(i1): -1153374643 -1153374643 PASSED | @@ -10,4 +10,6 @@ QIntC::to_offset<int32_t>(i1): -1153374643 -1153374643 PASSED | ||
| 10 | QIntC::to_ulonglong<int32_t>(i1): integer out of range converting -1153374643 from a 4-byte signed type to a 8-byte unsigned type PASSED | 10 | QIntC::to_ulonglong<int32_t>(i1): integer out of range converting -1153374643 from a 4-byte signed type to a 8-byte unsigned type PASSED |
| 11 | QIntC::to_char<int32_t>(i2): 81 Q PASSED | 11 | QIntC::to_char<int32_t>(i2): 81 Q PASSED |
| 12 | QIntC::to_uchar<int32_t>(i2): 81 Q PASSED | 12 | QIntC::to_uchar<int32_t>(i2): 81 Q PASSED |
| 13 | -QIntC::to_uchar<char>(c1): integer out of range converting ÷ from a 1-byte signed type to a 1-byte unsigned type PASSED | 13 | +QIntC::to_uchar<signed char>(c1): integer out of range converting ÷ from a 1-byte signed type to a 1-byte unsigned type PASSED |
| 14 | +QIntC::to_uchar<char>(c2): W W PASSED | ||
| 15 | +QIntC::to_char<char>(c2): W W PASSED |