Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 22 Mar 2012 14:20:51 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r233307 - in head/libexec/rtld-elf: . ia64 powerpc64
Message-ID:  <201203221420.q2MEKptP027771@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Thu Mar 22 14:20:51 2012
New Revision: 233307
URL: http://svn.freebsd.org/changeset/base/233307

Log:
  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.
  
  Reviewed by:	kan
  Discussed with:	bde
  MFC after:	2 weeks

Modified:
  head/libexec/rtld-elf/ia64/reloc.c
  head/libexec/rtld-elf/powerpc64/reloc.c
  head/libexec/rtld-elf/rtld.c
  head/libexec/rtld-elf/rtld.h
  head/libexec/rtld-elf/xmalloc.c

Modified: head/libexec/rtld-elf/ia64/reloc.c
==============================================================================
--- head/libexec/rtld-elf/ia64/reloc.c	Thu Mar 22 14:11:10 2012	(r233306)
+++ head/libexec/rtld-elf/ia64/reloc.c	Thu Mar 22 14:20:51 2012	(r233307)
@@ -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 = &current_chunk->fptrs[0];
 		last_fptr = &current_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);
 	}
 
 	/*

Modified: head/libexec/rtld-elf/powerpc64/reloc.c
==============================================================================
--- head/libexec/rtld-elf/powerpc64/reloc.c	Thu Mar 22 14:11:10 2012	(r233306)
+++ head/libexec/rtld-elf/powerpc64/reloc.c	Thu Mar 22 14:20:51 2012	(r233307)
@@ -338,7 +338,7 @@ reloc_plt_object(Obj_Entry *obj, const E
 	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);

Modified: head/libexec/rtld-elf/rtld.c
==============================================================================
--- head/libexec/rtld-elf/rtld.c	Thu Mar 22 14:11:10 2012	(r233306)
+++ head/libexec/rtld-elf/rtld.c	Thu Mar 22 14:20:51 2012	(r233307)
@@ -3733,7 +3733,7 @@ tls_get_addr_slow(Elf_Addr **dtvp, int i
     /* 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;
@@ -3788,7 +3788,7 @@ allocate_tls(Obj_Entry *objs, void *oldt
 	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) {
@@ -3804,7 +3804,7 @@ allocate_tls(Obj_Entry *objs, void *oldt
 	    }
 	}
     } 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;
@@ -3868,8 +3868,8 @@ allocate_tls(Obj_Entry *objs, void *oldt
     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;
@@ -4209,7 +4209,7 @@ rtld_verify_object_versions(Obj_Entry *o
      * 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) {

Modified: head/libexec/rtld-elf/rtld.h
==============================================================================
--- head/libexec/rtld-elf/rtld.h	Thu Mar 22 14:11:10 2012	(r233306)
+++ head/libexec/rtld-elf/rtld.h	Thu Mar 22 14:20:51 2012	(r233307)
@@ -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;
@@ -319,7 +319,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_[];

Modified: head/libexec/rtld-elf/xmalloc.c
==============================================================================
--- head/libexec/rtld-elf/xmalloc.c	Thu Mar 22 14:11:10 2012	(r233306)
+++ head/libexec/rtld-elf/xmalloc.c	Thu Mar 22 14:20:51 2012	(r233307)
@@ -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 *



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201203221420.q2MEKptP027771>