diff --git a/Src/builtin.c b/Src/builtin.c index e901fcc11..b94a67f02 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -6393,7 +6393,8 @@ int bin_read(char *name, char **args, Options ops, UNUSED(int func)) { char *reply, *readpmpt; - int bsiz, c = 0, gotnl = 0, al = 0, first, nchars = 1, bslash, keys = 0; + int c = 0, gotnl = 0, al = 0, first, nchars = 1, bslash, keys = 0; + size_t bsiz; int haso = 0; /* true if /dev/tty has been opened specially */ int isem = !strcmp(term, "emacs"), izle = zleactive; char *buf, *bptr, *firstarg, *zbuforig; @@ -6861,12 +6862,17 @@ bin_read(char *name, char **args, Options ops, UNUSED(int func)) #endif /* increase the buffer size, if necessary */ if (bptr >= buf + bsiz - 1) { - int blen = bptr - buf; + size_t blen = bptr - buf; #ifdef MULTIBYTE_SUPPORT - int llen = laststart - buf; + size_t llen = laststart - buf; #endif - buf = realloc(buf, bsiz *= 2); + if (bsiz > SIZE_MAX / 2) { + /* doubling would wrap: the address space is exhausted */ + zerr("fatal error: out of memory"); + exit(1); + } + buf = zrealloc(buf, bsiz *= 2); bptr = buf + blen; #ifdef MULTIBYTE_SUPPORT laststart = buf + llen; @@ -7044,12 +7050,17 @@ bin_read(char *name, char **args, Options ops, UNUSED(int func)) #endif /* increase the buffer size, if necessary */ if (bptr >= buf + bsiz - 1) { - int blen = bptr - buf; + size_t blen = bptr - buf; #ifdef MULTIBYTE_SUPPORT - int llen = laststart - buf; + size_t llen = laststart - buf; #endif - buf = realloc(buf, bsiz *= 2); + if (bsiz > SIZE_MAX / 2) { + /* doubling would wrap: the address space is exhausted */ + zerr("fatal error: out of memory"); + exit(1); + } + buf = zrealloc(buf, bsiz *= 2); bptr = buf + blen; #ifdef MULTIBYTE_SUPPORT laststart = buf + llen;