mirror of
https://gitlab.freedesktop.org/libbsd/libbsd.git
synced 2025-05-19 04:47:39 +02:00
Use reallocarray() instead of malloc() or realloc()
This commit is contained in:
parent
30e328cbf1
commit
32388fe59f
@ -68,7 +68,7 @@ fgetwln(FILE *stream, size_t *lenp)
|
|||||||
else
|
else
|
||||||
fb->len = FILEWBUF_INIT_LEN;
|
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) {
|
if (wp == NULL) {
|
||||||
wused = 0;
|
wused = 0;
|
||||||
break;
|
break;
|
||||||
|
@ -118,7 +118,8 @@ sradixsort(const u_char **a, int n, const u_char *tab, u_int endch)
|
|||||||
if (n < THRESHOLD)
|
if (n < THRESHOLD)
|
||||||
simplesort(a, n, 0, tr, endch);
|
simplesort(a, n, 0, tr, endch);
|
||||||
else {
|
else {
|
||||||
if ((ta = malloc(n * sizeof(a))) == NULL)
|
ta = reallocarray(NULL, n, sizeof(a));
|
||||||
|
if (ta == NULL)
|
||||||
return (-1);
|
return (-1);
|
||||||
r_sort_b(a, ta, n, 0, tr, endch);
|
r_sort_b(a, ta, n, 0, tr, endch);
|
||||||
free(ta);
|
free(ta);
|
||||||
|
@ -154,7 +154,7 @@ common: if (set->cmd2 & CMD2_CLR) {
|
|||||||
if (set >= endset) { \
|
if (set >= endset) { \
|
||||||
BITCMD *newset; \
|
BITCMD *newset; \
|
||||||
setlen += SET_LEN_INCR; \
|
setlen += SET_LEN_INCR; \
|
||||||
newset = realloc(saveset, sizeof(BITCMD) * setlen); \
|
newset = reallocarray(saveset, setlen, sizeof(BITCMD)); \
|
||||||
if (newset == NULL) \
|
if (newset == NULL) \
|
||||||
goto out; \
|
goto out; \
|
||||||
set = newset + (set - saveset); \
|
set = newset + (set - saveset); \
|
||||||
@ -197,7 +197,8 @@ setmode(const char *p)
|
|||||||
|
|
||||||
setlen = SET_LEN + 2;
|
setlen = SET_LEN + 2;
|
||||||
|
|
||||||
if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
|
set = reallocarray(NULL, setlen, sizeof(BITCMD));
|
||||||
|
if (set == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
saveset = set;
|
saveset = set;
|
||||||
endset = set + (setlen - 2);
|
endset = set + (setlen - 2);
|
||||||
|
@ -67,7 +67,7 @@ sl_init(void)
|
|||||||
|
|
||||||
sl->sl_cur = 0;
|
sl->sl_cur = 0;
|
||||||
sl->sl_max = _SL_CHUNKSIZE;
|
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) {
|
if (sl->sl_str == NULL) {
|
||||||
free(sl);
|
free(sl);
|
||||||
sl = NULL;
|
sl = NULL;
|
||||||
@ -88,8 +88,8 @@ sl_add(StringList *sl, char *name)
|
|||||||
if (sl->sl_cur == sl->sl_max - 1) {
|
if (sl->sl_cur == sl->sl_max - 1) {
|
||||||
char **new;
|
char **new;
|
||||||
|
|
||||||
new = realloc(sl->sl_str,
|
new = reallocarray(sl->sl_str,
|
||||||
(sl->sl_max + _SL_CHUNKSIZE) * sizeof(char *));
|
(sl->sl_max + _SL_CHUNKSIZE), sizeof(char *));
|
||||||
if (new == NULL)
|
if (new == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
sl->sl_max += _SL_CHUNKSIZE;
|
sl->sl_max += _SL_CHUNKSIZE;
|
||||||
|
@ -149,7 +149,7 @@ test_fgetln_multi(void)
|
|||||||
str = strdup("A\n");
|
str = strdup("A\n");
|
||||||
str[0] += i;
|
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[0] = str;
|
||||||
files[i].lines[1] = str;
|
files[i].lines[1] = str;
|
||||||
files[i].fp = pipe_feed("%s", files[i].lines, LINE_COUNT);
|
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 = wcsdup(L"A\n");
|
||||||
wstr[0] += i;
|
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[0] = wstr;
|
||||||
files[i].lines[1] = wstr;
|
files[i].lines[1] = wstr;
|
||||||
files[i].fp = pipe_feed("%ls", files[i].lines, LINE_COUNT);
|
files[i].fp = pipe_feed("%ls", files[i].lines, LINE_COUNT);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user