Sign extend arguments of smaller types passed to %ll? (#265)

This commit is contained in:
vitaut 2016-01-27 07:03:19 -08:00
parent ae6368c985
commit 7ee287d3d9
2 changed files with 5 additions and 6 deletions

View File

@ -328,11 +328,10 @@ class ArgConverter : public fmt::internal::ArgVisitor<ArgConverter<T>, void> {
} else {
if (is_signed) {
arg_.type = Arg::LONG_LONG;
// Convert value to unsigned type before converting to long long for
// consistency with glibc's printf even though in general it's UB:
// glibc's printf doesn't sign extend arguments of smaller types:
// std::printf("%lld", -42); // prints "4294967254"
arg_.long_long_value =
static_cast<typename fmt::internal::MakeUnsigned<U>::Type>(value);
// but we don't have to do the same because it's a UB.
arg_.long_long_value = value;
} else {
arg_.type = Arg::ULONG_LONG;
arg_.ulong_long_value =

View File

@ -306,8 +306,8 @@ void TestLength(const char *length_spec, U value) {
}
using fmt::internal::MakeUnsigned;
if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
signed_value = unsigned_value =
static_cast<typename MakeUnsigned<unsigned>::Type>(value);
signed_value = value;
unsigned_value = static_cast<typename MakeUnsigned<unsigned>::Type>(value);
} else {
signed_value = static_cast<typename MakeSigned<T>::Type>(value);
unsigned_value = static_cast<typename MakeUnsigned<T>::Type>(value);