From 4d6e0b1a427770dc739542bd56d1a4e693a4007c Mon Sep 17 00:00:00 2001 From: "Alan M. Carroll" Date: Wed, 15 Feb 2017 09:56:19 -0600 Subject: [PATCH] MemView: Fix coverity issues, constexpr correctness. --- lib/ts/MemView.cc | 2 +- lib/ts/MemView.h | 36 ++++++++++++++++++++++++------------ lib/ts/test_MemView.cc | 12 ++++++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/lib/ts/MemView.cc b/lib/ts/MemView.cc index 0d62765e495..980a01ae388 100644 --- a/lib/ts/MemView.cc +++ b/lib/ts/MemView.cc @@ -65,7 +65,7 @@ svtoi(StringView src, StringView *out, int base) intmax_t zret = 0; - if (*out) + if (out) out->clear(); if (!(1 < base && base <= 36)) return 0; diff --git a/lib/ts/MemView.h b/lib/ts/MemView.h index 1132f4aa0fd..de9e6357f8e 100644 --- a/lib/ts/MemView.h +++ b/lib/ts/MemView.h @@ -88,12 +88,20 @@ class MemView ); /** Construct from a half open range of two pointers. - @note The byte at @start is in the view but the byte at @a end is not. + @note The instance at @start is in the view but the instance at @a end is not. */ - constexpr MemView(const void *start, ///< First byte in the view. - const void *end ///< First byte not in the view. + template + constexpr MemView(T const *start, ///< First byte in the view. + T const *end ///< First byte not in the view. ); + /** Construct from a half open range of two pointers. + @note The instance at @start is in the view but the instance at @a end is not. + */ + MemView(void const *start, ///< First byte in the view. + void const *end ///< First byte not in the view. + ); + /** Construct from nullptr. This implicitly makes the length 0. */ @@ -305,7 +313,8 @@ class StringView explicit StringView(const char *s); /// Construct from @c MemView to reference the same view. - constexpr StringView(MemView const &that); + /// @internal Can't be @c constexpr because @c static_cast of @c is not permitted. + StringView(MemView const &that); /// Construct from @c std::string, referencing the entire string contents. StringView(std::string const &str); @@ -595,8 +604,13 @@ inline constexpr MemView::MemView() inline constexpr MemView::MemView(void const *ptr, size_t n) : _ptr(ptr), _size(n) { } -inline constexpr MemView::MemView(void const *start, void const *end) - : _ptr(start), _size(static_cast(end) - static_cast(start)) +template constexpr MemView::MemView(const T *start, const T *end) : _ptr(start), _size((end - start) * sizeof(T)) +{ +} +// is magic, handle that specially. +// No constexpr because the spec specifically forbids casting from to a typed pointer. +inline MemView::MemView(void const *start, void const *end) + : _ptr(start), _size(static_cast(end) - static_cast(start)) { } inline constexpr MemView::MemView(std::nullptr_t) : _ptr(nullptr), _size(0) @@ -820,7 +834,7 @@ inline StringView::StringView(const char *s) : _ptr(s), _size(strlen(s)) inline constexpr StringView::StringView(std::nullptr_t) : _ptr(nullptr), _size(0) { } -inline constexpr StringView::StringView(MemView const &that) : _ptr(static_cast(that.ptr())), _size(that.size()) +inline StringView::StringView(MemView const &that) : _ptr(static_cast(that.ptr())), _size(that.size()) { } inline StringView::StringView(std::string const &str) : _ptr(str.data()), _size(str.size()) @@ -1055,24 +1069,22 @@ StringView::suffix(const char *p) const return zret; } -// gcc 4.9 - it considers passing this->find(...) to suffix() to be amibugous between the const char* -// overload and the std::function. This shows up on Debian 7, so let's try a cast to help out. inline auto StringView::suffix(char c) -> self { - return this->suffix(static_cast(this->find(c))); + return this->suffix(this->find(c)); } inline auto StringView::suffix(self delimiters) -> self { - return this->suffix(static_cast(this->find(delimiters))); + return this->suffix(this->find(delimiters)); } inline auto StringView::suffix(std::function const &pred) -> self { - return this->suffix(static_cast(this->find(pred))); + return this->suffix(this->find(pred)); } inline StringView diff --git a/lib/ts/test_MemView.cc b/lib/ts/test_MemView.cc index 57774f82306..ca3ee1afb90 100644 --- a/lib/ts/test_MemView.cc +++ b/lib/ts/test_MemView.cc @@ -45,6 +45,18 @@ Test_1() return true; } +// These tests are purely compile time. +void +Test_Compile() +{ + int i[12]; + char c[29]; + void *x = i, *y = i + 12; + MemView mvi(i, i + 12); + MemView mci(c, c + 29); + MemView mcv(x, y); +} + int main(int, char *argv[]) {