From bc64e50bd75479b16cdbcd16e0ce151ead89dddd Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Wed, 22 Aug 2018 15:26:20 -0700 Subject: [PATCH] Assert that ParseInt/ParseUint are only used with signed/unsigned numbers respectively Test: build fails when the signedness is mismatched Test: build succeeds otherwise Change-Id: Idd6b146cc167d4607eafc81dbad6c2a79b167094 --- base/include/android-base/parseint.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/base/include/android-base/parseint.h b/base/include/android-base/parseint.h index 9444fddf0..be8b97b78 100644 --- a/base/include/android-base/parseint.h +++ b/base/include/android-base/parseint.h @@ -22,6 +22,7 @@ #include #include +#include namespace android { namespace base { @@ -33,6 +34,7 @@ namespace base { template bool ParseUint(const char* s, T* out, T max = std::numeric_limits::max(), bool allow_suffixes = false) { + static_assert(std::is_unsigned::value, "ParseUint can only be used with unsigned types"); while (isspace(*s)) { s++; } @@ -96,6 +98,7 @@ template bool ParseInt(const char* s, T* out, T min = std::numeric_limits::min(), T max = std::numeric_limits::max()) { + static_assert(std::is_signed::value, "ParseInt can only be used with signed types"); while (isspace(*s)) { s++; }