Gentoo Archives: gentoo-commits

From: Mike Frysinger <vapier@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/sandbox:master commit in: libsandbox/
Date: Sat, 19 Dec 2015 18:10:17
Message-Id: 1450548280.f02e644a90dde960b47f9bc87125fe37dece7ee9.vapier@gentoo
1 commit: f02e644a90dde960b47f9bc87125fe37dece7ee9
2 Author: Mike Frysinger <vapier <AT> gentoo <DOT> org>
3 AuthorDate: Sat Dec 19 18:04:40 2015 +0000
4 Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org>
5 CommitDate: Sat Dec 19 18:04:40 2015 +0000
6 URL: https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=f02e644a
7
8 libsandbox: tweak edge cases of realloc a bit
9
10 We need to return NULL when passed a size of 0 as the API requires the
11 return value be usable w/free, but we just freed the pointer so the ret
12 will cause memory corruption later on.
13
14 When we go to preserve the old content, we don't need the MIN check as
15 we already verified that a few lines up. But leave it for defensive
16 purposes as gcc already optimizes it out for us. Just comment things.
17
18 Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org>
19
20 libsandbox/memory.c | 6 +++++-
21 1 file changed, 5 insertions(+), 1 deletion(-)
22
23 diff --git a/libsandbox/memory.c b/libsandbox/memory.c
24 index a2d69a2..a8f4d4b 100644
25 --- a/libsandbox/memory.c
26 +++ b/libsandbox/memory.c
27 @@ -81,7 +81,7 @@ void *realloc(void *ptr, size_t size)
28 return malloc(size);
29 if (size == 0) {
30 free(ptr);
31 - return ptr;
32 + return NULL;
33 }
34
35 old_malloc_size = SB_MALLOC_TO_SIZE(ptr);
36 @@ -91,6 +91,10 @@ void *realloc(void *ptr, size_t size)
37 ret = malloc(size);
38 if (!ret)
39 return ret;
40 + /* We already verified old_malloc_size is smaller than size above, so
41 + * we don't really need the MIN() here. We leave it to be defensive,
42 + * and because gcc optimizes away the check for us.
43 + */
44 memcpy(ret, ptr, MIN(size, old_malloc_size));
45 free(ptr);
46 return ret;