folly: ubsan: format: avoid UB in abs(-INT_MIN)
authorLucian Grijincu <lucian@fb.com>
Wed, 11 May 2016 17:45:33 +0000 (10:45 -0700)
committerFacebook Github Bot 6 <facebook-github-bot-6-bot@fb.com>
Wed, 11 May 2016 17:50:21 +0000 (10:50 -0700)
Summary:
abs(-INT_MIN) is UB. Avoid it by first casting the number to
its unsigned equivalent and then negating that.

Reviewed By: yfeldblum

Differential Revision: D3280835

fbshipit-source-id: 40922d686173e6467e15d5a6dc2b62ad718349b5

folly/Format-inl.h

index 35c0298e14fa0b81f955fad893412d2e390f9608..48125dc5df3529dcc1cb154d1666a62367cf1208 100644 (file)
@@ -443,7 +443,7 @@ class FormatValue<
     char sign;
     if (std::is_signed<T>::value) {
       if (folly::is_negative(val_)) {
-        uval = static_cast<UT>(-val_);
+        uval = -static_cast<UT>(val_);
         sign = '-';
       } else {
         uval = static_cast<UT>(val_);