mailing list of musl libc
 help / color / Atom feed
* [musl] perhaps we should add re[c]allocarray?
@ 2020-07-21 10:18 Ariadne Conill
  2020-07-21 16:56 ` Markus Wichmann
  2020-07-21 18:39 ` Rich Felker
  0 siblings, 2 replies; 11+ messages in thread
From: Ariadne Conill @ 2020-07-21 10:18 UTC (permalink / raw)
  To: musl

Hello,

reallocarray and recallocarray are BSD extensions that solve similar issues as 
strlcpy/strlcat, but with array reallocations instead of strings.

reallocarray itself is already part of glibc since 2.28.

Unfortunately, while working on new ifupdown implementation for Alpine, I 
wanted to use recallocarray because it is very helpful in terms of pushing new 
strings to a string array (you will always maintain a NULL-terminated array, 
and you don't have to worry about it) -- but I discovered musl still does not 
have it.

Anyway, I think it would be useful to include both functions in musl 1.2.1.  
If everyone agrees, I'll make a patch.

Ariadne



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [musl] perhaps we should add re[c]allocarray?
  2020-07-21 10:18 [musl] perhaps we should add re[c]allocarray? Ariadne Conill
@ 2020-07-21 16:56 ` Markus Wichmann
  2020-07-21 17:27   ` Hadrien Lacour
  2020-07-21 23:19   ` Ariadne Conill
  2020-07-21 18:39 ` Rich Felker
  1 sibling, 2 replies; 11+ messages in thread
From: Markus Wichmann @ 2020-07-21 16:56 UTC (permalink / raw)
  To: musl

On Tue, Jul 21, 2020 at 04:18:35AM -0600, Ariadne Conill wrote:
> Hello,
>
> reallocarray and recallocarray are BSD extensions that solve similar issues as
> strlcpy/strlcat, but with array reallocations instead of strings.
>
> reallocarray itself is already part of glibc since 2.28.
>
> Unfortunately, while working on new ifupdown implementation for Alpine, I
> wanted to use recallocarray because it is very helpful in terms of pushing new
> strings to a string array (you will always maintain a NULL-terminated array,
> and you don't have to worry about it) -- but I discovered musl still does not
> have it.
>
> Anyway, I think it would be useful to include both functions in musl 1.2.1.
> If everyone agrees, I'll make a patch.
>
> Ariadne
>
>

Seems mostly useless to me. reallocarray() is equivalent to realloc(),
multiplying the last two arguments. And recallocarray() does seem
useful, but moreso as a subroutine. I see little reason to put this into
a standard library.

On a formal point of view, neither of these has been standardized. I can
find an Oracle man page for reallocarray(), but not recallocarray().
Both are OpenBSD extensions. For glibc, I can find reallocarray() (which
mostly wraps realloc()), but no recallocarray() (I checked in the most
recent released version, which is 2.31 as of right now).

It appears, reallocarray() enjoys more widespread adoption than
recallocarray(). Both can, however, be easily found by a compile/link
test. As stated above, however, the necessary functionality can easily
be written in whatever application needs it, so I don't see the point.
I've done that before; it is two lines if you manage your variables
well.

JM2C,
Markus

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [musl] perhaps we should add re[c]allocarray?
  2020-07-21 16:56 ` Markus Wichmann
@ 2020-07-21 17:27   ` Hadrien Lacour
  2020-07-21 23:19   ` Ariadne Conill
  1 sibling, 0 replies; 11+ messages in thread
From: Hadrien Lacour @ 2020-07-21 17:27 UTC (permalink / raw)
  To: musl

On Tue, Jul 21, 2020 at 06:56:57PM +0200, Markus Wichmann wrote:
> On Tue, Jul 21, 2020 at 04:18:35AM -0600, Ariadne Conill wrote:
> > Hello,
> >
> > reallocarray and recallocarray are BSD extensions that solve similar issues as
> > strlcpy/strlcat, but with array reallocations instead of strings.
> >
> > reallocarray itself is already part of glibc since 2.28.
> >
> > Unfortunately, while working on new ifupdown implementation for Alpine, I
> > wanted to use recallocarray because it is very helpful in terms of pushing new
> > strings to a string array (you will always maintain a NULL-terminated array,
> > and you don't have to worry about it) -- but I discovered musl still does not
> > have it.
> >
> > Anyway, I think it would be useful to include both functions in musl 1.2.1.
> > If everyone agrees, I'll make a patch.
> >
> > Ariadne
> >
> >
>
> Seems mostly useless to me. reallocarray() is equivalent to realloc(),
> multiplying the last two arguments. And recallocarray() does seem
> useful, but moreso as a subroutine. I see little reason to put this into
> a standard library.
>
> On a formal point of view, neither of these has been standardized. I can
> find an Oracle man page for reallocarray(), but not recallocarray().
> Both are OpenBSD extensions. For glibc, I can find reallocarray() (which
> mostly wraps realloc()), but no recallocarray() (I checked in the most
> recent released version, which is 2.31 as of right now).
>
> It appears, reallocarray() enjoys more widespread adoption than
> recallocarray(). Both can, however, be easily found by a compile/link
> test. As stated above, however, the necessary functionality can easily
> be written in whatever application needs it, so I don't see the point.
> I've done that before; it is two lines if you manage your variables
> well.
>
> JM2C,
> Markus

I'm pretty sure the point of reallocarray is that it checks for overflow during
the multiplication of the arguments.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [musl] perhaps we should add re[c]allocarray?
  2020-07-21 10:18 [musl] perhaps we should add re[c]allocarray? Ariadne Conill
  2020-07-21 16:56 ` Markus Wichmann
@ 2020-07-21 18:39 ` Rich Felker
  2020-07-21 18:58   ` Florian Weimer
  2020-07-21 19:40   ` Leah Neukirchen
  1 sibling, 2 replies; 11+ messages in thread
From: Rich Felker @ 2020-07-21 18:39 UTC (permalink / raw)
  To: Ariadne Conill; +Cc: musl

On Tue, Jul 21, 2020 at 04:18:35AM -0600, Ariadne Conill wrote:
> Hello,
> 
> reallocarray and recallocarray are BSD extensions that solve similar issues as 
> strlcpy/strlcat, but with array reallocations instead of strings.
> 
> reallocarray itself is already part of glibc since 2.28.
> 
> Unfortunately, while working on new ifupdown implementation for Alpine, I 
> wanted to use recallocarray because it is very helpful in terms of pushing new 
> strings to a string array (you will always maintain a NULL-terminated array, 
> and you don't have to worry about it) -- but I discovered musl still does not 
> have it.
> 
> Anyway, I think it would be useful to include both functions in musl 1.2.1.  
> If everyone agrees, I'll make a patch.

reallocarray is a straightforward wrapper around realloc that can be
implemented portably to work with arbitrary underlying malloc and is
fairly non-controversial. I think it was already loosely agreed at
some point that we would eventually support this.

recallocarray presumably needs to zero the new part which means it
needs to know the old exact size, which means it depends on having
either knowledge of implementation internals or a working, exact
malloc_usable_size (AFAIK all legacy/existing ones except musl
mallocng are broken and return a value greater than the originally
allocated size). Implementing it interferes with safety of
overriding/interposing malloc, and therefore I'm fairly strongly
against it unless there's a widepread consensus between implementors
that it should exist.

Is there a strong reason you want recallocarray rather than just
reallocarray?

Rich

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [musl] perhaps we should add re[c]allocarray?
  2020-07-21 18:39 ` Rich Felker
@ 2020-07-21 18:58   ` Florian Weimer
  2020-07-21 20:40     ` Rich Felker
  2020-07-21 19:40   ` Leah Neukirchen
  1 sibling, 1 reply; 11+ messages in thread
From: Florian Weimer @ 2020-07-21 18:58 UTC (permalink / raw)
  To: Rich Felker; +Cc: Ariadne Conill, musl

* Rich Felker:

> recallocarray presumably needs to zero the new part which means it
> needs to know the old exact size, which means it depends on having
> either knowledge of implementation internals or a working, exact
> malloc_usable_size (AFAIK all legacy/existing ones except musl
> mallocng are broken and return a value greater than the originally
> allocated size).

The caller has to pass the old member count to recallocarray, in an
additional argument.  I think this avoids this particular issue, and
also makes it easy to achive interposition-safety.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [musl] perhaps we should add re[c]allocarray?
  2020-07-21 18:39 ` Rich Felker
  2020-07-21 18:58   ` Florian Weimer
@ 2020-07-21 19:40   ` Leah Neukirchen
  1 sibling, 0 replies; 11+ messages in thread
From: Leah Neukirchen @ 2020-07-21 19:40 UTC (permalink / raw)
  To: Rich Felker; +Cc: Ariadne Conill, musl

Rich Felker <dalias@libc.org> writes:

> On Tue, Jul 21, 2020 at 04:18:35AM -0600, Ariadne Conill wrote:
>> Hello,
>> 
>> reallocarray and recallocarray are BSD extensions that solve similar issues as 
>> strlcpy/strlcat, but with array reallocations instead of strings.
>> 
>> reallocarray itself is already part of glibc since 2.28.
>> 
>> Unfortunately, while working on new ifupdown implementation for Alpine, I 
>> wanted to use recallocarray because it is very helpful in terms of pushing new 
>> strings to a string array (you will always maintain a NULL-terminated array, 
>> and you don't have to worry about it) -- but I discovered musl still does not 
>> have it.
>> 
>> Anyway, I think it would be useful to include both functions in musl 1.2.1.  
>> If everyone agrees, I'll make a patch.
>
> reallocarray is a straightforward wrapper around realloc that can be
> implemented portably to work with arbitrary underlying malloc and is
> fairly non-controversial. I think it was already loosely agreed at
> some point that we would eventually support this.
>
> recallocarray presumably needs to zero the new part which means it
> needs to know the old exact size, which means it depends on having
> either knowledge of implementation internals or a working, exact
> malloc_usable_size (AFAIK all legacy/existing ones except musl
> mallocng are broken and return a value greater than the originally
> allocated size). Implementing it interferes with safety of
> overriding/interposing malloc, and therefore I'm fairly strongly
> against it unless there's a widepread consensus between implementors
> that it should exist.

No, it's an argument:
void *recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)

-- 
Leah Neukirchen  <leah@vuxu.org>  https://leahneukirchen.org/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [musl] perhaps we should add re[c]allocarray?
  2020-07-21 18:58   ` Florian Weimer
@ 2020-07-21 20:40     ` Rich Felker
  2020-07-21 23:21       ` Ariadne Conill
  0 siblings, 1 reply; 11+ messages in thread
From: Rich Felker @ 2020-07-21 20:40 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Ariadne Conill, musl

On Tue, Jul 21, 2020 at 08:58:04PM +0200, Florian Weimer wrote:
> * Rich Felker:
> 
> > recallocarray presumably needs to zero the new part which means it
> > needs to know the old exact size, which means it depends on having
> > either knowledge of implementation internals or a working, exact
> > malloc_usable_size (AFAIK all legacy/existing ones except musl
> > mallocng are broken and return a value greater than the originally
> > allocated size).
> 
> The caller has to pass the old member count to recallocarray, in an
> additional argument.  I think this avoids this particular issue, and
> also makes it easy to achive interposition-safety.

Ah, great, that makes it a non-issue then, and in that case I have no
significant objections to it.

Rich

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [musl] perhaps we should add re[c]allocarray?
  2020-07-21 16:56 ` Markus Wichmann
  2020-07-21 17:27   ` Hadrien Lacour
@ 2020-07-21 23:19   ` Ariadne Conill
  1 sibling, 0 replies; 11+ messages in thread
From: Ariadne Conill @ 2020-07-21 23:19 UTC (permalink / raw)
  To: musl

Hello,

On Tuesday, July 21, 2020 10:56:57 AM MDT Markus Wichmann wrote:
> On Tue, Jul 21, 2020 at 04:18:35AM -0600, Ariadne Conill wrote:
> > Hello,
> > 
> > reallocarray and recallocarray are BSD extensions that solve similar
> > issues as strlcpy/strlcat, but with array reallocations instead of
> > strings.
> > 
> > reallocarray itself is already part of glibc since 2.28.
> > 
> > Unfortunately, while working on new ifupdown implementation for Alpine, I
> > wanted to use recallocarray because it is very helpful in terms of pushing
> > new strings to a string array (you will always maintain a NULL-terminated
> > array, and you don't have to worry about it) -- but I discovered musl
> > still does not have it.
> > 
> > Anyway, I think it would be useful to include both functions in musl
> > 1.2.1.
> > If everyone agrees, I'll make a patch.
> > 
> > Ariadne
> 
> Seems mostly useless to me. reallocarray() is equivalent to realloc(),
> multiplying the last two arguments. And recallocarray() does seem
> useful, but moreso as a subroutine. I see little reason to put this into
> a standard library.

The reason is that we would like to see people use these routines instead of 
fussing with realloc() directly because they do the right thing.  It is better 
to provide the right thing in the standard library instead of having people 
mess it up with their own implementation.

> On a formal point of view, neither of these has been standardized. I can
> find an Oracle man page for reallocarray(), but not recallocarray().
> Both are OpenBSD extensions. For glibc, I can find reallocarray() (which
> mostly wraps realloc()), but no recallocarray() (I checked in the most
> recent released version, which is 2.31 as of right now).

As I previously stated, both are BSD extensions, so I do not understand why 
you are mentioning it again.  At any rate, I plan to propose these extensions 
for inclusion in next POSIX revision.  Just haven't gotten around to writing 
to the Austin Group yet.

> It appears, reallocarray() enjoys more widespread adoption than
> recallocarray(). Both can, however, be easily found by a compile/link
> test. As stated above, however, the necessary functionality can easily
> be written in whatever application needs it, so I don't see the point.
> I've done that before; it is two lines if you manage your variables
> well.

While it is possible to probe for these functions using autoconf or meson or 
whatever, Alpine approaches these concerns from the standpoint that the libc 
provides what Alpine requires for its own utilities.  For now, we will carry 
our own recallocarray in ifupdown, but it would be nice to drop this at some 
point.  That is what *this* thread is about.

Ariadne



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [musl] perhaps we should add re[c]allocarray?
  2020-07-21 20:40     ` Rich Felker
@ 2020-07-21 23:21       ` Ariadne Conill
  2020-07-22  0:21         ` Rich Felker
  0 siblings, 1 reply; 11+ messages in thread
From: Ariadne Conill @ 2020-07-21 23:21 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl

Hello,

On Tuesday, July 21, 2020 2:40:53 PM MDT you wrote:
> On Tue, Jul 21, 2020 at 08:58:04PM +0200, Florian Weimer wrote:
> > * Rich Felker:
> > > recallocarray presumably needs to zero the new part which means it
> > > needs to know the old exact size, which means it depends on having
> > > either knowledge of implementation internals or a working, exact
> > > malloc_usable_size (AFAIK all legacy/existing ones except musl
> > > mallocng are broken and return a value greater than the originally
> > > allocated size).
> > 
> > The caller has to pass the old member count to recallocarray, in an
> > additional argument.  I think this avoids this particular issue, and
> > also makes it easy to achive interposition-safety.
> 
> Ah, great, that makes it a non-issue then, and in that case I have no
> significant objections to it.

Okay great.  I will work on reallocarray() first and then follow up with 
recallocarray().

Ariadne




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [musl] perhaps we should add re[c]allocarray?
  2020-07-21 23:21       ` Ariadne Conill
@ 2020-07-22  0:21         ` Rich Felker
  2020-07-22  0:24           ` Ariadne Conill
  0 siblings, 1 reply; 11+ messages in thread
From: Rich Felker @ 2020-07-22  0:21 UTC (permalink / raw)
  To: Ariadne Conill; +Cc: musl

On Tue, Jul 21, 2020 at 05:21:03PM -0600, Ariadne Conill wrote:
> Hello,
> 
> On Tuesday, July 21, 2020 2:40:53 PM MDT you wrote:
> > On Tue, Jul 21, 2020 at 08:58:04PM +0200, Florian Weimer wrote:
> > > * Rich Felker:
> > > > recallocarray presumably needs to zero the new part which means it
> > > > needs to know the old exact size, which means it depends on having
> > > > either knowledge of implementation internals or a working, exact
> > > > malloc_usable_size (AFAIK all legacy/existing ones except musl
> > > > mallocng are broken and return a value greater than the originally
> > > > allocated size).
> > > 
> > > The caller has to pass the old member count to recallocarray, in an
> > > additional argument.  I think this avoids this particular issue, and
> > > also makes it easy to achive interposition-safety.
> > 
> > Ah, great, that makes it a non-issue then, and in that case I have no
> > significant objections to it.
> 
> Okay great.  I will work on reallocarray() first and then follow up with 
> recallocarray().

Yes, reallocarray should be very straightforward. I think
recallocarray should probably involve a slight refactor of calloc.c to
make mal0_clear external so it can be reused; otherwise recallocarray
would be significantly worse than calloc/memcpy/free since it would
fault in all pages right away.

Rich

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [musl] perhaps we should add re[c]allocarray?
  2020-07-22  0:21         ` Rich Felker
@ 2020-07-22  0:24           ` Ariadne Conill
  0 siblings, 0 replies; 11+ messages in thread
From: Ariadne Conill @ 2020-07-22  0:24 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl

Hello,

On Tuesday, July 21, 2020 6:21:16 PM MDT Rich Felker wrote:
> On Tue, Jul 21, 2020 at 05:21:03PM -0600, Ariadne Conill wrote:
> > Hello,
> > 
> > On Tuesday, July 21, 2020 2:40:53 PM MDT you wrote:
> > > On Tue, Jul 21, 2020 at 08:58:04PM +0200, Florian Weimer wrote:
> > > > * Rich Felker:
> > > > > recallocarray presumably needs to zero the new part which means it
> > > > > needs to know the old exact size, which means it depends on having
> > > > > either knowledge of implementation internals or a working, exact
> > > > > malloc_usable_size (AFAIK all legacy/existing ones except musl
> > > > > mallocng are broken and return a value greater than the originally
> > > > > allocated size).
> > > > 
> > > > The caller has to pass the old member count to recallocarray, in an
> > > > additional argument.  I think this avoids this particular issue, and
> > > > also makes it easy to achive interposition-safety.
> > > 
> > > Ah, great, that makes it a non-issue then, and in that case I have no
> > > significant objections to it.
> > 
> > Okay great.  I will work on reallocarray() first and then follow up with
> > recallocarray().
> 
> Yes, reallocarray should be very straightforward. I think
> recallocarray should probably involve a slight refactor of calloc.c to
> make mal0_clear external so it can be reused; otherwise recallocarray
> would be significantly worse than calloc/memcpy/free since it would
> fault in all pages right away.

Yeah, I was going to alias mal0_clear to __malloc_mal0_clear or similar as 
part of recallocarray work.

Ariadne



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, back to index

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-21 10:18 [musl] perhaps we should add re[c]allocarray? Ariadne Conill
2020-07-21 16:56 ` Markus Wichmann
2020-07-21 17:27   ` Hadrien Lacour
2020-07-21 23:19   ` Ariadne Conill
2020-07-21 18:39 ` Rich Felker
2020-07-21 18:58   ` Florian Weimer
2020-07-21 20:40     ` Rich Felker
2020-07-21 23:21       ` Ariadne Conill
2020-07-22  0:21         ` Rich Felker
2020-07-22  0:24           ` Ariadne Conill
2020-07-21 19:40   ` Leah Neukirchen

mailing list of musl libc

Archives are clonable: git clone --mirror http://inbox.vuxu.org/musl

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.musl


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git