5

My program runs on several platforms. Amongst them, Windows CE. Currently, sprintf is extensively used, which results in a lot of problems with buffer-overflows etc. I want to replace these with calls to snprintf instead. For Visual Studio, I found this question, which solves the win32 part of the problem:

snprintf and Visual Studio 2010

but I can't get it working for Windows CE, since the functions _vsnprintf_s and _vscprintf, and the constant _TUNCATE aren't available. Does anybody know of a way to replicate snprintf (linux) behaviour on Windows CE?

Community
  • 1
  • 1
pjaall
  • 379
  • 5
  • 14
  • Check whether this response can give you the useful info http://stackoverflow.com/questions/23882340/windows-ce-5-0-vs-windows-mobile-6/23883768#23883768 – daniele3004 Oct 02 '14 at 09:33

3 Answers3

1

So far I've ended up on 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 OK, and it even compiles for WinCE.. Any feedback is greatly appreciated

pjaall
  • 379
  • 5
  • 14
  • 1
    Looks good so far. Just to have it mentioned: You're aware, that `snprintf` doesn't fix buggy code? You need to re-write either way, you have to test the return value of `snprintf` to see if enough space has been available and if not, reallocate the buffer (or whatever is appropriate in your case). – mafso Oct 02 '14 at 12:51
  • yes I'm aware -- that will be the next step in debugging the code.. The main purpose now is to avoid some mysterious crashes caused by overflows – pjaall Oct 02 '14 at 13:03
  • `The main purpose now is to avoid some mysterious crashes caused by overflows` - I dont think you will fix your bugs doing this, it will only hide real root problem. snprintf will protect against buffer overrun, but if your code logic requires bigger data and buffer is too small then it will still not work properly. – marcinj Oct 03 '14 at 08:33
  • Yes, and that's OK. Those kinds of bugs are way easier to track than errors caused by buffer overwrites - which may pop up in more or less random places.. – pjaall Oct 03 '14 at 10:49
  • Note: the second test of `if (size > 0)` is not needed. – chux - Reinstate Monica Nov 05 '14 at 18:11
0

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

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

It's _snprintf under Windows CE:

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

but its behaviour is slightly different from standard, read here: Is snprintf() ALWAYS null terminating?

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • yes, and that was the starting point of these compatibility issues -- I should have mentioned that in the question.. :) – pjaall Oct 03 '14 at 10:50