Skip to content

Commit

Permalink
APPEND: avoid overrunning the buffer (#161)
Browse files Browse the repository at this point in the history
if two APPENDs are called one after another, and first of them
cannot fit the message into the buffer, the second will end up
writing its message pass the end of the buffer (due to size
parameter to snprintf being unsigned).
  • Loading branch information
dottedmag authored Dec 11, 2023
1 parent 9983708 commit 46d82a0
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion blink/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

#define MAX_BACKTRACE_LINES 64

#define APPEND(...) o += snprintf(b + o, n - o, __VA_ARGS__)
#define APPEND(...) o += snprintf(b + o, o > n ? 0 : n - o, __VA_ARGS__)

_Thread_local static jmp_buf g_busted;

Expand Down
2 changes: 1 addition & 1 deletion blink/hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "blink/log.h"
#include "blink/tsan.h"

#define APPEND(...) oi += snprintf(ob + oi, on - oi, __VA_ARGS__)
#define APPEND(...) oi += snprintf(ob + oi, oi > on ? 0 : on - oi, __VA_ARGS__)

void DumpHex(u8 *p, size_t n) {
int oi = 0;
Expand Down
2 changes: 1 addition & 1 deletion blink/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

#define DEFAULT_LOG_PATH "blink.log"

#define APPEND(F, ...) n += F(b + n, PIPE_BUF - n, __VA_ARGS__)
#define APPEND(F, ...) n += F(b + n, n > PIPE_BUF ? 0 : PIPE_BUF - n, __VA_ARGS__)

static struct Log {
pthread_once_t_ once;
Expand Down
2 changes: 1 addition & 1 deletion blink/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "blink/stats.h"
#include "blink/vfs.h"

#define APPEND(...) o += snprintf(b + o, n - o, __VA_ARGS__)
#define APPEND(...) o += snprintf(b + o, o > n ? 0 : n - o, __VA_ARGS__)

#ifdef HAVE_JIT

Expand Down
2 changes: 1 addition & 1 deletion blink/pml4tfmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#define INTERESTING_FLAGS (PAGE_U | PAGE_RW | PAGE_XD | PAGE_FILE)

#define BYTES 16384
#define APPEND(...) u->o += snprintf(u->b + u->o, BYTES - u->o, __VA_ARGS__)
#define APPEND(...) u->o += snprintf(u->b + u->o, u->o > BYTES ? 0 : BYTES - u->o, __VA_ARGS__)

struct MapMaker {
bool t;
Expand Down
2 changes: 1 addition & 1 deletion blink/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#undef DEFINE_AVERAGE
#undef DEFINE_COUNTER

#define APPEND(...) o += snprintf(b + o, n - o, __VA_ARGS__)
#define APPEND(...) o += snprintf(b + o, o > n ? 0 : n - o, __VA_ARGS__)

void PrintStats(void) {
#ifndef NDEBUG
Expand Down
2 changes: 1 addition & 1 deletion blink/strace.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "blink/thread.h"
#include "blink/util.h"

#define APPEND(...) bi += snprintf(bp + bi, bn - bi, __VA_ARGS__)
#define APPEND(...) bi += snprintf(bp + bi, bi > bn ? 0 : bn - bi, __VA_ARGS__)

struct thatispacked MagicNumber {
int x;
Expand Down

0 comments on commit 46d82a0

Please sign in to comment.