简体   繁体   中英

vswprintf crashes

using the Symbian S60 5th edition SDK released on October 2nd, I am compiling/running(on sim) the following code snippet:

void test(wchar_t *dest, int size, const wchar_t *fmt, ...) {
    va_list vl;
    va_start(vl, fmt);
    vswprintf(dest, size, fmt, vl);
    va_end(vl);
}

...

wchar_t str[1024];

// this crashes (2nd string 123 characters (+ \0) equals 248 bytes)
test(str, 1024, L"msg: %S", L"this is a test messagethis is a test messagethis is a test messagethis is a test messagethis is a test messagethis is a tes");

// this works (2nd string 122 characters (+ \0) equals 246 bytes)
test(str, 1024, L"msg: %S", L"this is a test messagethis is a test messagethis is a test messagethis is a test messagethis is a test messagethis is a te");

For no reason obvious to me (even after having read the vswprintf man page a hundred times) can I figure out why this code is crashing on me in the vswprintf call for long strings :-( The exact same code works fine on a Linux box. There is sufficient memory allocated for str, plus vswprintf is checking for buffer overruns anyway. Unfortunately the ... S60 debugger does not break on this crash, so I have no details :-(

Does anybody have any ideas?

Assuming a bug in Symbian's vswprintf routine, what would be possible replacement functions using POSIX compliant code? (this is supposed to be a cross-platform library)

Thanks.

To me this looks like a job for stepping into the vswprintf() call. Even if you can only do assembly-level debugging, it should be clear what's more or less going on by keeping a watch on what's going into the the str[] memory.

I happened to find an internal buffer inside vswprintf's implementation to be hard-coded to 128 bytes. This could very well cause such a crash on long strings.

Change the %S to a %s - uppercase to lowercase.

In MS-based printfs, %S means unicode characters, so this is why the 123 character string fails, it expects 2 bytes per character. (note %S is not part of the standard, so Symbian may be different here)

Actually, I think that still applies to Symbian .

You could try changing the %S format specifier to %ls . As mentioned in my earlier comment, they're supposed to be equivalent, but there could be a bug in the implementation. Note that the vswprintf function is defined in the C99 standard, and since there are not yet any fully conforming C99 compilers (I believe), it's very possible that any given implementation of vswprintf does not fully conform to the spec, or that it contains bugs (the former is more likely than the latter).

如果该错误与VARARGS处理有关,您可以尝试不调用test()而是使用swprintf吗?

I've now "solved" this problem by using Symbian functions to perform this task:

void test(wchar_t *dest, int size, const wchar_t *fmt, ...) {
    VA_LIST args;
    VA_START(args, fmt);

    TPtrC16 fmtPtr((const TUint16*)fmt, wcslen(fmt) + 1);  
    TPtr16  targetPtr((TUint16*)dest, size);

    targetPtr.FormatList(fmtPtr, args);
    targetPtr.ZeroTerminate();

    VA_END(args);
}

(in which case you actually have to use %s )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM