Skip to content

Commit

Permalink
Avoid passing null pointers to memcpy
Browse files Browse the repository at this point in the history
  • Loading branch information
rocallahan committed Mar 7, 2024
1 parent fe0e8be commit 1fe5951
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/GdbServerConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ void GdbServerConnection::write_packet(const char* data) {
}

void GdbServerConnection::write_binary_packet(const char* pfx, const uint8_t* data,
ssize_t num_bytes) {
ssize_t num_bytes) {
ssize_t pfx_num_chars = strlen(pfx);
vector<uint8_t> buf;
buf.resize(2 * num_bytes + pfx_num_chars);
buf.resize(2 * num_bytes + pfx_num_chars + 1);
ssize_t buf_num_bytes = 0;
int i;

Expand Down
10 changes: 6 additions & 4 deletions src/processor_trace_check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ static vector<uint8_t> flatten_vector(const vector<vector<uint8_t>>& data) {
total_size += d.size();
}
ret.resize(total_size);
size_t offset = 0;
for (const auto& d : data) {
memcpy(ret.data() + offset, d.data(), d.size());
offset += d.size();
if (total_size) {
size_t offset = 0;
for (const auto& d : data) {
memcpy(ret.data() + offset, d.data(), d.size());
offset += d.size();
}
}
return ret;
}
Expand Down
8 changes: 5 additions & 3 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,11 @@ vector<uint8_t> read_pt_data(Task* t, FrameTime global_time) {
while (true) {
char buf[1024*1024];
size_t bytes_read = fread(buf, 1, sizeof(buf), dump_file);
size_t current_size = result.size();
result.resize(current_size + bytes_read);
memcpy(result.data() + current_size, buf, bytes_read);
if (bytes_read) {
size_t current_size = result.size();
result.resize(current_size + bytes_read);
memcpy(result.data() + current_size, buf, bytes_read);
}
if (bytes_read < sizeof(buf)) {
break;
}
Expand Down

0 comments on commit 1fe5951

Please sign in to comment.