Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 249731 - scanelf.c no longer compiles: *BSD lacks POSIX strndup()
Summary: scanelf.c no longer compiles: *BSD lacks POSIX strndup()
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All All
: High normal
Assignee: Diego Elio Pettenò (RETIRED)
URL:
Whiteboard:
Keywords:
: 250359 254144 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-12-03 20:48 UTC by Fabian Groffen
Modified: 2009-10-25 21:02 UTC (History)
6 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
proposed no-strndup patch (p.patch,1.20 KB, patch)
2008-12-03 20:48 UTC, Fabian Groffen
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Fabian Groffen gentoo-dev 2008-12-03 20:48:01 UTC
revision 1.197 of scanelf.c broke it on a handful of non-linux platforms.

The attached patch avoids the use of strndup.  It should be faster for free too, since malloc/free is expensive.  Should work unless one can't modify ret, of course.  In any case, please remove the use of strndup.
Comment 1 Fabian Groffen gentoo-dev 2008-12-03 20:48:58 UTC
Created attachment 174190 [details, diff]
proposed no-strndup patch
Comment 2 SpanKY gentoo-dev 2008-12-09 13:42:45 UTC
*** Bug 250359 has been marked as a duplicate of this bug. ***
Comment 3 Javier Villavicencio (RETIRED) gentoo-dev 2009-01-07 20:48:24 UTC
*** Bug 254144 has been marked as a duplicate of this bug. ***
Comment 4 solar (RETIRED) gentoo-dev 2009-01-07 21:30:26 UTC
The title of this bug is misleading. strndup() is not POSIX extension it's a GNU one. strdup() is POSIX however. 

Also I think the memory is allocated for a reason. Like the stack gets overwritten or some such so this patch is probably not enough as is. Easy enough to add a strndup however. str = malloc(size) ; then str{l,n}cat() for the platforms that lack it.

pax-utils is a GNU tool and thus we mostly expect that we can use GNU extensions.
Comment 5 Diego Elio Pettenò (RETIRED) gentoo-dev 2009-01-07 21:35:02 UTC
The reason why I used strndup() is that there might be more symbols to be looked at so changing the input data is not good. I haven't had the time to get around adding a strndup() replacement, but that's what should be done if it's missing, rather than making the code more complex than it is.
Comment 6 Javier Villavicencio (RETIRED) gentoo-dev 2009-01-07 21:44:08 UTC
For the record, FreeBSD HEAD imported strndup from NetBSD 4 weeks ago (easy to backport)
OpenBSD lacks it, OSX 10.5.4 lacks it afaics, and Solaris (up to OpenSolaris of august last year) lacks it too.
Comment 7 Javier Villavicencio (RETIRED) gentoo-dev 2009-01-07 21:52:25 UTC
And this is (pretty much) the patch that included strndup on FreeBSD:

http://webmail.solomo.de/~flo/strndup.patch
Comment 8 solar (RETIRED) gentoo-dev 2009-01-07 22:01:54 UTC
Flameeyes,

Careful on what you import as pax-utils needs to be picky about the code it imports from others. Must be GPL-2 compat and then put under the GPL-2. 

Thanks in advance.
Comment 9 Javier Villavicencio (RETIRED) gentoo-dev 2009-01-07 22:10:59 UTC
Another thingy, if I understand this thread right, strndup has been made POSIX very recently.

http://lists.freebsd.org/pipermail/freebsd-arch/2008-December/008754.html

(Last spam from me :$)
Comment 10 Diego Elio Pettenò (RETIRED) gentoo-dev 2009-01-07 22:14:08 UTC
char *strndup(const char *orig, size_t len) {
  const size_t orig_len = strlen(orig);
  const size_t new_len = MIN(orig_len, len);
  char *out = malloc(new_len+1);
  strncpy(out, orig, new_len);

  return out;
}

This I just wrote myself on the comment without looking anywhere else, does anybody find a fault in this? :P
Comment 11 Javier Villavicencio (RETIRED) gentoo-dev 2009-01-07 22:23:14 UTC
Just 
out[new_len] = '\0';
I think.
Comment 12 Martín Conte Mac Donell 2009-01-08 01:38:20 UTC
(In reply to comment #10)
> char *strndup(const char *orig, size_t len) {
>   const size_t orig_len = strlen(orig);
>   const size_t new_len = MIN(orig_len, len);
>   char *out = malloc(new_len+1);
>   strncpy(out, orig, new_len);
> 
>   return out;
> }
> 
> This I just wrote myself on the comment without looking anywhere else, does
> anybody find a fault in this? :P
> 

I would go with this one:

char *strndup(const char *orig, size_t len) {
    const size_t orig_len = (orig == NULL) ? 0: strlen(orig);
    const size_t new_len = MIN(orig_len, len);
    char *out = malloc(new_len+1);
  
    if (out == NULL)
        return NULL;
  
    out[new_len] = '\0';
    return memcpy(out, orig, new_len);
}
Comment 13 Diego Elio Pettenò (RETIRED) gentoo-dev 2009-01-08 02:15:13 UTC
I'm not sure if that is the same behaviour as GNU strndup for strndup(NULL), to be honest, but I'd have to check to be sure.
Comment 14 SpanKY gentoo-dev 2009-01-08 05:50:14 UTC
solar: strndup() is in POSIX, so the summary is perfectly accurate

http://www.opengroup.org/onlinepubs/9699919799/functions/strndup.html

if you guys want reliable replacements, take them from gnulib
Comment 15 Javier Villavicencio (RETIRED) gentoo-dev 2009-01-08 07:16:58 UTC
The gnulib one uses strnlen, which would cause the same problem as strndup (unless strnlen is added as well :$).
Comment 16 SpanKY gentoo-dev 2009-01-08 07:49:05 UTC
yes, adding strnlen is fine

question now is whether we want to autotoolize the package ...
Comment 17 Fabian Groffen gentoo-dev 2009-01-08 08:07:48 UTC
(In reply to comment #4)
> pax-utils is a GNU tool and thus we mostly expect that we can use GNU
> extensions.

While that is a bit strange while it is used on Gentoo/FreeBSD, fine with me, but then it just needs a gnulib dependency (which I think provides strndup).

Personally I like it when it compiles without to much hassle on many platforms, since it's used on them.

(In reply to comment #16)
> yes, adding strnlen is fine
> 
> question now is whether we want to autotoolize the package ...

Would allow to clean up some #ifdef stuff...  Entirely depends on how much you guys want to support non-Linux, IMO.
Comment 18 Javier Villavicencio (RETIRED) gentoo-dev 2009-01-29 07:19:31 UTC
FYI, freebsd-lib-7.1-r1 has strndup(3) now. I have rekeyworded pax-utils-0.1.19.
Comment 19 Fabian Groffen gentoo-dev 2009-01-31 18:00:31 UTC
I added a xstrndup wrapper function, and a conditional implementation for strndup.  It compiles on Darwin, Solaris and Linux for me now.

Please review, bless and release revisions 1.208, 1.4 and 1.3 of scanelf.c, xfuncs.c and xfuncs.h respectively.
Comment 20 Fabian Groffen gentoo-dev 2009-10-25 21:02:22 UTC
patches added to ebuild in prefix.