]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix stringcompare when strings contain null bytes.
authorGuido van Rossum <guido@python.org>
Wed, 13 Feb 1991 23:18:39 +0000 (23:18 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 13 Feb 1991 23:18:39 +0000 (23:18 +0000)
Objects/stringobject.c

index 1881bdf6f95d64eededfea2717460c1da611e272..abb3dd69953508de121bf56a457172054b9e97e7 100644 (file)
@@ -233,8 +233,12 @@ static int
 stringcompare(a, b)
        stringobject *a, *b;
 {
-       /* XXX should use memcmp on shortest size, then compare lengths */
-       return strcmp(a->ob_sval, b->ob_sval);
+       int len_a = a->ob_size, len_b = b->ob_size;
+       int min_len = (len_a < len_b) ? len_a : len_b;
+       int cmp = memcmp(a->ob_sval, b->ob_sval, min_len);
+       if (cmp != 0)
+               return cmp;
+       return (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
 }
 
 static sequence_methods string_as_sequence = {