summaryrefslogtreecommitdiffstats
path: root/vdslib
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@oath.com>2018-07-25 14:24:21 +0000
committerTor Brede Vekterli <vekterli@oath.com>2018-07-25 14:24:21 +0000
commit12c8f3005e202b31a8e0ff3816ce9d714a269046 (patch)
treea5d9ba0eedc49df2cea2dbdfea677c4c37ed3775 /vdslib
parente3af3d215feb1e416b27b92bbf421dde281f3a09 (diff)
Remove stringref::c_str()
The expected semantics of c_str() (a null-terminated string) cannot be satisfied with a string reference, so remove the function entirely to prevent people from using it in buggy ways. Replaces c_str() with data() in places where it is presumed safe, otherwise constructs temporary string instances. Certain callsites have been de-stringref'd in favor of regular strings, in particular where C APIs have been transitively called. The vast majority of these were called with string parameters anyway, so should not cause much extra allocation.
Diffstat (limited to 'vdslib')
-rw-r--r--vdslib/src/tests/distribution/distributiontest.cpp2
-rw-r--r--vdslib/src/tests/distribution/grouptest.cpp2
-rw-r--r--vdslib/src/vespa/vdslib/container/parameters.h2
-rw-r--r--vdslib/src/vespa/vdslib/distribution/redundancygroupdistribution.cpp4
-rw-r--r--vdslib/src/vespa/vdslib/state/clusterstate.cpp12
-rw-r--r--vdslib/src/vespa/vdslib/state/clusterstate.h2
-rw-r--r--vdslib/src/vespa/vdslib/state/diskstate.cpp2
-rw-r--r--vdslib/src/vespa/vdslib/state/nodestate.cpp2
8 files changed, 15 insertions, 13 deletions
diff --git a/vdslib/src/tests/distribution/distributiontest.cpp b/vdslib/src/tests/distribution/distributiontest.cpp
index 3090910d2bb..7af48c1904b 100644
--- a/vdslib/src/tests/distribution/distributiontest.cpp
+++ b/vdslib/src/tests/distribution/distributiontest.cpp
@@ -191,7 +191,7 @@ namespace {
try{
std::vector<uint16_t> nvect;
distribution.getIdealNodes(nodeType, state, results[i].bucket,
- nvect, upStates.c_str(), redundancy);
+ nvect, upStates.data(), redundancy);
IdealNodeList nodes;
for (uint32_t j=0, m=nvect.size(); j<m; ++j) {
nodes.push_back(Node(nodeType, nvect[j]));
diff --git a/vdslib/src/tests/distribution/grouptest.cpp b/vdslib/src/tests/distribution/grouptest.cpp
index c108a1e8a38..71d8b8bac0c 100644
--- a/vdslib/src/tests/distribution/grouptest.cpp
+++ b/vdslib/src/tests/distribution/grouptest.cpp
@@ -30,7 +30,7 @@ namespace {
vespalib::StringTokenizer st(nodelist, ",");
std::vector<uint16_t> nodes(st.size());
for (uint32_t i=0; i<st.size(); ++i) {
- nodes[i] = atoi(st[i].c_str());
+ nodes[i] = atoi(st[i].data());
}
group->setNodes(nodes);
return group;
diff --git a/vdslib/src/vespa/vdslib/container/parameters.h b/vdslib/src/vespa/vdslib/container/parameters.h
index 4572b2f6f1c..ab65932496a 100644
--- a/vdslib/src/vespa/vdslib/container/parameters.h
+++ b/vdslib/src/vespa/vdslib/container/parameters.h
@@ -68,7 +68,7 @@ public:
ParametersMap::const_iterator begin() const { return _parameters.begin(); }
ParametersMap::const_iterator end() const { return _parameters.end(); }
/// Convenience from earlier use.
- void set(const KeyT & id, const vespalib::stringref & value) { _parameters[id] = Value(value.c_str(), value.size()); }
+ void set(const KeyT & id, const vespalib::stringref & value) { _parameters[id] = Value(value.data(), value.size()); }
vespalib::stringref get(const KeyT & id, const vespalib::stringref & def = "") const;
/**
* Set the value identified by the id given. This requires the type to be
diff --git a/vdslib/src/vespa/vdslib/distribution/redundancygroupdistribution.cpp b/vdslib/src/vespa/vdslib/distribution/redundancygroupdistribution.cpp
index f620dc15928..99974225e9e 100644
--- a/vdslib/src/vespa/vdslib/distribution/redundancygroupdistribution.cpp
+++ b/vdslib/src/vespa/vdslib/distribution/redundancygroupdistribution.cpp
@@ -30,7 +30,7 @@ namespace {
firstAsterisk = i;
continue;
}
- uint32_t number = atoi(st[i].c_str());
+ uint32_t number = atoi(vespalib::string(st[i]).c_str());
if (number <= 0 || number >= 256) {
throw vespalib::IllegalArgumentException(
"Illegal distribution spec \"" + serialized + "\". "
@@ -48,7 +48,7 @@ namespace {
}
}
- std::vector<uint16_t> parse(vespalib::stringref& serialized) {
+ std::vector<uint16_t> parse(vespalib::stringref serialized) {
std::vector<uint16_t> result;
if (serialized == "") return result;
vespalib::StringTokenizer st(serialized, "|");
diff --git a/vdslib/src/vespa/vdslib/state/clusterstate.cpp b/vdslib/src/vespa/vdslib/state/clusterstate.cpp
index fbc9943e22d..e50b18a5a82 100644
--- a/vdslib/src/vespa/vdslib/state/clusterstate.cpp
+++ b/vdslib/src/vespa/vdslib/state/clusterstate.cpp
@@ -88,7 +88,7 @@ ClusterState::ClusterState(const vespalib::string& serialized)
if (key.empty() || ! parse(key, value, nodeData) ) {
LOG(debug, "Unknown key %s in systemstate. Ignoring it, assuming it's "
"a new feature from a newer version than ourself: %s",
- key.c_str(), serialized.c_str());
+ vespalib::string(key).c_str(), serialized.c_str());
}
}
nodeData.addTo(_nodeStates, _nodeCount);
@@ -106,13 +106,13 @@ ClusterState::parse(vespalib::stringref key, vespalib::stringref value, NodeData
break;
case 'b':
if (key == "bits") {
- _distributionBits = atoi(value.c_str());
+ _distributionBits = atoi(value.data());
return true;
}
break;
case 'v':
if (key == "version") {
- _version = atoi(value.c_str());
+ _version = atoi(value.data());
return true;
}
break;
@@ -145,7 +145,7 @@ ClusterState::parseSorD(vespalib::stringref key, vespalib::stringref value, Node
if (nodeType == 0) return false;
if (dot == vespalib::string::npos) { // Entry that set node counts
uint16_t nodeCount = 0;
- nodeCount = atoi(value.c_str());
+ nodeCount = atoi(value.data());
if (nodeCount > _nodeCount[*nodeType] ) {
_nodeCount[*nodeType] = nodeCount;
@@ -155,9 +155,9 @@ ClusterState::parseSorD(vespalib::stringref key, vespalib::stringref value, Node
vespalib::string::size_type dot2 = key.find('.', dot + 1);
Node node;
if (dot2 == vespalib::string::npos) {
- node = Node(*nodeType, atoi(key.substr(dot + 1).c_str()));
+ node = Node(*nodeType, atoi(key.substr(dot + 1).data()));
} else {
- node = Node(*nodeType, atoi(key.substr(dot + 1, dot2 - dot - 1).c_str()));
+ node = Node(*nodeType, atoi(key.substr(dot + 1, dot2 - dot - 1).data()));
}
if (node.getIndex() >= _nodeCount[*nodeType]) {
diff --git a/vdslib/src/vespa/vdslib/state/clusterstate.h b/vdslib/src/vespa/vdslib/state/clusterstate.h
index 9e8dd0f292a..26c6f1b95ef 100644
--- a/vdslib/src/vespa/vdslib/state/clusterstate.h
+++ b/vdslib/src/vespa/vdslib/state/clusterstate.h
@@ -72,7 +72,9 @@ public:
const std::string& indent = "") const;
private:
+ // Preconditions: `key` and `value` MUST point into null-terminated strings.
bool parse(vespalib::stringref key, vespalib::stringref value, NodeData & nodeData);
+ // Preconditions: `key` and `value` MUST point into null-terminated strings.
bool parseSorD(vespalib::stringref key, vespalib::stringref value, NodeData & nodeData);
void removeExtraElements();
void printStateGroupwise(std::ostream& out, const Group&, bool verbose,
diff --git a/vdslib/src/vespa/vdslib/state/diskstate.cpp b/vdslib/src/vespa/vdslib/state/diskstate.cpp
index bcc380e0d75..c963dacff82 100644
--- a/vdslib/src/vespa/vdslib/state/diskstate.cpp
+++ b/vdslib/src/vespa/vdslib/state/diskstate.cpp
@@ -73,7 +73,7 @@ DiskState::DiskState(const vespalib::stringref & serialized)
}
LOG(debug, "Unknown key %s in diskstate. Ignoring it, assuming it's a "
"new feature from a newer version than ourself: %s",
- key.c_str(), serialized.c_str());
+ key.c_str(), vespalib::string(serialized).c_str());
}
}
diff --git a/vdslib/src/vespa/vdslib/state/nodestate.cpp b/vdslib/src/vespa/vdslib/state/nodestate.cpp
index ff6c8f31c89..d59686dcb1c 100644
--- a/vdslib/src/vespa/vdslib/state/nodestate.cpp
+++ b/vdslib/src/vespa/vdslib/state/nodestate.cpp
@@ -229,7 +229,7 @@ NodeState::NodeState(const vespalib::stringref & serialized, const NodeType* typ
}
LOG(debug, "Unknown key %s in nodestate. Ignoring it, assuming it's a "
"new feature from a newer version than ourself: %s",
- key.c_str(), serialized.c_str());
+ key.c_str(), vespalib::string(serialized).c_str());
}
diskData.addTo(_diskStates);
updateAnyDiskDownFlag();