tech-userlevel archive

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

Re: macro directives inside macro arguments undefined



In article <alpine.NEB.2.00.1102241103580.4478%galant.ukfsn.org@localhost>,
Iain Hibbert  <plunky%rya-online.net@localhost> wrote:
>-=-=-=-=-=-
>
>Hi
>
>according to the C99 spec section 6.10.3 clause 11
>
>| If there are sequences of preprocessing tokens within the list of
>| arguments that would otherwise act as preprocessing directives, the
>| behavior is undefined.
>
>this was discussed on the pcc list last year:
>  http://marc.info/?t=128991333400003&r=1&w=2
>
>but I found an problem in our sources because in <ssp/stdio.h> we turn
>sprint() into a macro
>
>| #if __SSP_FORTIFY_LEVEL > 0
>|
>| #define sprintf(str, ...) \
>|    __builtin___sprintf_chk(str, 0, __ssp_bos(str), __VA_ARGS__)
>
>which triggers a problem in ipf/ipmon because of USE_FORT=yes coinciding
>with a sprintf containing preprocessor directives in the argument list:
>
>               sprintf(t,
>#ifdef USE_QUAD_T
>#ifdef PRId64
>                       " Forward: Pkts in %" PRId64 " Bytes in %" PRId64
>                       " Pkts out %" PRId64 " Bytes out %" PRId64
>                       " Backward: Pkts in %" PRId64 " Bytes in %" PRId64
>                       " Pkts out %" PRId64 " Bytes out %" PRId64,
>#else
>                       " Forward: Pkts in %qd Bytes in %qd Pkts out %qd Bytes 
> out %qd
>Backward: Pkts in %qd Bytes in %qd Pkts out %qd Bytes out %qd",
>#endif /* PRId64 */
>#else
>                       " Forward: Pkts in %ld Bytes in %ld Pkts out %ld Bytes 
> out %ld
>Backward: Pkts in %ld Bytes in %ld Pkts out %ld Bytes out %ld",
>#endif
>                       sl->isl_pkts[0], sl->isl_bytes[0],
>                       sl->isl_pkts[1], sl->isl_bytes[1],
>                       sl->isl_pkts[2], sl->isl_bytes[2],
>                       sl->isl_pkts[3], sl->isl_bytes[3]);
>
>The patch attached fixes building this with pcc (for which the behaviour
>is to error out with a syntax error), by moving the directives away from
>the arguments, any objections to commit?

I don't like this patch much. I think it is better to do:

        static const char fmt[] = .....;

        sprintf(str, fmt, ...

christos



Home | Main Index | Thread Index | Old Index