From: Thibault Godouet Date: Sun, 9 Jul 2023 20:51:24 +0000 (+0100) Subject: Fixed fcrondyn printing truncated strings. X-Git-Tag: ver3_3_2~15 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8cdee4a0deae5807818892e4431b8036ce1de6cd;p=thirdparty%2Ffcron.git Fixed fcrondyn printing truncated strings. --- diff --git a/fcrondyn.c b/fcrondyn.c index 4199ac5..e44e0ff 100644 --- a/fcrondyn.c +++ b/fcrondyn.c @@ -581,12 +581,16 @@ talk_fcron(char *cmd_str, int fd) while ((read_len = recv(fd, buf, sizeof(buf) - 1, 0)) >= 0 || errno == EINTR) { + if (debug_opt) { + fprintf(stderr, "read_len=%ld\n", read_len); + } + if (errno == EINTR && debug_opt) fprintf(stderr, "got EINTR ...\n"); else if (read_len > sizeof(buf)) { /* weird ... no data yet ? */ if (debug_opt) - fprintf(stderr, "no data yet ?"); + fprintf(stderr, "no data yet ?\n"); } else if (read_len <= 0) { if (debug_opt) @@ -598,13 +602,23 @@ talk_fcron(char *cmd_str, int fd) else { /* ensure the string is terminated by a '\0' for when we'll printf() it */ buf[read_len] = '\0'; - printf("%s", buf); + + int printed_len = 0; + while (printed_len < read_len) { + int str_len = strlen(buf + printed_len); + printf("%s", buf + printed_len); + if (str_len > 0 && buf[printed_len+str_len-1] != '\n') { + printf("\n"); + } + printed_len += str_len + 1; /* +1 to account for the end-of-string '\0' marker */ + } /* check for the end of command output marker */ if (read_len >= sizeof(END_STR) && - strncmp(&buf[read_len - sizeof(END_STR)], END_STR, - sizeof(END_STR)) == 0) + memcmp(&buf[read_len - sizeof(END_STR)], END_STR, + sizeof(END_STR)) == 0) { break; + } } } if (read_len < 0) { diff --git a/fcrondyn_svr.c b/fcrondyn_svr.c index 9d26d30..b27eae3 100644 --- a/fcrondyn_svr.c +++ b/fcrondyn_svr.c @@ -362,7 +362,7 @@ auth_client_password(struct fcrondyn_cl *client) #define Test_add_field(FIELD_NB, FIELD_STR) \ if ( (bit_test(details, FIELD_NB)) ) { \ - strncat(fields, FIELD_STR, sizeof(fields)-1 - len); \ + strncat(fields, FIELD_STR, sizeof(fields) - len -1); \ len += (sizeof(FIELD_STR)-1); \ } #define Add_field(FIELD_STR) \ @@ -399,9 +399,12 @@ print_fields(int fd, unsigned char *details) Add_field(field_cmd); Add_field(field_endline); - fields[TERM_LEN - 1] = '\0'; + /* Extra safety (which should be redundant as strncat (used in Add_field + and Test_add_field) always null-terminate the string: */ + fields[sizeof(fields) - 1] = '\0'; - if (send(fd, fields, (len < sizeof(fields)) ? len : sizeof(fields), 0) < 0) + /* add +1 to include the final end-of-string "\0" */ + if (send(fd, fields, (len+1 < sizeof(fields)) ? len+1 : sizeof(fields), 0) < 0) error_e("error in send()"); } @@ -471,7 +474,12 @@ print_line(int fd, struct cl_t *line, unsigned char *details, pid_t pid, } len += snprintf(buf + len, sizeof(buf) - len, "|%s\n", line->cl_shell); - if (send(fd, buf, (len < sizeof(buf)) ? len : sizeof(buf), 0) < 0) + /* as extra safety to make sure the string is always null-terminated + (even though snprintf man page suggests it does it already) */ + buf[sizeof(buf) - 1] = '\0'; + + /* add +1 to include the final end-of-string "\0" */ + if (send(fd, buf, (len+1 < sizeof(buf)) ? len+1 : sizeof(buf), 0) < 0) error_e("error in send()"); } @@ -530,7 +538,7 @@ cmd_ls(struct fcrondyn_cl *client, long int *cmd, int fd, int is_root) getloadavg(lavg, 3); i = snprintf(lavg_str, sizeof(lavg_str), "Current load average : " "%.1f, %.1f, %.1f\n", lavg[0], lavg[1], lavg[2]); - send(fd, lavg_str, i, 0); + send(fd, lavg_str, (i+1 < sizeof(lavg_str))? i+1 : sizeof(lavg_str), 0); bit_set(fields, FIELD_LAVG); }