From 4a5d82624cb0d807227aeb8860d1f106f95c272c Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Fri, 25 Jul 2008 20:54:21 +0200
Subject: [PATCH] Fixed extremely stupid bug in the i;ascii-numeric comparator.

---
 .../ext-cmp-i-ascii-numeric.c                 | 41 +++++++++++++------
 1 file changed, 29 insertions(+), 12 deletions(-)

diff --git a/src/lib-sieve/plugins/comparator-i-ascii-numeric/ext-cmp-i-ascii-numeric.c b/src/lib-sieve/plugins/comparator-i-ascii-numeric/ext-cmp-i-ascii-numeric.c
index 19802006d..cc0d2c55a 100644
--- a/src/lib-sieve/plugins/comparator-i-ascii-numeric/ext-cmp-i-ascii-numeric.c
+++ b/src/lib-sieve/plugins/comparator-i-ascii-numeric/ext-cmp-i-ascii-numeric.c
@@ -97,6 +97,7 @@ static int cmp_i_ascii_numeric_compare
 	const char *kend = key + key_size;
 	const char *vp = val;
 	const char *kp = key;
+	int digits, i;
 	
 	/* Ignore leading zeros */
 
@@ -106,25 +107,41 @@ static int cmp_i_ascii_numeric_compare
 	while ( *kp == '0' && kp < kend )  
 		kp++;
 
-	while ( vp < vend && kp < kend ) {
-		if ( !i_isdigit(*vp) || !i_isdigit(*kp) ) 
-			break;
-
-		if ( *vp != *kp ) 
-			break;
+	/* Check whether both numbers are equally long in terms of digits */
 
+	digits = 0;
+	while ( vp < vend && kp < kend && i_isdigit(*vp) && i_isdigit(*kp) ) {
 		vp++;
-		kp++;	
+		kp++;
+		digits++;	
 	}
 
 	if ( vp == vend || !i_isdigit(*vp) ) {
-		if ( kp == kend || !i_isdigit(*kp) ) 
-			return 0;
-		else	
+		if ( kp != kend && i_isdigit(*kp) ) {
+			/* Value is less */
 			return -1;
-	} else if ( kp == kend || !i_isdigit(*kp) )  
+		}
+	} else {
+		/* Value is greater */	
 		return 1;
+	}
+
+	/* Equally long: compare digits */
+
+	vp -= digits;
+	kp -= digits;
+	i = 0;
+	while ( i < digits ) {
+		if ( *vp > *kp )
+			return 1;
+		else if ( *vp < *kp )
+			return -1;
+
+		kp++;
+		vp++;
+		i++;
+	}
 		
-	return (*vp > *kp);
+	return 0;
 }
 
-- 
GitLab