Skip to content

Commit

Permalink
pty.c: Don't attempt to write 0 bytes to node.
Browse files Browse the repository at this point in the history
For non-ANSI nodes, we call bbs_ansi_strip to
remove all the escape sequences from the buffer
before we write it to the node. However, in the
case that this actually results in an empty
buffer, don't attempt to write it. No nasty
side effects will happen, but a warning will be
thrown otherwise since we shouldn't be attempting
to write 0 bytes.
  • Loading branch information
InterLinked1 committed Nov 25, 2023
1 parent e9aab62 commit c44d082
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions bbs/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ static int cli_nodes(struct bbs_cli_args *a)
int c = 0;
time_t now = time(NULL);

bbs_dprintf(a->fdout, "%3s %8s %9s %7s %-15s %-25s %15s %5s %1s %1s %7s %3s %3s %3s %3s %3s %3s %s\n", "#", "PROTOCOL", "ELAPSED", "TRM SZE", "USER", "MENU/PAGE", "IP ADDRESS", "RPORT", "E", "B", "TID", "FD", "RFD", "WFD", "MST", "SLV", "SPY", "SLV NAME");
bbs_dprintf(a->fdout, "%3s %9s %9s %7s %-15s %-25s %15s %5s %1s %1s %7s %3s %3s %3s %3s %3s %3s %s\n", "#", "PROTOCOL", "ELAPSED", "TRM SZE", "USER", "MENU/PAGE", "IP ADDRESS", "RPORT", "E", "B", "TID", "FD", "RFD", "WFD", "MST", "SLV", "SPY", "SLV NAME");

RWLIST_RDLOCK(&nodes);
RWLIST_TRAVERSE(&nodes, n, entry) {
Expand All @@ -678,7 +678,7 @@ static int cli_nodes(struct bbs_cli_args *a)
print_time_elapsed(n->created, now, elapsed, sizeof(elapsed));
snprintf(menufull, sizeof(menufull), "%s%s%s%s", S_IF(n->menu), n->menuitem ? " (" : "", S_IF(n->menuitem), n->menuitem ? ")" : "");
lwp = bbs_pthread_tid(n->thread);
bbs_dprintf(a->fdout, "%3d %8s %9s %3dx%3d %-15s %-25s %15s %5u %1s %1s %7d %3d %3d %3d %3d %3d %3d %s\n",
bbs_dprintf(a->fdout, "%3d %9s %9s %3dx%3d %-15s %-25s %15s %5u %1s %1s %7d %3d %3d %3d %3d %3d %3d %s\n",
n->id, n->protname, elapsed, n->cols, n->rows, bbs_username(n->user), menufull, n->ip, n->rport, BBS_YN(n->echo), BBS_YN(n->buffered),
lwp, n->fd, n->rfd, n->wfd, n->amaster, n->slavefd, n->spyfd, n->slavename);
c++;
Expand Down
6 changes: 6 additions & 0 deletions bbs/pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,12 @@ void *pty_master(void *varg)
writebuf[bytes_read] = '\0'; /* NUL terminate for bbs_ansi_strip */
/*! \todo XXX This should always get smaller... so couldn't this be done in place? */
if (!bbs_ansi_strip(writebuf, (size_t) bytes_read, strippedbuf, sizeof(strippedbuf), &strippedlen)) {
if (strippedlen == 0) {
bbs_debug(9, "Reduced all %lu bytes to nothing after stripping escape sequences\n", bytes_read);
/* There is nothing to write, don't even bother calling bbs_write,
* since we shouldn't call write with 0 bytes. */
continue;
}
bytes_read = strippedlen;
relaybuf = strippedbuf;
} /* else, failed to strip, just write the original data (possibly containing ANSI escape sequences) */
Expand Down
10 changes: 4 additions & 6 deletions bbs/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -2124,7 +2124,6 @@ static int bbs_node_ansi_write(struct bbs_node *node, const char *restrict buf,
ssize_t bbs_node_write(struct bbs_node *node, const char *buf, size_t len)
{
struct pollfd pfd;
size_t left = len;
ssize_t res;

#ifdef DETECT_IMPROPER_NODE_WRITES
Expand All @@ -2147,28 +2146,27 @@ ssize_t bbs_node_write(struct bbs_node *node, const char *buf, size_t len)
/* So helgrind doesn't complain about data race if node is shut down
* and slavefd closed during write */
bbs_node_lock(node);
res = full_write(&pfd, node->slavefd, buf, left);
res = full_write(&pfd, node->slavefd, buf, len);
if (res <= 0 || res != (ssize_t) len) {
bbs_debug(5, "Node %d: write returned %lu/%lu\n", node->id, res, len);
}
bbs_node_unlock(node);
return (int) res;
return res;
}

ssize_t bbs_write(int fd, const char *buf, size_t len)
{
struct pollfd pfd;
size_t left = len;
ssize_t res;

pfd.fd = fd;
pfd.events = POLLOUT;

res = full_write(&pfd, fd, buf, left);
res = full_write(&pfd, fd, buf, len);
if (res <= 0) {
bbs_debug(5, "fd %d: write returned %ld: %s\n", fd, res, res ? strerror(errno) : "");
}
return (int) res;
return res;
}

ssize_t bbs_timed_write(int fd, const char *buf, size_t len, int ms)
Expand Down
1 change: 1 addition & 0 deletions include/ansi.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
* \param out Buffer in which the stripped string will be placed
* \param outlen Size of out. Should be at least inlen, but no more than it.
* \param strippedlen Will be set to the actual length of the stripped output, not including null terminator.
* \retval 0 on success, -1 on failure
*/
int bbs_ansi_strip(const char *restrict in, size_t inlen, char *restrict out, size_t outlen, int *restrict strippedlen);

0 comments on commit c44d082

Please sign in to comment.