snprintf non-portable usage bug

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

 



Sorry, i'm also assuming that pj_strbuf() would be rewritten as:

    return (str->ptr ? str->ptr : "");

-Ted Merrill

On Tuesday 12 January 2010 03:43:04 pm Ted Merrill wrote:
> There are quite a few places in the pjsip code where it is assumed that an
> snprintf of "%.*s" will nicely print nothing if the supplied arguments are
> 0 and NULL.
> For example, in pjsua_acc.c there is:
>         len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
>                               
> "<sip:%.*s%s%s%.*s%s:%d;transport=%s%.*s>%.*s", (int)acc->user_part.slen,
>                                acc->user_part.ptr,
>                                (acc->user_part.slen? "@" : ""),
>                                beginquote,
>                                (int)via_addr->slen,
>                                via_addr->ptr,
>                                endquote,
>                                rport,
>                                tp->type_name,
>                                (int)acc->cfg.contact_uri_params.slen,
>                                acc->cfg.contact_uri_params.ptr,
>                                (int)acc->cfg.contact_params.slen,
>                                acc->cfg.contact_params.ptr);
> ... and by default acc->cfg.contact_uri_params and acc->cfg.contact_params
> both have 0 lengths and NULL pointers.
> This is very convenient when it works but sadly is not a feature of C
> libraries that one can count on, which makes the code less than portable.
> In my case, i'm using a version of uclibc that prints "(null)" whenever the
> pointer is NULL, regardless of the "precision" argument.
> Modifying uclibc is a possible work around for me, although it means i have
> to fork a version of uclibc which will make it hard to track their latest
> version... which won't make people i work with happy.
>
> I suggest that all such cases in the pj code be slightly rewritten so that
> "" is substituted where "ptr" is NULL.
> This can best be done with a wrapper inline function which we already have
> in pj/string.h !!! :
> PJ_INLINE(const char*) pj_strbuf( const pj_str_t *str )
> {
>     return str->ptr;
> }
> used as:
> pj_strbuf(&acc->cfg.contact_params)
>
> To generalize, it would be better if the fields of pj_str_t were accessed
> only by standard access functions pj_strbuf() and pj_strlen() already
> provided in pj/string.h , and never directly as is the case in so much of
> the pj code... however, fixing the snprintfs would be a good start.
>
> Thanks a lot for pjsip,
> -Ted Merrill




[Index of Archives]     [Asterisk Users]     [Asterisk App Development]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [Linux API]
  Powered by Linux