Commit 32388fe59f526a150058b58a8bc22785e4b6b4cc

Guillem Jover 2014-11-03T23:21:52

Use reallocarray() instead of malloc() or realloc()

diff --git a/src/fgetwln.c b/src/fgetwln.c
index 2473932..9ee0776 100644
--- a/src/fgetwln.c
+++ b/src/fgetwln.c
@@ -68,7 +68,7 @@ fgetwln(FILE *stream, size_t *lenp)
 			else
 				fb->len = FILEWBUF_INIT_LEN;
 
-			wp = realloc(fb->wbuf, fb->len * sizeof(wchar_t));
+			wp = reallocarray(fb->wbuf, fb->len, sizeof(wchar_t));
 			if (wp == NULL) {
 				wused = 0;
 				break;
diff --git a/src/radixsort.c b/src/radixsort.c
index b9746fc..1473925 100644
--- a/src/radixsort.c
+++ b/src/radixsort.c
@@ -118,7 +118,8 @@ sradixsort(const u_char **a, int n, const u_char *tab, u_int endch)
 	if (n < THRESHOLD)
 		simplesort(a, n, 0, tr, endch);
 	else {
-		if ((ta = malloc(n * sizeof(a))) == NULL)
+		ta = reallocarray(NULL, n, sizeof(a));
+		if (ta == NULL)
 			return (-1);
 		r_sort_b(a, ta, n, 0, tr, endch);
 		free(ta);
diff --git a/src/setmode.c b/src/setmode.c
index c3c9a8b..cdc2179 100644
--- a/src/setmode.c
+++ b/src/setmode.c
@@ -154,7 +154,7 @@ common:			if (set->cmd2 & CMD2_CLR) {
 	if (set >= endset) {						\
 		BITCMD *newset;						\
 		setlen += SET_LEN_INCR;					\
-		newset = realloc(saveset, sizeof(BITCMD) * setlen);	\
+		newset = reallocarray(saveset, setlen, sizeof(BITCMD));	\
 		if (newset == NULL)					\
 			goto out;					\
 		set = newset + (set - saveset);				\
@@ -197,7 +197,8 @@ setmode(const char *p)
 
 	setlen = SET_LEN + 2;
 
-	if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
+	set = reallocarray(NULL, setlen, sizeof(BITCMD));
+	if (set == NULL)
 		return (NULL);
 	saveset = set;
 	endset = set + (setlen - 2);
diff --git a/src/stringlist.c b/src/stringlist.c
index ce1b2ce..6e5e488 100644
--- a/src/stringlist.c
+++ b/src/stringlist.c
@@ -67,7 +67,7 @@ sl_init(void)
 
 	sl->sl_cur = 0;
 	sl->sl_max = _SL_CHUNKSIZE;
-	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
+	sl->sl_str = reallocarray(NULL, sl->sl_max, sizeof(char *));
 	if (sl->sl_str == NULL) {
 		free(sl);
 		sl = NULL;
@@ -88,8 +88,8 @@ sl_add(StringList *sl, char *name)
 	if (sl->sl_cur == sl->sl_max - 1) {
 		char	**new;
 
-		new = realloc(sl->sl_str,
-		    (sl->sl_max + _SL_CHUNKSIZE) * sizeof(char *));
+		new = reallocarray(sl->sl_str,
+		    (sl->sl_max + _SL_CHUNKSIZE), sizeof(char *));
 		if (new == NULL)
 			return -1;
 		sl->sl_max += _SL_CHUNKSIZE;
diff --git a/test/fgetln.c b/test/fgetln.c
index 83d120c..d3814d8 100644
--- a/test/fgetln.c
+++ b/test/fgetln.c
@@ -149,7 +149,7 @@ test_fgetln_multi(void)
 		str = strdup("A\n");
 		str[0] += i;
 
-		files[i].lines = malloc(sizeof(char *) * LINE_COUNT);
+		files[i].lines = reallocarray(NULL, LINE_COUNT, sizeof(char *));
 		files[i].lines[0] = str;
 		files[i].lines[1] = str;
 		files[i].fp = pipe_feed("%s", files[i].lines, LINE_COUNT);
@@ -211,7 +211,7 @@ test_fgetwln_multi(void)
 		wstr = wcsdup(L"A\n");
 		wstr[0] += i;
 
-		files[i].lines = malloc(sizeof(char *) * LINE_COUNT);
+		files[i].lines = reallocarray(NULL, LINE_COUNT, sizeof(char *));
 		files[i].lines[0] = wstr;
 		files[i].lines[1] = wstr;
 		files[i].fp = pipe_feed("%ls", files[i].lines, LINE_COUNT);