| 454 | |
| 455 | |
| 456 | int sntprintf(LPTSTR aBuf, int aBufSize, LPCTSTR aFormat, ...) |
| 457 | // aBufSize is an int so that any negative values passed in from caller are not lost. |
| 458 | // aBuf will always be terminated here except when aBufSize is <= zero (in which case the caller should |
| 459 | // already have terminated it). If aBufSize is greater than zero but not large enough to hold the |
| 460 | // entire result, as much of the result as possible is copied and the return value is aBufSize - 1. |
| 461 | // Returns the exact number of characters written, not including the zero terminator. A negative |
| 462 | // number is never returned, even if aBufSize is <= zero (which means there isn't even enough space left |
| 463 | // to write a zero terminator), under the assumption that the caller has already terminated the string |
| 464 | // and thus prefers to have 0 rather than -1 returned in such cases. |
| 465 | // MSDN says (about _snprintf(), and testing shows that it applies to _vsnprintf() too): "This function |
| 466 | // does not guarantee NULL termination, so ensure it is followed by sz[size - 1] = 0". |
| 467 | { |
| 468 | // The following should probably never be changed without a full suite of tests to ensure the |
| 469 | // change doesn't cause the finicky _vsnprintf() to break something. |
| 470 | if (aBufSize < 1 || !aBuf || !aFormat) return 0; // It's called from so many places that the extra checks seem warranted. |
| 471 | va_list ap; |
| 472 | va_start(ap, aFormat); |
| 473 | // Must use _vsnprintf() not _snprintf() because of the way va_list is handled: |
| 474 | int result = _vsntprintf(aBuf, aBufSize, aFormat, ap); // "returns the number of characters written, not including the terminating null character, or a negative value if an output error occurs" |
| 475 | aBuf[aBufSize - 1] = '\0'; // Confirmed through testing: Must terminate at this exact spot because _vsnprintf() doesn't always do it. |
| 476 | // Fix for v1.0.34: If result==aBufSize, must reduce result by 1 to return an accurate result to the |
| 477 | // caller. In other words, if the line above turned the last character into a terminator, one less character |
| 478 | // is now present in aBuf. |
| 479 | if (result == aBufSize) |
| 480 | --result; |
| 481 | return result > -1 ? result : aBufSize - 1; // Never return a negative value. See comment under function definition, above. |
| 482 | } |
| 483 | |
| 484 | |
| 485 |
no outgoing calls
no test coverage detected