Fix signed to unsigned conversion
Summary: `folly::to<uint32_t>()` and `folly::to<uint64_t>()` were silently failing and truncating the input when negative values were passed: ``` int8_t x = -1; folly::to<uint8_t>(x); // THROWS int16_t x = -1; folly::to<uint16_t>(x); // THROWS ``` all throw folly::ConversionError, but ``` int32_t x = -1; folly::to<uint32_t>(x); // DOES NOT THROW int64_t x = -1; folly::to<uint64_t>(x); // DOES NOT THROW ``` The actual bug is in `less_than<>()`, from Traits.h, which in these cases relies that `(uint32)0 <= std::numeric_limits<int32_t>::min()`, which doesn't hold according to the standard. The fixes in this diff are: - Fix `less_than<>()` to properly support signed to unsigned conversions - Fix `less_than<>()` to properly support unsigned to signed conversions - Fix `greater_than<>()` to properly support signed to unsigned conversions - Fix `greater_than<>()` to properly support unsigned to signed conversions - Add unit tests to cover all these cases. Reviewed By: yfeldblum Differential Revision: D19511557 fbshipit-source-id: 48649dfcdadeaa652c8f57f7b11481de44a88927
Showing
Please register or sign in to comment