Skip to content

Commit eed0f9f

Browse files
committed
io_log: Add assertion, remove magic numbers.
* 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.
1 parent a7800f1 commit eed0f9f

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

bbs/socket.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2355,7 +2355,9 @@ static ssize_t full_write(struct pollfd *pfd, int fd, const char *restrict buf,
23552355
* to terminate node execution if needed.
23562356
* In this particular case, a return value of 0 is logically
23572357
* interpreted as a total failure and should result in disconnect. */
2358-
bbs_error("Failed to fully write %lu bytes to fd %d: %s\n", len, fd, strerror(errno));
2358+
if (errno) { /* Not all nonpos returns are errors. Don't log spurious error for non-error branches (e.g. Exceptional activity, not writable) */
2359+
bbs_error("Failed to fully write %lu bytes to fd %d: %s\n", len, fd, strerror(errno));
2360+
}
23592361
return -1;
23602362
}
23612363
return bytes;

io/io_log.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#include "include/alertpipe.h"
3838
#include "include/utils.h"
3939

40+
/* Extra correctness checks */
41+
#define VERIFY_INTEGRITY
42+
4043
struct log_data {
4144
int rpfd[2];
4245
int wpfd[2];
@@ -73,12 +76,17 @@ static void flowlog(FILE *fp, char *restrict buf, size_t len, char dirchr)
7376

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

79+
#define LINE_LENGTH 32
80+
7681
/* The output here is designed to be formatted similar to tcpflow's output with the -C and -D options. */
77-
for (i = 0; len > 0; i += 32) {
82+
for (i = 0; len > 0; i += LINE_LENGTH) {
7883
char hexbuf[81]; /* 80 cols, so 81 with NUL */
79-
char asciibuf[33]; /* 32 cols, so 33 with NUL */
84+
char asciibuf[LINE_LENGTH + 1]; /* 32 cols, so 33 with NUL */
8085
char *hexpos = hexbuf, *asciipos = asciibuf;
81-
size_t bytes_this_line = MIN((size_t) 32, len);
86+
size_t bytes_this_line = MIN((size_t) LINE_LENGTH, len);
87+
#ifdef VERIFY_INTEGRITY
88+
bbs_assert(bytes_this_line <= LINE_LENGTH); /* If this fails, some hanky panky has gone on with the types */
89+
#endif
8290
/* For all characters available for this line: */
8391
for (j = 0; j < bytes_this_line; j++, s++) {
8492
#undef sprintf
@@ -89,8 +97,8 @@ static void flowlog(FILE *fp, char *restrict buf, size_t len, char dirchr)
8997
*asciipos = '\0';
9098
/* Line of output in log file */
9199
fprintf(fp, "%c %04x: %-80s %-32s\n", dirchr, (int) i, hexbuf, asciibuf);
92-
if (len >= 32) {
93-
len -= 32;
100+
if (len >= LINE_LENGTH) {
101+
len -= LINE_LENGTH;
94102
} else {
95103
len = 0;
96104
}

0 commit comments

Comments
 (0)