lib: initial use of reallocarray(3).

Make some use of reallocarray, attempting to limit it to cases where the
parameters are unsigned and there is some theoretical chance of overflow.

MFC afer:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D9980
This commit is contained in:
Pedro F. Giffuni 2017-04-21 19:27:33 +00:00
parent 6284ff8398
commit efa8af7c73
5 changed files with 7 additions and 7 deletions

View File

@ -76,8 +76,8 @@ gss_add_buffer_set_member(OM_uint32 * minor_status,
}
set = *buffer_set;
set->elements = realloc(set->elements,
(set->count + 1) * sizeof(set->elements[0]));
set->elements = reallocarray(set->elements, set->count + 1,
sizeof(set->elements[0]));
if (set->elements == NULL) {
*minor_status = ENOMEM;
return (GSS_S_FAILURE);

View File

@ -259,8 +259,8 @@ get_recommend(_ISO2022EncodingInfo * __restrict ei,
if (!ei->recommend[i])
ei->recommend[i] = malloc(sizeof(_ISO2022Charset));
else {
p = realloc(ei->recommend[i],
sizeof(_ISO2022Charset) * (ei->recommendsize[i] + 1));
p = reallocarray(ei->recommend[i], ei->recommendsize[i] + 1,
sizeof(_ISO2022Charset));
if (!p)
return (_PARSEFAIL);
ei->recommend[i] = p;

View File

@ -205,7 +205,7 @@ gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr)
if (eof)
break;
while ((size_t)(q - p) >= size) {
if ((tmp = realloc(buf, size * 2)) == NULL) {
if ((tmp = reallocarray(buf, 2, size)) == NULL) {
warnx("group line too long");
goto err;
}

View File

@ -86,7 +86,7 @@ allocarray(size_t sz)
if (sz <= internal_arraysz)
p = internal_array;
else if ((p = realloc(internal_array, sz * sizeof(char*))) != NULL) {
else if ((p = reallocarray(internal_array, sz, sizeof(char*))) != NULL) {
internal_arraysz = sz;
internal_array = p;
}

View File

@ -468,7 +468,7 @@ pw_copy(int ffd, int tfd, const struct passwd *pw, struct passwd *old_pw)
if (eof)
break;
while ((size_t)(q - p) >= size) {
if ((tmp = realloc(buf, size * 2)) == NULL) {
if ((tmp = reallocarray(buf, 2, size)) == NULL) {
warnx("passwd line too long");
goto err;
}