This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

vsprintf Produces Garbage



I'm using C2000 compiler 6.0.1 with CCSv4.   I've written a printf-like function to help me print formatted debugging data to a serial port.  I'm making use of the vsprintf() function but it seems to be broken whenever I use it with a non-empty argument list.  The function code is as follows:

unsigned int Uart::send(const char* data, ...)
{
 unsigned int count;
 va_list args;
 int stringLimit = MAX_TX_SIZE;
 
 // Truncate format string
 strncpy(formattedString, data, stringLimit);
  
 // Execute formatting
 va_start(args, data);
 count = vsprintf(formattedString, formattedString, args);
 va_end(args);
 
 return send(formattedString, count);
} // end send

I've ensured that my heap and stack are both allocated and each is 4KB in size.  If I make a call such as:  myUart->send("Hello world");  the function works fine.  If, however, I call it as myUart->send("Value is %d", someInt); the program will crash.  Using the debugger, I've determined that this is because after the system returns from vsprintf, the formattedString variable points to a location in Flash which contains what is certainly not a null-terminated string.  I have also ensured that my build settings are such that full printf support is included in my executable.

Does anyone know what might be going wrong?  Also if you know of some vsnprintf() implementation that TI provides, that would be great :)

 

Thanks!

 

  • I may have answered my own question.  From a Linux man page:

    C99 and POSIX.1-2001 specify that the results are undefined if  a  call
           to  sprintf(),  snprintf(),  vsprintf(),  or  vsnprintf()  would  cause
           copying to take place between objects that overlap (e.g., if the target
           string  array and one of the supplied input arguments refer to the same
           buffer). 

  • SJackson said:

    Also if you know of some vsnprintf() implementation that TI provides, that would be great :)

    The C2000 library has provided vsnprintf since before version 4.3.0; is it not working for you for some reason?