Skip to content

Commit

Permalink
io_log: Add assertion, remove magic numbers.
Browse files Browse the repository at this point in the history
* io_log: Remove magic numbers and add assertion for
  condition that was observed not holding in one instance,
  leading to a crash.
* socket.c: Don't emit spurious warning when errno is 0.
  • Loading branch information
InterLinked1 committed Feb 14, 2025
1 parent a7800f1 commit eed0f9f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion bbs/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,9 @@ static ssize_t full_write(struct pollfd *pfd, int fd, const char *restrict buf,
* to terminate node execution if needed.
* In this particular case, a return value of 0 is logically
* interpreted as a total failure and should result in disconnect. */
bbs_error("Failed to fully write %lu bytes to fd %d: %s\n", len, fd, strerror(errno));
if (errno) { /* Not all nonpos returns are errors. Don't log spurious error for non-error branches (e.g. Exceptional activity, not writable) */
bbs_error("Failed to fully write %lu bytes to fd %d: %s\n", len, fd, strerror(errno));
}
return -1;
}
return bytes;
Expand Down
18 changes: 13 additions & 5 deletions io/io_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#include "include/alertpipe.h"
#include "include/utils.h"

/* Extra correctness checks */
#define VERIFY_INTEGRITY

struct log_data {
int rpfd[2];
int wpfd[2];
Expand Down Expand Up @@ -73,12 +76,17 @@ static void flowlog(FILE *fp, char *restrict buf, size_t len, char dirchr)

fprintf(fp, "%c %s.%06d\n", dirchr, datestr, (int) now.tv_usec);

#define LINE_LENGTH 32

/* The output here is designed to be formatted similar to tcpflow's output with the -C and -D options. */
for (i = 0; len > 0; i += 32) {
for (i = 0; len > 0; i += LINE_LENGTH) {
char hexbuf[81]; /* 80 cols, so 81 with NUL */
char asciibuf[33]; /* 32 cols, so 33 with NUL */
char asciibuf[LINE_LENGTH + 1]; /* 32 cols, so 33 with NUL */
char *hexpos = hexbuf, *asciipos = asciibuf;
size_t bytes_this_line = MIN((size_t) 32, len);
size_t bytes_this_line = MIN((size_t) LINE_LENGTH, len);
#ifdef VERIFY_INTEGRITY
bbs_assert(bytes_this_line <= LINE_LENGTH); /* If this fails, some hanky panky has gone on with the types */
#endif
/* For all characters available for this line: */
for (j = 0; j < bytes_this_line; j++, s++) {
#undef sprintf
Expand All @@ -89,8 +97,8 @@ static void flowlog(FILE *fp, char *restrict buf, size_t len, char dirchr)
*asciipos = '\0';
/* Line of output in log file */
fprintf(fp, "%c %04x: %-80s %-32s\n", dirchr, (int) i, hexbuf, asciibuf);
if (len >= 32) {
len -= 32;
if (len >= LINE_LENGTH) {
len -= LINE_LENGTH;
} else {
len = 0;
}
Expand Down

0 comments on commit eed0f9f

Please sign in to comment.