From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 11B651384B4 for ; Sat, 19 Dec 2015 18:10:17 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 1C7A6E088D; Sat, 19 Dec 2015 18:10:15 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 54321E088A for ; Sat, 19 Dec 2015 18:10:14 +0000 (UTC) Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 468B43406DD for ; Sat, 19 Dec 2015 18:10:13 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id AD360CAF for ; Sat, 19 Dec 2015 18:10:10 +0000 (UTC) From: "Mike Frysinger" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Mike Frysinger" Message-ID: <1450548280.f02e644a90dde960b47f9bc87125fe37dece7ee9.vapier@gentoo> Subject: [gentoo-commits] proj/sandbox:master commit in: libsandbox/ X-VCS-Repository: proj/sandbox X-VCS-Files: libsandbox/memory.c X-VCS-Directories: libsandbox/ X-VCS-Committer: vapier X-VCS-Committer-Name: Mike Frysinger X-VCS-Revision: f02e644a90dde960b47f9bc87125fe37dece7ee9 X-VCS-Branch: master Date: Sat, 19 Dec 2015 18:10:10 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 40767bb9-166e-4167-9e58-1e513e4e71af X-Archives-Hash: 8568613b0e8eb0c9602bd1486ac008ee commit: f02e644a90dde960b47f9bc87125fe37dece7ee9 Author: Mike Frysinger gentoo org> AuthorDate: Sat Dec 19 18:04:40 2015 +0000 Commit: Mike Frysinger gentoo org> CommitDate: Sat Dec 19 18:04:40 2015 +0000 URL: https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=f02e644a libsandbox: tweak edge cases of realloc a bit We need to return NULL when passed a size of 0 as the API requires the return value be usable w/free, but we just freed the pointer so the ret will cause memory corruption later on. When we go to preserve the old content, we don't need the MIN check as we already verified that a few lines up. But leave it for defensive purposes as gcc already optimizes it out for us. Just comment things. Signed-off-by: Mike Frysinger gentoo.org> libsandbox/memory.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libsandbox/memory.c b/libsandbox/memory.c index a2d69a2..a8f4d4b 100644 --- a/libsandbox/memory.c +++ b/libsandbox/memory.c @@ -81,7 +81,7 @@ void *realloc(void *ptr, size_t size) return malloc(size); if (size == 0) { free(ptr); - return ptr; + return NULL; } old_malloc_size = SB_MALLOC_TO_SIZE(ptr); @@ -91,6 +91,10 @@ void *realloc(void *ptr, size_t size) ret = malloc(size); if (!ret) return ret; + /* We already verified old_malloc_size is smaller than size above, so + * we don't really need the MIN() here. We leave it to be defensive, + * and because gcc optimizes away the check for us. + */ memcpy(ret, ptr, MIN(size, old_malloc_size)); free(ptr); return ret;