From ccfbed526798512d6d6685e984cb5a63010a68a5 Mon Sep 17 00:00:00 2001 From: kib Date: Thu, 5 Apr 2012 10:38:07 +0000 Subject: [PATCH] MFC r233307: Use xmalloc() instead of malloc() in the places where malloc() calls are assumed to not fail. Make the xcalloc() calling conventions follow the calloc(3) calling conventions and replace unchecked calls to calloc() with calls to xcalloc(). Remove redundand declarations from xmalloc.c, which are already present in rtld.h. git-svn-id: svn://svn.freebsd.org/base/stable/9@233922 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- libexec/rtld-elf/ia64/reloc.c | 6 ++---- libexec/rtld-elf/powerpc64/reloc.c | 2 +- libexec/rtld-elf/rtld.c | 12 ++++++------ libexec/rtld-elf/rtld.h | 4 ++-- libexec/rtld-elf/xmalloc.c | 15 +++++++++------ 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/libexec/rtld-elf/ia64/reloc.c b/libexec/rtld-elf/ia64/reloc.c index 1afcecfa1..01e20b8f9 100644 --- a/libexec/rtld-elf/ia64/reloc.c +++ b/libexec/rtld-elf/ia64/reloc.c @@ -87,7 +87,7 @@ alloc_fptr(Elf_Addr target, Elf_Addr gp) struct fptr* fptr; if (next_fptr == last_fptr) { - current_chunk = malloc(sizeof(struct fptr_chunk)); + current_chunk = xmalloc(sizeof(struct fptr_chunk)); next_fptr = ¤t_chunk->fptrs[0]; last_fptr = ¤t_chunk->fptrs[FPTR_CHUNK_SIZE]; } @@ -116,9 +116,7 @@ alloc_fptrs(Obj_Entry *obj, bool mapped) if (fptrs == MAP_FAILED) fptrs = NULL; } else { - fptrs = malloc(fbytes); - if (fptrs != NULL) - memset(fptrs, 0, fbytes); + fptrs = xcalloc(1, fbytes); } /* diff --git a/libexec/rtld-elf/powerpc64/reloc.c b/libexec/rtld-elf/powerpc64/reloc.c index 46e2c629e..92df83d11 100644 --- a/libexec/rtld-elf/powerpc64/reloc.c +++ b/libexec/rtld-elf/powerpc64/reloc.c @@ -338,7 +338,7 @@ reloc_plt_object(Obj_Entry *obj, const Elf_Rela *rela) reloff = rela - obj->pltrela; if (obj->priv == NULL) - obj->priv = malloc(obj->pltrelasize); + obj->priv = xmalloc(obj->pltrelasize); glink = obj->priv + reloff*sizeof(Elf_Addr)*2; dbg(" reloc_plt_object: where=%p,reloff=%lx,glink=%p", (void *)where, reloff, glink); diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index fbc35be34..b51246c37 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -3746,7 +3746,7 @@ tls_get_addr_slow(Elf_Addr **dtvp, int index, size_t offset) /* Check dtv generation in case new modules have arrived */ if (dtv[0] != tls_dtv_generation) { wlock_acquire(rtld_bind_lock, &lockstate); - newdtv = calloc(1, (tls_max_index + 2) * sizeof(Elf_Addr)); + newdtv = xcalloc(tls_max_index + 2, sizeof(Elf_Addr)); to_copy = dtv[1]; if (to_copy > tls_max_index) to_copy = tls_max_index; @@ -3803,7 +3803,7 @@ allocate_tls(Obj_Entry *objs, void *oldtcb, size_t tcbsize, size_t tcbalign) return (oldtcb); assert(tcbsize >= TLS_TCB_SIZE); - tcb = calloc(1, tls_static_space - TLS_TCB_SIZE + tcbsize); + tcb = xcalloc(1, tls_static_space - TLS_TCB_SIZE + tcbsize); tls = (Elf_Addr **)(tcb + tcbsize - TLS_TCB_SIZE); if (oldtcb != NULL) { @@ -3819,7 +3819,7 @@ allocate_tls(Obj_Entry *objs, void *oldtcb, size_t tcbsize, size_t tcbalign) } } } else { - dtv = calloc(tls_max_index + 2, sizeof(Elf_Addr)); + dtv = xcalloc(tls_max_index + 2, sizeof(Elf_Addr)); tls[0] = dtv; dtv[0] = tls_dtv_generation; dtv[1] = tls_max_index; @@ -3884,8 +3884,8 @@ allocate_tls(Obj_Entry *objs, void *oldtls, size_t tcbsize, size_t tcbalign) size = round(tls_static_space, tcbalign); assert(tcbsize >= 2*sizeof(Elf_Addr)); - tls = calloc(1, size + tcbsize); - dtv = calloc(1, (tls_max_index + 2) * sizeof(Elf_Addr)); + tls = xcalloc(1, size + tcbsize); + dtv = xcalloc(tls_max_index + 2, sizeof(Elf_Addr)); segbase = (Elf_Addr)(tls + size); ((Elf_Addr*)segbase)[0] = segbase; @@ -4229,7 +4229,7 @@ rtld_verify_object_versions(Obj_Entry *obj) * way. */ obj->vernum = maxvernum + 1; - obj->vertab = calloc(obj->vernum, sizeof(Ver_Entry)); + obj->vertab = xcalloc(obj->vernum, sizeof(Ver_Entry)); vd = obj->verdef; while (vd != NULL) { diff --git a/libexec/rtld-elf/rtld.h b/libexec/rtld-elf/rtld.h index cacea8ca5..8089012b6 100644 --- a/libexec/rtld-elf/rtld.h +++ b/libexec/rtld-elf/rtld.h @@ -58,7 +58,7 @@ #endif #define NEW(type) ((type *) xmalloc(sizeof(type))) -#define CNEW(type) ((type *) xcalloc(sizeof(type))) +#define CNEW(type) ((type *) xcalloc(1, sizeof(type))) /* We might as well do booleans like C++. */ typedef unsigned char bool; @@ -320,7 +320,7 @@ typedef struct Struct_SymLook { extern void _rtld_error(const char *, ...) __printflike(1, 2); extern const char *rtld_strerror(int); extern Obj_Entry *map_object(int, const char *, const struct stat *); -extern void *xcalloc(size_t); +extern void *xcalloc(size_t, size_t); extern void *xmalloc(size_t); extern char *xstrdup(const char *); extern Elf_Addr _GLOBAL_OFFSET_TABLE_[]; diff --git a/libexec/rtld-elf/xmalloc.c b/libexec/rtld-elf/xmalloc.c index 0d992257b..3783685dc 100644 --- a/libexec/rtld-elf/xmalloc.c +++ b/libexec/rtld-elf/xmalloc.c @@ -32,14 +32,17 @@ #include "rtld.h" #include "rtld_printf.h" -void *xcalloc(size_t); -void *xmalloc(size_t); -char *xstrdup(const char *); - void * -xcalloc(size_t size) +xcalloc(size_t number, size_t size) { - return memset(xmalloc(size), 0, size); + void *p; + + p = calloc(number, size); + if (p == NULL) { + rtld_fdputstr(STDERR_FILENO, "Out of memory\n"); + _exit(1); + } + return (p); } void * -- 2.45.0