Skip to content

Commit

Permalink
Sign extend arguments of smaller types passed to %ll? (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jan 27, 2016
1 parent ae6368c commit 7ee287d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
7 changes: 3 additions & 4 deletions format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
4 changes: 2 additions & 2 deletions test/printf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7ee287d

Please sign in to comment.