fa.openbsd.tech
[Top] [All Lists]

Re: memdup(3)

Subject: Re: memdup(3)
From: Alexey Dobriyan <adobriyan@xxxxxxxxx>
Date: Mon, 14 Jul 2008 15:19:50 UTC
Newsgroups: fa.openbsd.tech

On Mon, Jul 14, 2008 at 09:51:17AM -0500, Marco Peereboom wrote:
> On Mon, Jul 14, 2008 at 06:38:49PM +0400, Alexey Dobriyan wrote:
> > On Mon, Jul 14, 2008 at 03:06:14PM +0200, Marc Espie wrote:
> > > On Mon, Jul 14, 2008 at 04:49:34PM +0400, Alexey Dobriyan wrote:
> > > > memdup(3) has strdup(3) semantics but without strings.
> > > > 
> > > > Canonical code for duplicating buffer looks like:
> > > > 
> > > >         rpl = malloc(src, len);
> > > >         if (!rpl)
> > > >                 ...
> > > >         memcpy(rpl, src, len);
> > > > 
> > > > Mistakes happen and two lengths in snippet above will be different.
> > > > To prevent this memdup(3) was created:
> > > > 
> > > >         rpl = memdup(src, len);
> > > >         if (!rpl)
> > > >                 ...
> > > >         ...
> > > 
> > > This kind of code is very easy to write. The big question is: do we
> > > want it.
> > > 
> > > That's non-standard stuff, it's not ANSI, it's not POSIX, it's not single
> > > unix.
> > > 
> > > So what's the point ?
> > 
> > The point is writing "len" once, so it won't diverge from itself.
> 
> But you trade an explicit free for and implicit one.

malloc, you mean.

> I find this a confusing interface that adds nothing.

Do you find strdup() confusing?

> It is also not part of a standard.

Eventually it will be.

> > Plenty of examples in OpenBSD codebase alone.
> 
> Sure and they aren't broken.


<Prev in Thread] Current Thread [Next in Thread>