]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
Tests: test_index: Make it clear that my_alloc() has no integer overflows
authorLasse Collin <lasse.collin@tukaani.org>
Sat, 27 Apr 2024 11:56:16 +0000 (14:56 +0300)
committerLasse Collin <lasse.collin@tukaani.org>
Tue, 21 May 2024 21:12:07 +0000 (00:12 +0300)
liblzma guarantees that the product of the allocation size arguments
will fit in size_t.

Putting the pre-increment in the if-statement was clearly wrong
although in practice it didn't matter here as the function is
called only a couple of times.

(cherry picked from commit 7f865577a6224fbbb5f5ca52574b62ea8ac9bf51)

tests/test_index.c

index b7e91c26c345d17d00c637d389ee317c1d931372..69f51aecfb623f5f4115eeb65ab32dfa740941f3 100644 (file)
@@ -1282,10 +1282,13 @@ my_alloc(void *opaque, size_t a, size_t b)
 {
        (void)opaque;
 
+       assert_true(SIZE_MAX / a >= b);
+
        static unsigned count = 0;
-       if (++count > 2)
+       if (count >= 2)
                return NULL;
 
+       ++count;
        return malloc(a * b);
 }