Skip to content

Commit 2692a61

Browse files
committed
Cleanup: Improve get_fifo_path_to_addr
Changed size check to -2 due to 1 byte is needed at the beginning and 1 for \0 at the end
1 parent 75925a7 commit 2692a61

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/rtapi/uspace_rtapi_main.cc

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,26 +461,25 @@ static std::string get_fifo_path() {
461461
return s;
462462
}
463463

464-
static int get_fifo_path_to_addr(struct sockaddr_un *addr) {
465-
int len;
464+
static bool get_fifo_path_to_addr(struct sockaddr_un *addr) {
466465
const std::string s = get_fifo_path();
467466
if (s.empty()) {
468-
return -1;
467+
return false;
469468
}
470-
if (s.size() + 1 > sizeof(addr->sun_path)) {
469+
if (s.size() + 2 > sizeof(addr->sun_path)) {
471470
rtapi_print_msg(
472471
RTAPI_MSG_ERR,
473472
"rtapi_app: rtapi fifo path is too long (arch limit %zd): %s\n",
474473
sizeof(sockaddr_un::sun_path),
475474
s.c_str()
476475
);
477-
return -1;
476+
return false;
478477
}
479478
//See: https://www.man7.org/linux/man-pages/man7/unix.7.html abstract
480479
//sun_path[0] is a null byte ('\0')
481480
addr->sun_path[0]=0;
482-
len = snprintf(addr->sun_path + 1, sizeof(addr->sun_path) - 1, "%s", s.c_str());
483-
return len;
481+
strncpy(addr->sun_path + 1, s.c_str(), sizeof(addr->sun_path) - 2);
482+
return true;
484483
}
485484

486485
static double diff_timespec(const struct timespec *time1, const struct timespec *time0) {
@@ -525,7 +524,6 @@ int main(int argc, char **argv) {
525524
}
526525

527526
become_master:
528-
int len = 0;
529527
int fd = socket(PF_UNIX, SOCK_STREAM, 0);
530528
if (fd == -1) {
531529
perror("socket");
@@ -537,7 +535,7 @@ int main(int argc, char **argv) {
537535
struct sockaddr_un addr;
538536
memset(&addr, 0x0, sizeof(addr));
539537
addr.sun_family = AF_UNIX;
540-
if ((len = get_fifo_path_to_addr(&addr)) < 0)
538+
if (!get_fifo_path_to_addr(&addr))
541539
exit(1);
542540

543541
// plus one because we use the abstract namespace, it will show up in

0 commit comments

Comments
 (0)