Skip to content

Commit 777c8d9

Browse files
committed
Cleanup: Fix size_t / a few other conversion issues
Where possible with certainty
1 parent 716774f commit 777c8d9

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

src/rtapi/uspace_rtapi_main.cc

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static std::string remove_quotes(const std::string &s) {
168168
}
169169

170170
static int do_comp_args(void *module, const std::vector<std::string> &args) {
171-
for (unsigned i = 0; i < args.size(); i++) {
171+
for (size_t i = 0; i < args.size(); i++) {
172172
std::string s = remove_quotes(args[i]);
173173
size_t idx = s.find('=');
174174
if (idx == std::string::npos) {
@@ -312,7 +312,11 @@ static int do_debug_cmd(const std::string &value) {
312312
* signal, change to something like while(remaining > 0 && !exit_flag)
313313
* and set exit_flag in signal handler
314314
*/
315-
static int send_data(int fd, const void *buf, size_t n, int flags) {
315+
static ssize_t send_data(int fd, const void *buf, size_t n, int flags) {
316+
if(n > SSIZE_MAX){
317+
errno=EFBIG;
318+
return -1;
319+
}
316320
const uint8_t *ptr = (const uint8_t *)buf;
317321
size_t n_rem = n;
318322
while (n_rem > 0) {
@@ -333,7 +337,11 @@ static int send_data(int fd, const void *buf, size_t n, int flags) {
333337
return n; // All sent
334338
}
335339

336-
static int recv_data(int fd, void *buf, size_t n, int flags) {
340+
static ssize_t recv_data(int fd, void *buf, size_t n, int flags) {
341+
if(n > SSIZE_MAX){
342+
errno=EFBIG;
343+
return -1;
344+
}
337345
uint8_t *ptr = (uint8_t *)buf;
338346
size_t n_rem = n;
339347
while (n_rem > 0) {
@@ -408,8 +416,8 @@ static bool recv_result(int fd, int *result) {
408416
}
409417

410418
static void push_uint16(std::vector<char> &buf, uint16_t value) {
411-
buf.push_back(0xff & (value >> 0));
412-
buf.push_back(0xff & (value >> 8));
419+
buf.push_back((char)(0xff & (value >> 0)));
420+
buf.push_back((char)(0xff & (value >> 8)));
413421
}
414422

415423
static uint16_t get_uint16(const std::vector<char> &buf, size_t idx) {
@@ -486,19 +494,25 @@ static bool send_args(int fd, const std::vector<std::string> &args) {
486494
buff_size += args[i].size();
487495
}
488496

489-
//This is the largest value set by set_int16()
497+
//Check uint16_t conversions
498+
//Buffer size is > sum(args[i].size()) so they don't need a separate check
490499
if (buff_size > std::numeric_limits<uint16_t>::max()) {
491500
rtapi_print_msg(RTAPI_MSG_ERR, "rtapi_app: send_args: args to big, size = %li!\n", buff_size);
492501
return false;
493502
}
503+
//Edge case: One could in theory send many size zero args
504+
if (args.size() > std::numeric_limits<uint16_t>::max()) {
505+
rtapi_print_msg(RTAPI_MSG_ERR, "rtapi_app: send_args: arg count to big, size = %li!\n", args.size());
506+
return false;
507+
}
494508

495509
//Serialize
496510
std::vector<char> buf;
497511
buf.reserve(buff_size);
498-
push_uint16(buf, buff_size);
499-
push_uint16(buf, args.size());
512+
push_uint16(buf, (uint16_t)buff_size);
513+
push_uint16(buf, (uint16_t)args.size());
500514
for (size_t i = 0; i < args.size(); i++) {
501-
push_uint16(buf, args[i].size());
515+
push_uint16(buf, (uint16_t)args[i].size());
502516
buf.insert(buf.end(), args[i].begin(), args[i].end());
503517
}
504518
if (buf.size() != buff_size) {
@@ -672,7 +686,7 @@ static bool get_fifo_path_to_addr(struct sockaddr_un *addr) {
672686
}
673687

674688
static double diff_timespec(const struct timespec *time1, const struct timespec *time0) {
675-
return (time1->tv_sec - time0->tv_sec) + (time1->tv_nsec - time0->tv_nsec) / 1000000000.0;
689+
return (double)(time1->tv_sec - time0->tv_sec) + (double)(time1->tv_nsec - time0->tv_nsec) / 1000000000.0;
676690
}
677691

678692
int main(int argc, char **argv) {
@@ -755,7 +769,7 @@ int main(int argc, char **argv) {
755769
if (result == 0)
756770
break;
757771

758-
usleep(lrand48() % 100000 + 100); //Random sleep min 100us max 100100us
772+
usleep((useconds_t)(lrand48() % 100000) + 100); //Random sleep min 100us max 100100us
759773
clock_gettime(CLOCK_MONOTONIC, &now);
760774
}
761775
if (result < 0 && errno == ECONNREFUSED) {
@@ -941,8 +955,7 @@ static int harden_rt() {
941955
if (fd < 0) {
942956
rtapi_print_msg(RTAPI_MSG_WARN, "failed to open /dev/cpu_dma_latency: %s\n", strerror(errno));
943957
} else {
944-
int r;
945-
r = write(fd, "\0\0\0\0", 4);
958+
ssize_t r = write(fd, "\0\0\0\0", 4);
946959
if (r != 4) {
947960
rtapi_print_msg(RTAPI_MSG_WARN, "failed to write to /dev/cpu_dma_latency: %s\n", strerror(errno));
948961
}

0 commit comments

Comments
 (0)