开发者

c++ _vsnprintf unexpected format string

I have a logging function that takes in a variable number of arguments and uses _vsnprintf to format them. My problem is that when I debug my OCR automation the string it returns is sent to the log, so if the file said something like this:

This bitmap says %n

then it would get sent to my logging function like this:

void log(LPCSTR msg, ...)
{
    char log[MAX_ALLOWED];    
    int length = sizeof(log) / sizeof(log[0]);

    va_list argptr;
    va_start( argptr, pzMsg );

    // our msg accidentally开发者_如何学C has a %
    if ( strchr(msg, '%') ) { 

        // debug assertion - no parameters were passed
        _vsnprintf( log, length, msg, argptr );
    }

    log[length-1] = (char)0;
    va_end( arg_ptr );
}

is there a way, along with the check for '%' that i can check if there were no arguments? thank you.


The traditional way to make sure something can't be expanded by printf is

log("%s", yourString);

Of course, you could also add a variant of log that only takes one argument, or you could count the number of variable arguments and not format the string if there aren't any.


If I understand you correctly, you would like to check the number of arguments actually passed to log().

Unfortunately, this is highly machine-specific. I know of only one architecture which provides an unambiguous argument count. That is the VAX. All the others depend on the caller and the callee to "get it right".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜