Skip to content

Commit 93707b9

Browse files
authored
Merge pull request #68 from Enmk/fixed_string_checks
FixedString::Append checks
2 parents cb10171 + b3f2958 commit 93707b9

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

clickhouse/columns/string.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,23 @@ ColumnFixedString::ColumnFixedString(size_t n)
3131
}
3232

3333
void ColumnFixedString::Append(std::string_view str) {
34-
if (data_.capacity() < str.size())
34+
if (str.size() > string_size_) {
35+
throw std::runtime_error("Expected string of length not greater than "
36+
+ std::to_string(string_size_) + " bytes, received "
37+
+ std::to_string(str.size()) + " bytes.");
38+
}
39+
40+
if (data_.capacity() - data_.size() < str.size())
3541
{
3642
// round up to the next block size
3743
const auto new_size = (((data_.size() + string_size_) / DEFAULT_BLOCK_SIZE) + 1) * DEFAULT_BLOCK_SIZE;
3844
data_.reserve(new_size);
3945
}
4046

4147
data_.insert(data_.size(), str);
48+
// Pad up to string_size_ with zeroes.
49+
const auto padding_size = string_size_ - str.size();
50+
data_.resize(data_.size() + padding_size, char(0));
4251
}
4352

4453
void ColumnFixedString::Clear() {

clickhouse/columns/string.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ class ColumnFixedString : public Column {
1818

1919
explicit ColumnFixedString(size_t n);
2020

21+
template <typename Values>
22+
ColumnFixedString(size_t n, const Values & values)
23+
: ColumnFixedString(n)
24+
{
25+
for (const auto & v : values)
26+
Append(v);
27+
}
28+
2129
/// Appends one element to the column.
2230
void Append(std::string_view str);
2331

ut/columns_ut.cpp

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,48 @@ TEST(ColumnsCase, NumericSlice) {
144144

145145

146146
TEST(ColumnsCase, FixedStringInit) {
147-
auto col = std::make_shared<ColumnFixedString>(3);
148-
for (const auto& s : MakeFixedStrings()) {
147+
const auto column_data = MakeFixedStrings();
148+
auto col = std::make_shared<ColumnFixedString>(3, column_data);
149+
150+
ASSERT_EQ(col->Size(), column_data.size());
151+
152+
size_t i = 0;
153+
for (const auto& s : column_data) {
154+
EXPECT_EQ(s, col->At(i));
155+
++i;
156+
}
157+
}
158+
159+
TEST(ColumnsCase, FixedString_Append_SmallStrings) {
160+
// Ensure that strings smaller than FixedString's size
161+
// are padded with zeroes on insertion.
162+
163+
const size_t string_size = 7;
164+
const auto column_data = MakeFixedStrings();
165+
166+
auto col = std::make_shared<ColumnFixedString>(string_size);
167+
size_t i = 0;
168+
for (const auto& s : column_data) {
149169
col->Append(s);
170+
171+
EXPECT_EQ(string_size, col->At(i).size());
172+
173+
std::string expected = column_data[i];
174+
expected.resize(string_size, char(0));
175+
EXPECT_EQ(expected, col->At(i));
176+
177+
++i;
150178
}
151179

152-
ASSERT_EQ(col->Size(), 4u);
153-
ASSERT_EQ(col->At(1), "bbb");
154-
ASSERT_EQ(col->At(3), "ddd");
180+
ASSERT_EQ(col->Size(), i);
181+
}
182+
183+
TEST(ColumnsCase, FixedString_Append_LargeString) {
184+
// Ensure that inserting strings larger than FixedString size thorws exception.
185+
186+
const auto col = std::make_shared<ColumnFixedString>(1);
187+
EXPECT_ANY_THROW(col->Append("2c"));
188+
EXPECT_ANY_THROW(col->Append("this is a long string"));
155189
}
156190

157191
TEST(ColumnsCase, StringInit) {

0 commit comments

Comments
 (0)