From 635e62821b7d399f09b44d7193f0409b17cf686e Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Thu, 18 Aug 2016 17:31:47 -0700 Subject: [PATCH 1/6] Add better edge case testing to MarshallerTest --- src/autowiring/test/MarshallerTest.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/autowiring/test/MarshallerTest.cpp b/src/autowiring/test/MarshallerTest.cpp index abf0d53a1..d1178ae6a 100644 --- a/src/autowiring/test/MarshallerTest.cpp +++ b/src/autowiring/test/MarshallerTest.cpp @@ -24,5 +24,28 @@ TEST_F(MarshallerTest, NegativeValues) { int y = 0xCDCDCDCD; std::string valY = b.marshal(&y); ASSERT_STREQ("-842150451", valY.c_str()); + + int z = 0; + std::string valZ = b.marshal(&z); + ASSERT_STREQ("0", valZ.c_str()); +} + +TEST_F(MarshallerTest, UnsignedValues) { + autowiring::marshaller b; + + int x = 0; + std::string valX = b.marshal(&x); + ASSERT_STREQ("0", valX.c_str()); } +TEST_F(MarshallerTest, RoundTripTest) { + autowiring::marshaller b; + + unsigned int x = 20; + std::string valX = b.marshal(&x); + + unsigned int outX = 0; + b.unmarshal(&outX, valX.c_str()); + + ASSERT_EQ(outX, x); +} \ No newline at end of file From 83e7ced05b627bf7695e592c72934ba3d7633298 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Thu, 18 Aug 2016 18:15:43 -0700 Subject: [PATCH 2/6] Fix returning - for all 0 value integrals --- src/autowiring/marshaller.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/autowiring/marshaller.h b/src/autowiring/marshaller.h index e699bb10a..7969f330e 100644 --- a/src/autowiring/marshaller.h +++ b/src/autowiring/marshaller.h @@ -107,6 +107,9 @@ namespace autowiring { std::string marshal(const void* ptr) const override { std::string retVal; type val = *static_cast(ptr); + if (val == 0) + return "0"; + bool pos = 0 < val; if (!pos) val *= ~0; From f5715f577d8937099658cc805e966cba4c277540 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 19 Aug 2016 14:06:04 -0700 Subject: [PATCH 3/6] Replace custom rolled solution with C++11 standard --- src/autowiring/marshaller.h | 40 ++++--------------------------------- 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/src/autowiring/marshaller.h b/src/autowiring/marshaller.h index 7969f330e..5b13bf2fd 100644 --- a/src/autowiring/marshaller.h +++ b/src/autowiring/marshaller.h @@ -105,46 +105,14 @@ namespace autowiring { typedef typename std::remove_volatile::type type; std::string marshal(const void* ptr) const override { - std::string retVal; - type val = *static_cast(ptr); - if (val == 0) - return "0"; - - bool pos = 0 < val; - if (!pos) - val *= ~0; - for (; val; val /= 10) { - retVal.push_back(static_cast(val % 10 + '0')); - } - if (!pos) - retVal.push_back('-'); - - for ( - auto first = retVal.begin(), last = retVal.end(); - (first != last) && (first != --last); - ++first - ) - std::swap(*first, *last); - return retVal; + const type val = *static_cast(ptr); + return std::to_string(val); } void unmarshal(void* ptr, const char* szValue) const override { type& value = *static_cast(ptr); - bool negative = *szValue == '-'; - if(negative) { - // Skip over the sign, verify we aren't assigning to the wrong field type - szValue++; - if (std::is_unsigned::value) - throw std::range_error("Attempted to set a signed value on an unsigned calibration field"); - } - - for (value = 0; *szValue; szValue++) { - if (*szValue < '0' || '9' < *szValue) - throw std::invalid_argument("String value is not an integer"); - value = *szValue - '0' + value * 10; - } - if(negative) - value *= -1; + char* end = nullptr; + value = strtol(szValue, &end, 10); } void copy(void* lhs, const void* rhs) const override { From 142f89b9f715cfb23264dccc651505510aa3b1e4 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 19 Aug 2016 14:25:19 -0700 Subject: [PATCH 4/6] Fix potential range error, add includes --- src/autowiring/marshaller.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/autowiring/marshaller.h b/src/autowiring/marshaller.h index 5b13bf2fd..f9d5a60e8 100644 --- a/src/autowiring/marshaller.h +++ b/src/autowiring/marshaller.h @@ -7,6 +7,7 @@ #include #include #include +#include #include TYPE_TRAITS_HEADER namespace autowiring { @@ -112,7 +113,12 @@ namespace autowiring { void unmarshal(void* ptr, const char* szValue) const override { type& value = *static_cast(ptr); char* end = nullptr; - value = strtol(szValue, &end, 10); + const auto llvalue = std::strtoll(szValue, &end, 10); + + if (llvalue > std::numeric_limits::max() || llvalue < std::numeric_limits::min()) + throw std::range_error("Overflow error, value is outside the range representable by this type."); + + value = static_cast(llvalue); } void copy(void* lhs, const void* rhs) const override { From 99a7d2718812786319aab814cd1a6bc53b828e0f Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 19 Aug 2016 15:41:26 -0700 Subject: [PATCH 5/6] Fix issues with GCC 4.8 --- src/autowiring/marshaller.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/autowiring/marshaller.h b/src/autowiring/marshaller.h index f9d5a60e8..bcb11d2b7 100644 --- a/src/autowiring/marshaller.h +++ b/src/autowiring/marshaller.h @@ -5,9 +5,11 @@ #include #include #include + +#undef _GLIBCXX_HAVE_BROKEN_VSWPRINTF //Enable to_string in GCC 4.8. #include #include -#include +#include #include TYPE_TRAITS_HEADER namespace autowiring { @@ -113,7 +115,7 @@ namespace autowiring { void unmarshal(void* ptr, const char* szValue) const override { type& value = *static_cast(ptr); char* end = nullptr; - const auto llvalue = std::strtoll(szValue, &end, 10); + const auto llvalue = strtoll(szValue, &end, 10); if (llvalue > std::numeric_limits::max() || llvalue < std::numeric_limits::min()) throw std::range_error("Overflow error, value is outside the range representable by this type."); From 9585ecaed31238354c2a9bb2f52c14d17439a4d9 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 19 Aug 2016 16:15:09 -0700 Subject: [PATCH 6/6] Avoid use of to_string while we sort out WTF is going on with GCC 4.9 on android --- src/autowiring/marshaller.h | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/autowiring/marshaller.h b/src/autowiring/marshaller.h index bcb11d2b7..61943c727 100644 --- a/src/autowiring/marshaller.h +++ b/src/autowiring/marshaller.h @@ -6,7 +6,6 @@ #include #include -#undef _GLIBCXX_HAVE_BROKEN_VSWPRINTF //Enable to_string in GCC 4.8. #include #include #include @@ -108,8 +107,27 @@ namespace autowiring { typedef typename std::remove_volatile::type type; std::string marshal(const void* ptr) const override { - const type val = *static_cast(ptr); - return std::to_string(val); + std::string retVal; + type val = *static_cast(ptr); + if (val == 0) + return "0"; + + bool pos = 0 < val; + if (!pos) + val *= ~0; + for (; val; val /= 10) { + retVal.push_back(static_cast(val % 10 + '0')); + } + if (!pos) + retVal.push_back('-'); + + for ( + auto first = retVal.begin(), last = retVal.end(); + (first != last) && (first != --last); + ++first + ) + std::swap(*first, *last); + return retVal; } void unmarshal(void* ptr, const char* szValue) const override {