Merge pull request #13034 from poettering/memdup-suffix0-multiply-fixo

memdup_suffix0_multiply fix
This commit is contained in:
Lennart Poettering 2019-07-12 14:11:31 +02:00 committed by GitHub
commit 5978345750
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 5 deletions

View file

@ -112,7 +112,9 @@ _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, si
return memdup(p, size * need);
}
_alloc_(2, 3) static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t need) {
/* Note that we can't decorate this function with _alloc_() since the returned memory area is one byte larger
* than the product of its parameters. */
static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t need) {
if (size_multiply_overflow(size, need))
return NULL;

View file

@ -56,20 +56,20 @@ static void test_GREEDY_REALLOC(void) {
}
static void test_memdup_multiply_and_greedy_realloc(void) {
int org[] = {1, 2, 3};
static const int org[] = { 1, 2, 3 };
_cleanup_free_ int *dup;
int *p;
size_t i, allocated = 3;
dup = (int*) memdup_suffix0_multiply(org, sizeof(int), 3);
dup = memdup_suffix0_multiply(org, sizeof(int), 3);
assert_se(dup);
assert_se(dup[0] == 1);
assert_se(dup[1] == 2);
assert_se(dup[2] == 3);
assert_se(*(uint8_t*) (dup + 3) == (uint8_t) 0);
assert_se(((uint8_t*) dup)[sizeof(int) * 3] == 0);
free(dup);
dup = (int*) memdup_multiply(org, sizeof(int), 3);
dup = memdup_multiply(org, sizeof(int), 3);
assert_se(dup);
assert_se(dup[0] == 1);
assert_se(dup[1] == 2);