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.
Created attachment 174190 [details, diff] proposed no-strndup patch
*** Bug 250359 has been marked as a duplicate of this bug. ***
*** Bug 254144 has been marked as a duplicate of this bug. ***
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.
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.
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.
And this is (pretty much) the patch that included strndup on FreeBSD: http://webmail.solomo.de/~flo/strndup.patch
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.
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 :$)
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
Just out[new_len] = '\0'; I think.
(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); }
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.
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
The gnulib one uses strnlen, which would cause the same problem as strndup (unless strnlen is added as well :$).
yes, adding strnlen is fine question now is whether we want to autotoolize the package ...
(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.
FYI, freebsd-lib-7.1-r1 has strndup(3) now. I have rekeyworded pax-utils-0.1.19.
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.
patches added to ebuild in prefix.