The expression "s + strlen(s) - 1" can create a pointer to one before
*s if strlen(s) is 0.
---
util.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/util.c b/util.c
index bdba718..c97f491 100644
--- a/util.c
+++ b/util.c
@@ -59,9 +59,13 @@ skip(char *s, char c) {
static void
trim(char *s) {
char *e;
-
- e = s + strlen(s) - 1;
- while(isspace(*e) && e > s)
- e--;
- *(e + 1) = '\0';
+
+ e = s + strlen(s);
+ while (e > s) {
+ if (!isspace(*--e)) {
+ e++;
+ *e = '\0';
+ break;
+ }
+ }
}
--
2.31.1