aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@yahooinc.com>2022-05-25 13:20:52 +0000
committerHåvard Pettersen <havardpe@yahooinc.com>2022-05-25 13:21:39 +0000
commit5f47082852cf0c79a92abc46171cc2818db35d01 (patch)
tree7af850c91f4b797f01e9699441002167cf30a3e1
parent2656098f025f9efbb91e0a06feb66d8d9a0f2997 (diff)
avoid undefined behavior in vespalib unit tests
-rw-r--r--vespalib/src/tests/btree/btree_store/btree_store_test.cpp4
-rw-r--r--vespalib/src/tests/btree/btreeaggregation_test.cpp4
-rw-r--r--vespalib/src/vespa/vespalib/data/memory.h10
-rw-r--r--vespalib/src/vespa/vespalib/data/simple_buffer.cpp2
-rw-r--r--vespalib/src/vespa/vespalib/data/simple_buffer.h2
-rw-r--r--vespalib/src/vespa/vespalib/data/smart_buffer.cpp8
-rw-r--r--vespalib/src/vespa/vespalib/objects/nbostream.h2
-rw-r--r--vespalib/src/vespa/vespalib/stllike/string.h4
-rw-r--r--vespalib/src/vespa/vespalib/util/array.h8
9 files changed, 27 insertions, 17 deletions
diff --git a/vespalib/src/tests/btree/btree_store/btree_store_test.cpp b/vespalib/src/tests/btree/btree_store/btree_store_test.cpp
index 974aafb392a..5e2aa89b59e 100644
--- a/vespalib/src/tests/btree/btree_store/btree_store_test.cpp
+++ b/vespalib/src/tests/btree/btree_store/btree_store_test.cpp
@@ -43,8 +43,8 @@ protected:
additions.emplace_back(i, 0);
}
_store.apply(root,
- &additions[0], &additions[0] + additions.size(),
- &removals[0], &removals[0] + removals.size());
+ additions.data(), additions.data() + additions.size(),
+ removals.data(), removals.data() + removals.size());
return root;
}
static std::vector<int> make_exp_sequence(int start_key, int end_key)
diff --git a/vespalib/src/tests/btree/btreeaggregation_test.cpp b/vespalib/src/tests/btree/btreeaggregation_test.cpp
index b1198e3828d..f4300499fcd 100644
--- a/vespalib/src/tests/btree/btreeaggregation_test.cpp
+++ b/vespalib/src/tests/btree/btreeaggregation_test.cpp
@@ -258,8 +258,8 @@ MyTreeForceApplyStore::remove(EntryRef &ref, const KeyType &key,
std::vector<KeyType> removals;
removals.push_back(key);
apply(ref,
- &additions[0], &additions[additions.size()],
- &removals[0], &removals[removals.size()],
+ additions.data(), additions.data() + additions.size(),
+ removals.data(), removals.data() + removals.size(),
comp);
return retVal;
}
diff --git a/vespalib/src/vespa/vespalib/data/memory.h b/vespalib/src/vespa/vespalib/data/memory.h
index 2765309f4c3..795bbf2ffe7 100644
--- a/vespalib/src/vespa/vespalib/data/memory.h
+++ b/vespalib/src/vespa/vespalib/data/memory.h
@@ -27,9 +27,13 @@ struct Memory
vespalib::string make_string() const;
vespalib::stringref make_stringref() const { return stringref(data, size); }
bool operator == (const Memory &rhs) const noexcept {
- return ((size == rhs.size) &&
- ((data == rhs.data) ||
- (memcmp(data, rhs.data, size) == 0)));
+ if (size != rhs.size) {
+ return false;
+ }
+ if ((size == 0) || (data == rhs.data)) {
+ return true;
+ }
+ return (memcmp(data, rhs.data, size) == 0);
}
};
diff --git a/vespalib/src/vespa/vespalib/data/simple_buffer.cpp b/vespalib/src/vespa/vespalib/data/simple_buffer.cpp
index 38692552575..628b110d2fe 100644
--- a/vespalib/src/vespa/vespalib/data/simple_buffer.cpp
+++ b/vespalib/src/vespa/vespalib/data/simple_buffer.cpp
@@ -16,7 +16,7 @@ SimpleBuffer::~SimpleBuffer() = default;
Memory
SimpleBuffer::obtain()
{
- return Memory(&_data[0], _used);
+ return Memory(_data.data(), _used);
}
Input &
diff --git a/vespalib/src/vespa/vespalib/data/simple_buffer.h b/vespalib/src/vespa/vespalib/data/simple_buffer.h
index f91235894ae..e94a1ceb437 100644
--- a/vespalib/src/vespa/vespalib/data/simple_buffer.h
+++ b/vespalib/src/vespa/vespalib/data/simple_buffer.h
@@ -36,7 +36,7 @@ public:
++_used;
return *this;
}
- Memory get() const { return Memory(&_data[0], _used); }
+ Memory get() const { return Memory(_data.data(), _used); }
bool operator==(const SimpleBuffer &rhs) const { return (get() == rhs.get()); }
};
diff --git a/vespalib/src/vespa/vespalib/data/smart_buffer.cpp b/vespalib/src/vespa/vespalib/data/smart_buffer.cpp
index c0ccc9177da..de079261d6d 100644
--- a/vespalib/src/vespa/vespalib/data/smart_buffer.cpp
+++ b/vespalib/src/vespa/vespalib/data/smart_buffer.cpp
@@ -14,10 +14,14 @@ SmartBuffer::ensure_free(size_t bytes)
if ((unused() < bytes) || ((unused() * 3) < read_len())) {
size_t new_size = std::max(_data.size() * 2, read_len() + bytes);
alloc::Alloc new_buf(alloc::Alloc::alloc(new_size));
- memcpy(new_buf.get(), read_ptr(), read_len());
+ if (read_ptr()) {
+ memcpy(new_buf.get(), read_ptr(), read_len());
+ }
_data.swap(new_buf);
} else {
- memmove(_data.get(), read_ptr(), read_len());
+ if (read_ptr()) {
+ memmove(_data.get(), read_ptr(), read_len());
+ }
}
_write_pos = read_len();
_read_pos = 0;
diff --git a/vespalib/src/vespa/vespalib/objects/nbostream.h b/vespalib/src/vespa/vespalib/objects/nbostream.h
index cc4f91d91b4..e7bed8d59ff 100644
--- a/vespalib/src/vespa/vespalib/objects/nbostream.h
+++ b/vespalib/src/vespa/vespalib/objects/nbostream.h
@@ -145,7 +145,7 @@ public:
size_t capacity() const { return _wbuf.size(); }
bool empty() const { return size() == 0; }
const char * data() const { return &_rbuf[0]; }
- const char * peek() const { return &_rbuf[_rp]; }
+ const char * peek() const { return _rbuf.c_str() + _rp; }
size_t rp() const { return _rp; }
nbostream & rp(size_t pos) { if (pos > _wp) fail(eof); _rp = pos; return *this; }
nbostream & wp(size_t pos) { if (pos > _wbuf.size()) fail(oob); _wp = pos; return *this; }
diff --git a/vespalib/src/vespa/vespalib/stllike/string.h b/vespalib/src/vespa/vespalib/stllike/string.h
index 2e61d7ef0e9..7bf03895a88 100644
--- a/vespalib/src/vespa/vespalib/stllike/string.h
+++ b/vespalib/src/vespa/vespalib/stllike/string.h
@@ -594,7 +594,9 @@ private:
void init(const void *s) noexcept {
if (__builtin_expect(_sz < StackSize, true)) {
_bufferSize = StackSize;
- memcpy(_stack, s, _sz);
+ if (s) {
+ memcpy(_stack, s, _sz);
+ }
_stack[_sz] = '\0';
} else {
init_slower(s);
diff --git a/vespalib/src/vespa/vespalib/util/array.h b/vespalib/src/vespa/vespalib/util/array.h
index ec91c51b483..99bee183485 100644
--- a/vespalib/src/vespa/vespalib/util/array.h
+++ b/vespalib/src/vespa/vespalib/util/array.h
@@ -123,10 +123,10 @@ public:
const_iterator end() const { return array(_sz); }
iterator begin() { return array(0); }
iterator end() { return array(_sz); }
- const_reverse_iterator rbegin() const { return array(_sz) - 1; }
- const_reverse_iterator rend() const { return array(0) - 1; }
- reverse_iterator rbegin() { return array(_sz) - 1; }
- reverse_iterator rend() { return array(0) - 1; }
+ const_reverse_iterator rbegin() const { return empty() ? array(0) : array(_sz) - 1; }
+ const_reverse_iterator rend() const { return empty() ? array(0) : array(0) - 1; }
+ reverse_iterator rbegin() { return empty() ? array(0) : array(_sz) - 1; }
+ reverse_iterator rend() { return empty() ? array(0) : array(0) - 1; }
size_t size() const { return _sz; }
size_t byteSize() const { return _sz * sizeof(T); }
size_t byteCapacity() const { return _array.size(); }