You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in case of truncation it returns the total number of characters (not including the terminating null-byte) which would have been written, if the limit was not imposed. Negative value if an error occurred
on VS2013 & VS2015
return the number of characters written if the number of characters to write is less than or equal to count; if the number of characters to write is greater than count, these functions return -1 indicating that output has been truncated. The return value does not include the terminating null, if one is written.
The vsnprintf function always writes a null terminator...
vsnprintf_s is ANSI C++11
on cppreference
returns number of characters not including the terminating null character (which is always written as long as buffer is not a null pointer and bufsz is not zero and not greater than RSIZE_MAX), which would have been written to buffer if bufsz was ignored, or a negative value if a runtime constraints violation or an encoding error occurred
_vsnprintf_s is MSVC specific maybe be C++11
on VS2013 & VS2015
return the number of characters written, not including the terminating null,... unless count is _TRUNCATE, in which case as much of the string as will fit in buffer is written and -1 returned
writes up to count characters of the given data to the memory pointed to by buffer and appends a terminating null.
Summary
Return value: On MSVC number of characters written or -1 in case of truncation or error
Return value: On cppreference returns the total number of characters which would have been written or -1 in case of error
vsnprintf works as expected on cppreference and VS2015... always writes a null terminator. On VS2013 the buffer will be null-terminated If there is room at the end
My proposl
Use vsnprintf witch is ANSI C++99. On MSVC check for -1 and ensure null terminating on VS2013
staticboolcv_snprintf(char* buf, int len, constchar* fmt, ...)
{
va_list va;
va_start(va, fmt);
int res = vsnprintf((char *)buf, len, fmt, va);
va_end(va);
#if defined _MSC_VER
// maybe truncation maybe error if(res < 0)
check for last errno
res = len -1;
// ensure null terminating on VS2013
#if def_MSC_VER<=1800
buf[res] = 0;
#endif
#endifreturn res >= 0 && res < len;
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.