Skip to content

Commit aa574fb

Browse files
committed
Zend/zend_operators: fix casting underflowed unsigend to signed
Casting a huge unsigned value to signed is undefined behavior in C. We need to cast to signed before subtracting. In case anybody worries about truncating a 64 bit size_t to 32 bit int too early: this problem was there before already, this commit just makes one corner case more likely.
1 parent d6e4fbc commit aa574fb

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Zend/zend_operators.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2956,7 +2956,7 @@ ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const
29562956
}
29572957
retval = memcmp(s1, s2, MIN(len1, len2));
29582958
if (!retval) {
2959-
return (int)(len1 - len2);
2959+
return (int)len1 - (int)len2;
29602960
} else {
29612961
return retval;
29622962
}
@@ -2972,7 +2972,7 @@ ZEND_API int ZEND_FASTCALL zend_binary_strncmp(const char *s1, size_t len1, cons
29722972
}
29732973
retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
29742974
if (!retval) {
2975-
return (int)(MIN(length, len1) - MIN(length, len2));
2975+
return (int)MIN(length, len1) - (int)MIN(length, len2);
29762976
} else {
29772977
return retval;
29782978
}
@@ -2997,7 +2997,7 @@ ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, c
29972997
}
29982998
}
29992999

3000-
return (int)(len1 - len2);
3000+
return (int)len1 - (int)len2;
30013001
}
30023002
/* }}} */
30033003

@@ -3018,7 +3018,7 @@ ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp(const char *s1, size_t len1,
30183018
}
30193019
}
30203020

3021-
return (int)(MIN(length, len1) - MIN(length, len2));
3021+
return (int)MIN(length, len1) - (int)MIN(length, len2);
30223022
}
30233023
/* }}} */
30243024

@@ -3040,7 +3040,7 @@ ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp_l(const char *s1, size_t len1,
30403040
}
30413041
}
30423042

3043-
return (int)(len1 - len2);
3043+
return (int)len1 - (int)len2;
30443044
}
30453045
/* }}} */
30463046

@@ -3061,7 +3061,7 @@ ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp_l(const char *s1, size_t len1
30613061
}
30623062
}
30633063

3064-
return (int)(MIN(length, len1) - MIN(length, len2));
3064+
return (int)MIN(length, len1) - (int)MIN(length, len2);
30653065
}
30663066
/* }}} */
30673067

0 commit comments

Comments
 (0)