summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-18 23:13:36 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-18 23:13:37 +0200
commit7298453390da42d4fcd21d2911e2d14b2187ceff (patch)
treeeec7db1b965073d1de87541e85180811d6a8cce7 /src
parentbf8990a43e8374d98d37590f2a07c09c514a7127 (diff)
toilet: fix segfault in word count
Diffstat (limited to 'src')
-rw-r--r--src/toilet.c10
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: {