Skip to content

Commit a5f8dd3

Browse files
committed
Cleanup: socket protocol: fix send_args
vector.insert increases size, so to much data was sent. Change to reserve() and push_back() to avoid allocation.
1 parent 1d30868 commit a5f8dd3

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

src/rtapi/uspace_rtapi_main.cc

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ static bool recv_result(int fd, int *result) {
397397
}
398398
}
399399

400-
static void set_uint16(std::vector<char> &buf, uint16_t value, size_t idx) {
401-
buf[idx] = 0xff & (value >> 0);
402-
buf[idx + 1] = 0xff & (value >> 8);
400+
static void push_uint16(std::vector<char> &buf, uint16_t value) {
401+
buf.push_back(0xff & (value >> 0));
402+
buf.push_back(0xff & (value >> 8));
403403
}
404404

405405
static uint16_t get_uint16(const std::vector<char> &buf, size_t idx) {
@@ -420,7 +420,7 @@ static bool recv_args(int fd, std::vector<std::string> &args) {
420420
}
421421
return false;
422422
}
423-
size_t buff_size = tmp;
423+
size_t buff_size = tmp - sizeof(uint16_t); //Size already consumed
424424

425425
//Get data
426426
std::vector<char> buf(buff_size);
@@ -447,7 +447,7 @@ static bool recv_args(int fd, std::vector<std::string> &args) {
447447
args[i] = std::string(buf.begin() + idx, buf.begin() + idx + arg_size);
448448
idx += arg_size;
449449
}
450-
if (idx + sizeof(uint16_t) != buff_size) {
450+
if (idx != buff_size) {
451451
rtapi_print_msg(RTAPI_MSG_ERR, "rtapi_app: Bug recv_args: idx %li != buff_size %li\n", idx, buff_size);
452452
return false;
453453
}
@@ -471,20 +471,18 @@ static bool send_args(int fd, const std::vector<std::string> &args) {
471471
}
472472

473473
//Serialize
474-
std::vector<char> buf(buff_size);
475-
size_t idx = 0;
476-
set_uint16(buf, buff_size, idx);
477-
idx += sizeof(uint16_t);
478-
set_uint16(buf, args.size(), idx);
479-
idx += sizeof(uint16_t);
474+
std::vector<char> buf;
475+
buf.reserve(buff_size);
476+
push_uint16(buf, buff_size);
477+
push_uint16(buf, args.size());
480478
for (size_t i = 0; i < args.size(); i++) {
481-
set_uint16(buf, args[i].size(), idx);
482-
idx += sizeof(uint16_t);
483-
buf.insert(buf.begin() + idx, args[i].begin(), args[i].end());
484-
idx += args[i].size();
479+
push_uint16(buf, args[i].size());
480+
buf.insert(buf.end(), args[i].begin(), args[i].end());
485481
}
486-
if (idx != buff_size) {
487-
rtapi_print_msg(RTAPI_MSG_ERR, "rtapi_app: Bug send_args: idx %li != buff_size %li\n", idx, buff_size);
482+
if (buf.size() != buff_size) {
483+
rtapi_print_msg(
484+
RTAPI_MSG_ERR, "rtapi_app: Bug send_args: buf.size() %li != buff_size %li\n", buf.size(), buff_size
485+
);
488486
return false;
489487
}
490488

0 commit comments

Comments
 (0)