[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: strndupa in gnulib?

From: Bruno Haible
Subject: Re: strndupa in gnulib?
Date: Sat, 27 Mar 2010 15:42:31 +0100
User-agent: KMail/1.9.9


Jay Hesselberth asked:
> I'm trying to port a package developed in Linux to Mac OS X; the
> package makes use of strndupa in from Linux string.h, but this macro
> is missing in OS X string.h.  Is there a reason this macro wasn't
> included in gnulib (or am I just missing it)?

The Linux manual page lists some of the reasons:

  $ man strndupa
         strdupa() and strndupa() are similar, but use alloca(3) to allocate the
         buffer.  They are only available when using the GNU GCC suite, and suf‐
         fer from the same limitations described in alloca(3).

The problems with strndupa are the following:

  1) [Inherited from alloca.] Not all C compilers have alloca. The last one I
     know of that had this problem was 'cc' on HP-UX 10.01. But some C++
     compilers also don't have alloca (although they have variable-length
     arrays). For this reason, gnulib offers a module 'alloca-opt' that also
     provides a macro HAVE_ALLOCA that informs you whether alloca exists or

  2) [Inherited from alloca.] In multithreaded environments, often the stack
     size can be as small as 16 KB or 64 KB. Blindly allocating a string of
     1 MB on the stack causes a crash.

  3) [Inherited from strndup.] You are willfully cutting the string, possibly
     in the middle of a (multi-byte) character.

Reasonable software
  1) uses alloca only when it is available,
  2) uses alloca only for sizes <= 4 KB,
  3) considers all parts of a string and avoids arbitrary truncation.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]