Skip to content

Commit 15658a7

Browse files
committed
fix multiple issues with SimpleString
1 parent 4afefac commit 15658a7

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

include/behaviortree_cpp/utils/simple_string.hpp

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ namespace SafeAny
1818
class SimpleString
1919
{
2020
public:
21+
SimpleString()
22+
{
23+
_storage.soo.capacity_left = CAPACITY;
24+
_storage.soo.data[0] = '\0';
25+
}
26+
2127
SimpleString(const std::string& str) : SimpleString(str.data(), str.size())
2228
{}
2329

@@ -29,21 +35,28 @@ class SimpleString
2935

3036
SimpleString& operator=(const SimpleString& other)
3137
{
32-
this->~SimpleString();
33-
createImpl(other.data(), other.size());
38+
if(this != &other)
39+
{
40+
this->~SimpleString();
41+
createImpl(other.data(), other.size());
42+
}
3443
return *this;
3544
}
3645

37-
SimpleString(SimpleString&& other) : SimpleString(nullptr, 0)
46+
SimpleString(SimpleString&& other) noexcept : SimpleString()
3847
{
3948
std::swap(_storage, other._storage);
4049
}
4150

42-
SimpleString& operator=(SimpleString&& other)
51+
SimpleString& operator=(SimpleString&& other) noexcept
4352
{
44-
this->~SimpleString();
45-
46-
std::swap(_storage, other._storage);
53+
if(this != &other)
54+
{
55+
this->~SimpleString();
56+
// Ensure clean state before swap
57+
_storage = {};
58+
std::swap(_storage, other._storage);
59+
}
4760
return *this;
4861
}
4962

@@ -99,46 +112,58 @@ class SimpleString
99112

100113
bool operator==(const SimpleString& other) const
101114
{
102-
size_t N = size();
115+
const size_t N = size();
103116
return other.size() == N && std::strncmp(data(), other.data(), N) == 0;
104117
}
105118

106119
bool operator!=(const SimpleString& other) const
107120
{
108-
size_t N = size();
121+
const size_t N = size();
109122
return other.size() != N || std::strncmp(data(), other.data(), N) != 0;
110123
}
111124

112125
bool operator<=(const SimpleString& other) const
113126
{
114-
return std::strcmp(data(), other.data()) <= 0;
127+
return !(*this > other);
115128
}
116129

117130
bool operator>=(const SimpleString& other) const
118131
{
119-
return std::strcmp(data(), other.data()) >= 0;
132+
return !(*this < other);
120133
}
121134

122135
bool operator<(const SimpleString& other) const
123136
{
124-
return std::strcmp(data(), other.data()) < 0;
137+
const size_t min_size = std::min(size(), other.size());
138+
int cmp = std::memcmp(data(), other.data(), min_size);
139+
if(cmp != 0)
140+
{
141+
return cmp < 0;
142+
}
143+
return size() < other.size();
125144
}
126145

127146
bool operator>(const SimpleString& other) const
128147
{
129-
return std::strcmp(data(), other.data()) > 0;
148+
const size_t min_size = std::min(size(), other.size());
149+
int cmp = std::memcmp(data(), other.data(), min_size);
150+
if(cmp != 0)
151+
{
152+
return cmp > 0;
153+
}
154+
return size() > other.size();
130155
}
131156

132157
bool isSOO() const
133158
{
134-
return !(_storage.soo.capacity_left & IS_LONG_BIT);
159+
return (_storage.soo.capacity_left & IS_LONG_BIT) == 0;
135160
}
136161

137162
private:
138163
struct String
139164
{
140-
char* data;
141-
std::size_t size;
165+
char* data = nullptr;
166+
std::size_t size = 0;
142167
};
143168

144169
constexpr static std::size_t CAPACITY = 15; // sizeof(String) - 1);
@@ -153,9 +178,9 @@ class SimpleString
153178
struct SOO
154179
{
155180
char data[CAPACITY];
156-
uint8_t capacity_left;
181+
uint8_t capacity_left = CAPACITY;
157182
} soo;
158-
} _storage;
183+
} _storage = {};
159184

160185
private:
161186
void createImpl(const char* input_data, std::size_t size)

0 commit comments

Comments
 (0)