Skip to content

Commit d122179

Browse files
SolidWallOfCodezwoop
authored andcommitted
MemView: Fix coverity issues, constexpr correctness.
1 parent a0390f7 commit d122179

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

lib/ts/MemView.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ svtoi(StringView src, StringView *out, int base)
6565

6666
intmax_t zret = 0;
6767

68-
if (*out)
68+
if (out)
6969
out->clear();
7070
if (!(1 < base && base <= 36))
7171
return 0;

lib/ts/MemView.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,20 @@ class MemView
8888
);
8989

9090
/** Construct from a half open range of two pointers.
91-
@note The byte at @start is in the view but the byte at @a end is not.
91+
@note The instance at @start is in the view but the instance at @a end is not.
9292
*/
93-
constexpr MemView(const void *start, ///< First byte in the view.
94-
const void *end ///< First byte not in the view.
93+
template <typename T>
94+
constexpr MemView(T const *start, ///< First byte in the view.
95+
T const *end ///< First byte not in the view.
9596
);
9697

98+
/** Construct from a half open range of two pointers.
99+
@note The instance at @start is in the view but the instance at @a end is not.
100+
*/
101+
MemView(void const *start, ///< First byte in the view.
102+
void const *end ///< First byte not in the view.
103+
);
104+
97105
/** Construct from nullptr.
98106
This implicitly makes the length 0.
99107
*/
@@ -305,7 +313,8 @@ class StringView
305313
explicit StringView(const char *s);
306314

307315
/// Construct from @c MemView to reference the same view.
308-
constexpr StringView(MemView const &that);
316+
/// @internal Can't be @c constexpr because @c static_cast of @c <void*> is not permitted.
317+
StringView(MemView const &that);
309318

310319
/// Construct from @c std::string, referencing the entire string contents.
311320
StringView(std::string const &str);
@@ -595,8 +604,13 @@ inline constexpr MemView::MemView()
595604
inline constexpr MemView::MemView(void const *ptr, size_t n) : _ptr(ptr), _size(n)
596605
{
597606
}
598-
inline constexpr MemView::MemView(void const *start, void const *end)
599-
: _ptr(start), _size(static_cast<const char *>(end) - static_cast<const char *>(start))
607+
template <typename T> constexpr MemView::MemView(const T *start, const T *end) : _ptr(start), _size((end - start) * sizeof(T))
608+
{
609+
}
610+
// <void*> is magic, handle that specially.
611+
// No constexpr because the spec specifically forbids casting from <void*> to a typed pointer.
612+
inline MemView::MemView(void const *start, void const *end)
613+
: _ptr(start), _size(static_cast<const char *>(end) - static_cast<char const *>(start))
600614
{
601615
}
602616
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))
820834
inline constexpr StringView::StringView(std::nullptr_t) : _ptr(nullptr), _size(0)
821835
{
822836
}
823-
inline constexpr StringView::StringView(MemView const &that) : _ptr(static_cast<const char *>(that.ptr())), _size(that.size())
837+
inline StringView::StringView(MemView const &that) : _ptr(static_cast<const char *>(that.ptr())), _size(that.size())
824838
{
825839
}
826840
inline StringView::StringView(std::string const &str) : _ptr(str.data()), _size(str.size())
@@ -1055,24 +1069,22 @@ StringView::suffix(const char *p) const
10551069
return zret;
10561070
}
10571071

1058-
// gcc 4.9 - it considers passing this->find(...) to suffix() to be amibugous between the const char*
1059-
// overload and the std::function<bool (char)>. This shows up on Debian 7, so let's try a cast to help out.
10601072
inline auto
10611073
StringView::suffix(char c) -> self
10621074
{
1063-
return this->suffix(static_cast<const char *>(this->find(c)));
1075+
return this->suffix(this->find(c));
10641076
}
10651077

10661078
inline auto
10671079
StringView::suffix(self delimiters) -> self
10681080
{
1069-
return this->suffix(static_cast<const char *>(this->find(delimiters)));
1081+
return this->suffix(this->find(delimiters));
10701082
}
10711083

10721084
inline auto
10731085
StringView::suffix(std::function<bool(char)> const &pred) -> self
10741086
{
1075-
return this->suffix(static_cast<const char *>(this->find(pred)));
1087+
return this->suffix(this->find(pred));
10761088
}
10771089

10781090
inline StringView

lib/ts/test_MemView.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ Test_1()
4545
return true;
4646
}
4747

48+
// These tests are purely compile time.
49+
void
50+
Test_Compile()
51+
{
52+
int i[12];
53+
char c[29];
54+
void *x = i, *y = i + 12;
55+
MemView mvi(i, i + 12);
56+
MemView mci(c, c + 29);
57+
MemView mcv(x, y);
58+
}
59+
4860
int
4961
main(int, char *argv[])
5062
{

0 commit comments

Comments
 (0)