aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@yahooinc.com>2022-05-05 11:27:25 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2022-05-06 10:21:52 +0200
commitd28b4544664e8c1bf7d1565d7defd1cceadc776b (patch)
treebfc47302fc8ea5b4633003933918a82787489161
parent570a67ba5543f2fb712728694fc3acb3b68d5dbc (diff)
Avoid ref of nullptr in nbostream::extend() when existing buffer is empty
Add `Array::data()` utility function to get raw buffer pointer instead of going via `operator[]` which always takes a ref; the latter is not well defined if the underlying buffer is nullptr.
-rw-r--r--vespalib/src/vespa/vespalib/objects/nbostream.cpp6
-rw-r--r--vespalib/src/vespa/vespalib/util/array.h2
2 files changed, 5 insertions, 3 deletions
diff --git a/vespalib/src/vespa/vespalib/objects/nbostream.cpp b/vespalib/src/vespa/vespalib/objects/nbostream.cpp
index 1ef63bc5322..ca43027b2b5 100644
--- a/vespalib/src/vespa/vespalib/objects/nbostream.cpp
+++ b/vespalib/src/vespa/vespalib/objects/nbostream.cpp
@@ -128,17 +128,17 @@ void nbostream::compact()
void nbostream::extend(size_t extraSize)
{
- if (&_wbuf[0] != _rbuf.c_str()) {
+ if (_wbuf.data() != _rbuf.c_str()) {
_wbuf.resize(roundUp2inN(_rbuf.size() + extraSize));
compact();
- _rbuf = ConstBufferRef(&_wbuf[0], _wbuf.capacity());
+ _rbuf = ConstBufferRef(_wbuf.data(), _wbuf.capacity());
}
if (_rp != 0) {
compact();
}
if (space() < extraSize) {
_wbuf.resize(roundUp2inN(_wbuf.size() + extraSize));
- _rbuf = ConstBufferRef(&_wbuf[0], _wbuf.capacity());
+ _rbuf = ConstBufferRef(_wbuf.data(), _wbuf.capacity());
}
}
diff --git a/vespalib/src/vespa/vespalib/util/array.h b/vespalib/src/vespa/vespalib/util/array.h
index cb5d5c7cc63..ec91c51b483 100644
--- a/vespalib/src/vespa/vespalib/util/array.h
+++ b/vespalib/src/vespa/vespalib/util/array.h
@@ -137,6 +137,8 @@ public:
}
void reset();
bool empty() const { return _sz == 0; }
+ T * data() noexcept { return static_cast<T *>(_array.get()); }
+ const T * data() const noexcept { return static_cast<const T *>(_array.get()); }
T & operator [] (size_t i) { return *array(i); }
const T & operator [] (size_t i) const { return *array(i); }
bool operator == (const Array & rhs) const;