On May 22, 2008 at 09:30, Alfred E. Heggestad <[EMAIL PROTECTED]> wrote:
> Andrei Pelinescu-Onciul wrote:
> >andrei      2008/05/21 23:31:19 CEST
> >
> >  SER CVS Repository
> >
> >  Modified files:
> >    .                    ut.h 
> >  Log:
> >  - added ushort2sbuf() and ushort2str(): they convert an unsigned short 
> >  into
> >   its ascii representation (base 10). ushort2str() returns a pointer to a 
> >   null
> >    terminated static string, while ushor2sbuf() fills the supplied buffer.
> >  
> 
> Andrei,
> 
> 
> the code that was added declares the buffer static (i.e.
> not on the call stack):
> 
> 
>        static inline char* ushort2str(unsigned short u)
>        {
>                static char buf[USHORT2STR_MAX_LEN];
>                int len;
>       
>                len=ushort2sbuf(u, buf, sizeof(buf)-1);
>                buf[len]=0;
>                return buf;
>        }
> 
> 
> this means that the function is not re-entrant, and will not
> work in multi-thread/proc environments..

Yes, it's not thread safe, like most of the other string printing function
 (int2str(), ip_addr2a() a.s.o). All the functions ending in 2a or 2str
 will return a pointer to a static buffer and are not thread safe (but
 ok in multi-process).
 They will also not work if called twice in a row without saving the
 result (e.g. DBG("port1=%s port2=%s\n", ushortstr(1), ushortstr(2))
 will print either "port1=2 port2=2" or "port1=1  port2=1".
However they are more convenient for use in LOG() or DBG().

If you want thread safe, try the 2sbuf version: len=ushort2sbuf(u, buf,
buf_len); buf[len]=0 (which will write the result in buf).

> 
> I suggest changing "static char buf" to "char buf"..

That won't work (we can't return it).


Andrei
_______________________________________________
Serdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/serdev

Reply via email to