Snprintf for Windows CE

My program runs on several platforms. Among them, Windows CE. Currently, sprintf is widely used, which leads to many buffer overflow problems, etc. Instead, I want to replace them with snprintf calls. For Visual Studio, I found this question that solves part of the win32 problem:

snprintf and Visual Studio 2010

but I can't get it to work for Windows CE, because the functions _vsnprintf_s and _vscprintf , and the constant _TUNCATE not available. Does anyone know a way to replicate snprintf (linux) behavior in Windows CE?

+5
source share
3 answers

So far, I got the following function:

 int my_snprintf(char* str, size_t size, const char* format, ...) { int len = 0; va_list ap; if (size == 0) { return 0; } va_start(ap, format); len = _vsnprintf(str, size, format, ap); va_end(ap); if (len < 0 || len >= size) { len = size - 1; } if (size > 0) { str[size - 1] = '\0'; } return len; } 

Preliminary testing looks fine, and it even compiles for WinCE. Any feedback is greatly appreciated

+1
source

I think you can use the printf_s special function for Microsoft. I am not sure that it is 100% compatible, and I think that it does not allow the buffer to be empty when the size is 0, but this may be enough for your requirements and ... I do not have VS2010 to confirm. printf_s is present in the Windows CE release.

0
source

This is _snprintf under Windows CE:

http://msdn.microsoft.com/en-us/library/ms861145.aspx

but its behavior is slightly different from the standard, read here: Is snprintf () ALWAYS terminating a zero point?

0
source

Source: https://habr.com/ru/post/1203854/


All Articles