Skip to content

Commit 12a5116

Browse files
committed
fix: more stricter check
1 parent bc447d2 commit 12a5116

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

tuple/include/array_of_strings_sketch_impl.hpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,23 @@ template<typename Allocator>
107107
void default_array_of_strings_serde<Allocator>::serialize(
108108
std::ostream& os, const array_of_strings* items, unsigned num
109109
) const {
110-
for (unsigned i = 0; i < num; ++i) {
111-
const uint32_t total_bytes = compute_total_bytes(items[i]);
112-
const uint8_t num_nodes = static_cast<uint8_t>(items[i].size());
113-
write(os, total_bytes);
114-
write(os, num_nodes);
115-
const std::string* data = items[i].data();
116-
for (uint8_t j = 0; j < num_nodes; ++j) {
117-
const uint32_t length = static_cast<uint32_t>(data[j].size());
118-
write(os, length);
119-
os.write(data[j].data(), length);
110+
unsigned i = 0;
111+
try {
112+
for (; i < num; ++i) {
113+
const uint32_t total_bytes = compute_total_bytes(items[i]);
114+
const uint8_t num_nodes = static_cast<uint8_t>(items[i].size());
115+
write(os, total_bytes);
116+
write(os, num_nodes);
117+
const std::string* data = items[i].data();
118+
for (uint8_t j = 0; j < num_nodes; ++j) {
119+
const uint32_t length = static_cast<uint32_t>(data[j].size());
120+
write(os, length);
121+
os.write(data[j].data(), length);
122+
}
120123
}
124+
} catch (std::runtime_error& e) {
125+
if (std::string(e.what()).find("size exceeds 127") != std::string::npos) throw;
126+
throw std::runtime_error("array_of_strings stream write failed at item " + std::to_string(i));
121127
}
122128
}
123129

@@ -149,7 +155,7 @@ void default_array_of_strings_serde<Allocator>::deserialize(
149155
summary_allocator alloc(summary_allocator_);
150156
std::allocator_traits<summary_allocator>::construct(alloc, &items[i], std::move(array));
151157
}
152-
} catch (std::istream::failure&) {
158+
} catch (...) {
153159
failure = true;
154160
}
155161
if (failure) {
@@ -191,6 +197,7 @@ size_t default_array_of_strings_serde<Allocator>::deserialize(
191197
) const {
192198
const uint8_t* ptr8 = static_cast<const uint8_t*>(ptr);
193199
size_t bytes_read = 0;
200+
194201
unsigned i = 0;
195202

196203
try {
@@ -200,28 +207,35 @@ size_t default_array_of_strings_serde<Allocator>::deserialize(
200207
uint32_t total_bytes;
201208
bytes_read += copy_from_mem(ptr8 + bytes_read, total_bytes);
202209
check_memory_size(item_start + total_bytes, capacity);
210+
211+
check_memory_size(bytes_read + sizeof(uint8_t), capacity);
203212
uint8_t num_nodes;
204213
bytes_read += copy_from_mem(ptr8 + bytes_read, num_nodes);
205214
check_num_nodes(num_nodes);
215+
206216
array_of_strings array(num_nodes, "");
207217
for (uint8_t j = 0; j < num_nodes; ++j) {
218+
check_memory_size(bytes_read + sizeof(uint32_t), capacity);
208219
uint32_t length;
209220
bytes_read += copy_from_mem(ptr8 + bytes_read, length);
221+
210222
std::string value(length, '\0');
211223
if (length != 0) {
224+
check_memory_size(bytes_read + length, capacity);
212225
bytes_read += copy_from_mem(ptr8 + bytes_read, &value[0], length);
213226
}
214227
array[j] = std::move(value);
215228
}
216229
summary_allocator alloc(summary_allocator_);
217230
std::allocator_traits<summary_allocator>::construct(alloc, &items[i], std::move(array));
218231
}
219-
} catch (...) {
232+
} catch (std::exception& e) {
220233
summary_allocator alloc(summary_allocator_);
221234
for (unsigned j = 0; j < i; ++j) {
222235
std::allocator_traits<summary_allocator>::destroy(alloc, &items[j]);
223236
}
224-
throw;
237+
if (std::string(e.what()).find("size exceeds 127") != std::string::npos) throw;
238+
throw std::runtime_error("array_of_strings bytes read failed at item " + std::to_string(i));
225239
}
226240
return bytes_read;
227241
}

0 commit comments

Comments
 (0)