diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-18 23:13:36 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-18 23:13:37 +0200 |
commit | 7298453390da42d4fcd21d2911e2d14b2187ceff (patch) | |
tree | eec7db1b965073d1de87541e85180811d6a8cce7 /src | |
parent | bf8990a43e8374d98d37590f2a07c09c514a7127 (diff) |
toilet: fix segfault in word count
Diffstat (limited to 'src')
-rw-r--r-- | src/toilet.c | 10 |
1 files 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: { |