]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
Use reallocarray() instead of malloc() or realloc()
authorGuillem Jover <guillem@hadrons.org>
Mon, 3 Nov 2014 22:21:52 +0000 (23:21 +0100)
committerGuillem Jover <guillem@hadrons.org>
Wed, 23 Sep 2015 05:59:34 +0000 (07:59 +0200)
src/fgetwln.c
src/radixsort.c
src/setmode.c
src/stringlist.c
test/fgetln.c

index 247393244317a0b3485656ca2de16a2a5a84408f..9ee0776882dc618eb8b1391791801043d9c96f79 100644 (file)
@@ -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;
index b9746fc161127bae8ea35fa40b0b3490537f4713..1473925779de02746fabd7c2cae0b7e64b1ffef9 100644 (file)
@@ -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);
index c3c9a8b96d9e702a498e3c8bd03dc045d19a4c80..cdc21798e9f6f23b32591c8ff87f2bac1f75e0b1 100644 (file)
@@ -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);
index ce1b2cebcacf791fe71804816cd0baad59a153f3..6e5e488f8be8eada1b0d0e1793fcac4a7bc3ff0a 100644 (file)
@@ -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;
index 83d120cc141d28d3368c81a98f7ffc482ab10587..d3814d8667901cd07189c4b2e392848eecbb0011 100644 (file)
@@ -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);