diff options
author | Dennis Wölfing <denniswoelfing@gmx.de> | 2017-05-30 18:26:19 -0300 |
---|---|---|
committer | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2017-05-30 18:27:57 -0300 |
commit | 2e0bbbfbf95fc9e22692e93658a6fbdd2d4554da (patch) | |
tree | 662856c19816b2b441dbfda3e15ca771eb303e5f /malloc/reallocarray.c | |
parent | 4f26ef1b67287d1f2c32865f7d79c13abda81915 (diff) | |
download | glibc-2e0bbbfbf95fc9e22692e93658a6fbdd2d4554da.tar glibc-2e0bbbfbf95fc9e22692e93658a6fbdd2d4554da.tar.gz glibc-2e0bbbfbf95fc9e22692e93658a6fbdd2d4554da.tar.bz2 glibc-2e0bbbfbf95fc9e22692e93658a6fbdd2d4554da.zip |
Add reallocarray function
The reallocarray function is an extension from OpenBSD. It is an
integer-overflow-safe replacement for realloc(p, X*Y) and
malloc(X*Y) (realloc(NULL, X*Y)). It can therefore help in preventing
certain security issues in code.
This is an updated version of a patch originally submitted by Rüdiger
Sonderfeld in May 2014 [1].
Checked on i686-linux-gnu and x86_64-linux-gnu.
[1] <https://sourceware.org/ml/libc-alpha/2014-05/msg00481.html>.
2017-05-30 Dennis Wölfing <denniswoelfing@gmx.de>
Rüdiger Sonderfeld <ruediger@c-plusplus.de>
* include/stdlib.h (__libc_reallocarray): New declaration.
* malloc/Makefile (routines): Add reallocarray.
(tests): Add tst-reallocarray.c.
* malloc/Versions: Add reallocarray and __libc_reallocarray.
* malloc/malloc-internal.h (check_mul_overflow_size_t): New inline
function.
* malloc/malloc.h (reallocarray): New declaration.
* stdlib/stdlib.h (reallocarray): Likewise.
* malloc/reallocarray.c: New file.
* malloc/tst-reallocarray.c: New test file.
* manual/memory.texi: Document reallocarray.
* sysdeps/unix/sysv/linux/aarch64/libc.abilist: Add reallocarray.
* sysdeps/unix/sysv/linux/alpha/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/arm/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/hppa/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/i386/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/ia64/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/microblaze/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/nios2/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist:
Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist:
Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist: Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/sh/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/tilepro/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/x86_64/64/libc.abilist: Likewise.
* sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist: Likewise.
Diffstat (limited to 'malloc/reallocarray.c')
-rw-r--r-- | malloc/reallocarray.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/malloc/reallocarray.c b/malloc/reallocarray.c new file mode 100644 index 0000000000..07562c30c9 --- /dev/null +++ b/malloc/reallocarray.c @@ -0,0 +1,37 @@ +/* Change the size of an allocated block. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; see the file COPYING.LIB. If + not, see <http://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <malloc.h> +#include <malloc/malloc-internal.h> + +void * +__libc_reallocarray (void *optr, size_t nmemb, size_t elem_size) +{ + size_t bytes; + if (check_mul_overflow_size_t (nmemb, elem_size, &bytes)) + { + __set_errno (ENOMEM); + return 0; + } + else + return realloc (optr, bytes); +} +libc_hidden_def (__libc_reallocarray) + +weak_alias (__libc_reallocarray, reallocarray) |