From 7298453390da42d4fcd21d2911e2d14b2187ceff Mon Sep 17 00:00:00 2001 From: Lieuwe Rooijakkers Date: Sun, 18 Aug 2024 23:13:36 +0200 Subject: toilet: fix segfault in word count --- src/toilet.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/toilet.c b/src/toilet.c index 6339599..de32003 100644 --- a/src/toilet.c +++ b/src/toilet.c @@ -79,17 +79,21 @@ static size_t get_count(enum MODE mode, struct filebuf *fb) { return fb->sz; case M_WORDS: { +#define IN(i) (i < fb->sz) + size_t words = 0; // (c) Tom Forging - for (size_t i = 0; i < (size_t)fb->sz;) { + for (size_t i = 0; IN(i);) { size_t previ = i; - while (!isspace(fb->buf[i])) i++; + while (IN(i) && !isspace(fb->buf[i])) i++; words += i != previ; - while (isspace(fb->buf[i])) i++; + while (IN(i) && isspace(fb->buf[i])) i++; } return words; + +#undef IN } case M_LINES: { -- cgit v1.2.3-70-g09d2