From 72731832cfedb1ab575965e8d9b1d635ec493752 Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Fri, 16 Oct 2026 06:17:03 -1000 Subject: Read and write slot keys and pages in full xread and xwrite may move fewer bytes than asked. A short write of the key left a slot that could never match, a short read of it reported a collision, and a page write that moved nothing looped for ever. --- source/cache.c | 4 ++-- source/html.c | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'source') diff --git a/source/cache.c b/source/cache.c index 60e071b..af391db 100644 --- a/source/cache.c +++ b/source/cache.c @@ -85,7 +85,7 @@ static int open_slot(struct cache_slot *slot) if (fstat(slot->cache_fd, &slot->st)) return errno; - slot->buflen = xread(slot->cache_fd, slot->buf, sizeof(slot->buf)); + slot->buflen = read_in_full(slot->cache_fd, slot->buf, sizeof(slot->buf)); if (slot->buflen < 0) return errno; @@ -265,7 +265,7 @@ static int lock_slot(struct cache_slot *slot) // start from empty now that nobody else can be writing it. if (ftruncate(slot->lock_fd, 0) < 0) return errno; - if (xwrite(slot->lock_fd, slot->key, slot->keylen + 1) < 0) + if (write_in_full(slot->lock_fd, slot->key, slot->keylen + 1) < 0) return errno; return 0; } diff --git a/source/html.c b/source/html.c index ef74bb3..367cea7 100644 --- a/source/html.c +++ b/source/html.c @@ -65,7 +65,11 @@ static void write_out(const char *data, size_t size) while (size) { ssize_t written = xwrite(STDOUT_FILENO, data, size); - if (written < 0) + // A write that moves nothing would repeat for ever, so it + // counts as a full device, the way write_in_full treats it. + if (!written) + errno = ENOSPC; + if (written <= 0) break; data += written; size -= written; -- cgit