Re: [hackers] [sbase] ed: Use reallocarray || Michael Forney

2020-01-02 Thread Hiltjo Posthuma
On Thu, Jan 02, 2020 at 08:28:40AM +, k0ga wrote:
> Hi,
> 
> 
> On Tue, Dec 31, 2019 at 10:47:28PM +0100, g...@suckless.org wrote:
> > diff --git a/ed.c b/ed.c
> > index b844e86..e998e81 100644
> > --- a/ed.c
> > +++ b/ed.c
> > @@ -204,7 +204,7 @@ makeline(char *s, int *off)
> > if (lastidx >= idxsize) {
> > lp = NULL;
> > if (idxsize <= SIZE_MAX - NUMLINES)
> > -   lp = realloc(zero, (idxsize + NUMLINES) * sizeof(*lp));
> > +   lp = reallocarray(zero, idxsize + NUMLINES, sizeof(*lp));
> 
> reallocarray is not a posix function, so it shouldn't be used here. If you
> don't fell confortable using realloc due to the possibility of overflow then
> you should add an explicit check against it.
> 
> Regards,
> 

sbase has reallocarray in libutil:
https://git.suckless.org/sbase/file/libutil/reallocarray.c.html

-- 
Kind regards,
Hiltjo



Re: [hackers] [sbase] ed: Use reallocarray || Michael Forney

2020-01-02 Thread k0ga
Hi,


On Tue, Dec 31, 2019 at 10:47:28PM +0100, g...@suckless.org wrote:
> diff --git a/ed.c b/ed.c
> index b844e86..e998e81 100644
> --- a/ed.c
> +++ b/ed.c
> @@ -204,7 +204,7 @@ makeline(char *s, int *off)
>   if (lastidx >= idxsize) {
>   lp = NULL;
>   if (idxsize <= SIZE_MAX - NUMLINES)
> - lp = realloc(zero, (idxsize + NUMLINES) * sizeof(*lp));
> + lp = reallocarray(zero, idxsize + NUMLINES, sizeof(*lp));

reallocarray is not a posix function, so it shouldn't be used here. If you
don't fell confortable using realloc due to the possibility of overflow then
you should add an explicit check against it.

Regards,



[hackers] [sbase] ed: Use reallocarray || Michael Forney

2019-12-31 Thread git
commit a8dc42e6b5aa0926ec3bea32dd17bd319d0f3b99
Author: Michael Forney 
AuthorDate: Tue Dec 31 13:41:38 2019 -0800
Commit: Michael Forney 
CommitDate: Tue Dec 31 13:41:38 2019 -0800

ed: Use reallocarray

diff --git a/ed.c b/ed.c
index b844e86..e998e81 100644
--- a/ed.c
+++ b/ed.c
@@ -204,7 +204,7 @@ makeline(char *s, int *off)
if (lastidx >= idxsize) {
lp = NULL;
if (idxsize <= SIZE_MAX - NUMLINES)
-   lp = realloc(zero, (idxsize + NUMLINES) * sizeof(*lp));
+   lp = reallocarray(zero, idxsize + NUMLINES, sizeof(*lp));
if (!lp)
error("out of memory");
idxsize += NUMLINES;