tags:

views:

185

answers:

2

I have used this type of convention many times in my code in the past:

strcpy ( cTmpA, "hello" );
sprintf ( cTmpA, "%s world", cTmpA );

Recently I switched my legacy C compiler to Visual Studio 2005, and found I got a garbled string resulting from the above code. It then occurred to me that perhaps the behaviour of sprintf() is not rigidly defined where one of the inputs matches the output string.

Is the above code valid K&R C? If not, how do I find all the places in my code where this type of call to sprintf() has occurred?

+4  A: 

Most implementations of sprintf() don't copy the format string and instead use a pointer inside the string you passed. If format and output point to the same memory, that will lead to odd results.

And you should really use snprintf() which protects you against buffer overflows.

To find all calls, put #define sprintf +++ into a common header find and recompile all sources. That should give you a list of errors along with the file name and line numbers :) Or use recursive search'n'replace of your IDE.

If you want to trim this list down to the ones where you use the same pointer for both arguments, use this macro:

#define sprintf(output,format,...) check_sprintf(__FILE__,__LINE__,output,format,....)

Note that not all compilers support macros with varargs. Then define a new function check_sprintf:

int check_sprintf (char*filename,int line,char*output,char*format,...) {
    va_list args;

    if(output==format) {
        fprintf(stderr,
             "Output and format are the same at %s:%d", filename, line);
             abort();
    }

    va_start (args, format);
    len = vsprintf (output, format, args);
    va_end (args);

    return len;   
}

[EDIT] I just saw that you're talking about the output and the first argument. You can reuse the code from above and call va_arg() to get the first argument and use that in the compare.

Aaron Digulla
Good idea on using macro substitution to spot where this occurs. I thought of this but couldn't think of a way to craft a macro that resulted in compile-time errors only when the output is also one of the inputs. The problem with redefining sprintf() is that this will only show up at run-time, whereas my project is sufficiently large enough that I would never find every code-path without some type of automated unit testing.
Piers
Over time, you'll find all places because the program crashes hard (instead of silently produce buggy data). So start with your unit tests right now and fix as many of the places as you can and then fix the rest as the bug reports come in.
Aaron Digulla
+5  A: 

While it is valid K&R C, you probably want to rather know if it's valid POSIX - see sprintf Specification. We read:

If copying takes place between objects that overlap as a result of a call to sprintf() or snprintf(), the results are undefined.

Kornel Kisielewicz