Skip to content

Commit 7ed7af9

Browse files
committed
Fix timespec_object.*_32bit_sec tests on 32-bit platforms
On 32-bit unix platforms, 0xffffffffUL is a 32-bit value so the compiler complains about converting it to a signed value. /home/runner/work/msgpack-c/msgpack-c/test/msgpack_cpp11.cpp:1085:20: error: constant expression evaluates to 4294967295 which cannot be narrowed to type '__time_t' (aka 'long') [-Wc++11-narrowing] timespec val1{ 0xffffffffUL, 0 }; ^~~~~~~~~~~~ /home/runner/work/msgpack-c/msgpack-c/test/msgpack_cpp11.cpp:1085:20: note: insert an explicit cast to silence this issue timespec val1{ 0xffffffffUL, 0 }; ^~~~~~~~~~~~ static_cast<__time_t>( ) /home/runner/work/msgpack-c/msgpack-c/test/msgpack_cpp11.cpp:1085:20: warning: implicit conversion changes signedness: 'unsigned long' to '__time_t' (aka 'long') [-Wsign-conversion] timespec val1{ 0xffffffffUL, 0 }; ~ ^~~~~~~~~~~~ Since we're trying to test how the maximum 32-bit value that fits in timespec.tv_sec is handled, directly use the maximum 32-bit value for the appropriate (un)signed type used for timespec.tv_sec. We don't just cast to the value, as the compiler suggests, because that would result in an extremely negative value.
1 parent 4a94f83 commit 7ed7af9

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

test/msgpack_cpp11.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ TEST(MSGPACK_TIMESPEC, timespec_object_with_zone_zero)
10821082
TEST(MSGPACK_TIMESPEC, timespec_pack_convert_32bit_sec)
10831083
{
10841084
std::stringstream ss;
1085-
timespec val1{ 0xffffffffUL, 0 };
1085+
timespec val1{ std::numeric_limits<decltype(std::declval<timespec>().tv_sec)>::is_signed ? INT32_MAX : UINT32_MAX, 0 };
10861086

10871087
msgpack::pack(ss, val1);
10881088
std::string const& str = ss.str();
@@ -1098,7 +1098,7 @@ TEST(MSGPACK_TIMESPEC, timespec_pack_convert_32bit_sec)
10981098
TEST(MSGPACK_TIMESPEC, timespec_object_with_zone_32bit_sec)
10991099
{
11001100
msgpack::zone z;
1101-
timespec val1{ 0xffffffffUL, 0 };
1101+
timespec val1{ std::numeric_limits<decltype(std::declval<timespec>().tv_sec)>::is_signed ? INT32_MAX : UINT32_MAX, 0 };
11021102
msgpack::object obj(val1, z);
11031103
timespec val2 = obj.as<timespec>();
11041104
EXPECT_EQ(val1.tv_sec, val2.tv_sec);

0 commit comments

Comments
 (0)