[Mesa-dev] [PATCH 3/3] clover: unsure compat::string is \0 terminated
EdB
edb+mesa at sigluy.net
Thu Aug 7 22:02:09 PDT 2014
otherwise c_str() is not safe
---
src/gallium/state_trackers/clover/util/compat.hpp | 54 ++++++++++++++++++++---
1 file changed, 48 insertions(+), 6 deletions(-)
diff --git a/src/gallium/state_trackers/clover/util/compat.hpp b/src/gallium/state_trackers/clover/util/compat.hpp
index 6f0f7cc..7ca1f85 100644
--- a/src/gallium/state_trackers/clover/util/compat.hpp
+++ b/src/gallium/state_trackers/clover/util/compat.hpp
@@ -197,7 +197,7 @@ namespace clover {
return _p[i];
}
- private:
+ protected:
iterator _p; //memory array
size_type _s; //size
size_type _c; //capacity
@@ -306,18 +306,56 @@ namespace clover {
class string : public vector<char> {
public:
- string() : vector() {
+ string() : vector(0, 1) {
+ _p[_s - 1] = '\0';
}
- string(const char *p) : vector(p, std::strlen(p)) {
+ string(const char *p) : vector(p, std::strlen(p) + 1) {
+ _p[_s - 1] = '\0';
}
template<typename C>
- string(const C &v) : vector(v) {
+ string(const C &v) : vector(&*v.begin(), v.size() + 1) {
+ _p[_s - 1] = '\0';
}
- operator std::string() const {
- return std::string(begin(), end());
+ void
+ reserve(size_type m) {
+ vector::reserve(m + 1);
+ }
+
+ void
+ resize(size_type m, char x = '\0') {
+ vector::resize(m + 1, x);
+ _p[_s - 1] = '\0';
+ }
+
+ void
+ push_back(char &x) {
+ reserve(_s + 1);
+ _p[_s - 1] = x;
+ _p[_s] = '\0';
+ ++_s;
+ }
+
+ size_type
+ size() const {
+ return _s - 1;
+ }
+
+ size_type
+ capacity() const {
+ return _c - 1;
+ }
+
+ iterator
+ end() {
+ return _p + size();
+ }
+
+ const_iterator
+ end() const {
+ return _p + size();
}
const char *
@@ -325,6 +363,10 @@ namespace clover {
return begin();
}
+ operator std::string() const {
+ return std::string(begin(), end());
+ }
+
const char *
find(const string &s) const {
for (size_t i = 0; i + s.size() < size(); ++i) {
--
2.0.4
More information about the mesa-dev
mailing list