[OPEN-ILS-DEV] non-portable code: strndup() in osrf-chat.c

Scott McKellar mck9 at swbell.net
Wed Dec 26 21:37:51 EST 2007


Did anything ever happen with this?

I haven't looked at any magical autotools, but when I look at the
source code, it looks like we call strndup() only in a single
file, namely osrf_chat.c.

We define this function in strndup.c and prototype it in strndup.h.
However we don't #include <strndup.h> anywhere (unless it is
inserted magically somehow in a way that isn't obvious to me).

Since it doesn't see a declaration or prototype for strndup(), my
compiler assumes that it returns an int.  However strndup() actually
returns a pointer.  The compiler therefore converts what it thinks is
an int to a pointer, without an explicit cast, and issues a 
diagnostic.

The resulting mismatch may have unpleasant consequences if ints and 
pointers are returned in different registers, or they have different
sizes, or are otherwise incompatible.

Rather than rely on magical autotools, it might be simpler and more
robust to define our own osrf_strndup() somewhere.  The most logical
locations would be either utils.c or osrf_chat.c.

Other possibilities:

1. #include strndup.h within osrf_chat.c;

2. Hard-code a prototype for strndup() within osrf_chat.c;

3. Rewrite osrf_chat.c so that it doesn't need strndup() by any name.

Scott McKellar
http://home.swbell.net/mck9/ct/

--- Mike Rylander <mrylander at gmail.com> wrote:

> On 4/30/07, Jeroen Ruigrok van der Werven <asmodai at in-nomine.org>
> wrote:
> > -On [20070430 21:38], Jeroen Ruigrok van der Werven
> (asmodai at in-nomine.org) wrote:
> > >[...] meaning you have to provide your own implementation anyway.
> >
> > I found a MIT licensed implementation of strndup(). Now all I need
> is an
> > accompanying strnlen() to make it without too much additional
> hackery.
> 
> Just to keep the list up to date, we're working through this in IRC.
> The plan is looking like some autotools magic to detect strndup (and
> strnlen) and if not found to include a local implementation.  Those
> will probably be inserted via a ports.[ch] set that will be included
> by utils.h (that's used basically everywhere).
> 
> The plan may change ... that is all  :)
> 
> Thanks, Jeroen!
> 
> --miker
> 
> >
> > --
> > Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> /
> asmodai
> > ¥¤¥§¥ë¡¼¥ó ¥é¥¦¥Õ¥í¥Ã¥¯ ¥ô¥¡¥ó ¥Ç¥ë ¥¦¥§¥ë¥ô¥§¥ó
> > http://www.in-nomine.org/ | http://www.rangaku.org/
> > And your ways appear a total lack of Faith...
> >
> 
> 
> -- 
> Mike Rylander
> 



More information about the Open-ils-dev mailing list