diff --git a/src/lib-sieve/util/strtrim.c b/src/lib-sieve/util/strtrim.c
index 7f03295a4b81d8c88e4a5bdf5c015c34ebb78f8c..989410d521710cd261071efe136db7f762a340bd 100644
--- a/src/lib-sieve/util/strtrim.c
+++ b/src/lib-sieve/util/strtrim.c
@@ -24,17 +24,19 @@ static void ph_str_trim_parse(const char *str,
 	if ((sides & STR_TRIM_LEFT) != 0) {
 		while (p < pend && strchr(chars, *p) != NULL)
 			p++;
+		if (p == pend)
+			return;
 	}
 	begin = p;
 
-	p = pend - 1;
+	p = pend;
 	if ((sides & STR_TRIM_RIGHT) != 0) {
-		while (p > begin && strchr(chars, *p) != NULL)
+		while (p > begin && strchr(chars, *(p-1)) != NULL)
 			p--;
+		if (p == begin)
+			return;
 	}
-	if (p <= begin)
-		return;
-	end = p + 1;
+	end = p;
 
 	*begin_r = begin;
 	*end_r = end;