简体   繁体   English

vswprintf崩溃

[英]vswprintf crashes

using the Symbian S60 5th edition SDK released on October 2nd, I am compiling/running(on sim) the following code snippet: 使用10月2日发布的Symbian S60第五版SDK,我正在编译/运行(在sim上)以下代码段:

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 :-( 毫无理由(即使阅读了一百遍vswprintf手册页之后),我是否还能弄清楚为什么这段代码在对长字符串的vswprintf调用中崩溃了:-(完全相同的代码在Linux机器上可以正常工作。为str分配了足够的内存,再加上vswprintf仍然在检查缓冲区溢出。不幸的是... S60调试器在此崩溃中没有中断,所以我没有详细信息:-(

Does anybody have any ideas? 有人有什么想法吗?

Assuming a bug in Symbian's vswprintf routine, what would be possible replacement functions using POSIX compliant code? 假设Symbian的vswprintf例程中存在错误,那么使用POSIX兼容代​​码可以替代哪些功能? (this is supposed to be a cross-platform library) (这应该是一个跨平台的库)

Thanks. 谢谢。

To me this looks like a job for stepping into the vswprintf() call. 对我来说,这似乎是进入vswprintf()调用的工作。 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. 即使您只能进行程序集级调试,也应通过监视str[]内存中发生的事情来弄清到底发生了什么。

I happened to find an internal buffer inside vswprintf's implementation to be hard-coded to 128 bytes. 我偶然发现在vswprintf的实现中的内部缓冲区被硬编码为128个字节。 This could very well cause such a crash on long strings. 这很可能导致长字符串崩溃。

Change the %S to a %s - uppercase to lowercase. 将%S更改为%s-将大写字母更改为小写字母。

In MS-based printfs, %S means unicode characters, so this is why the 123 character string fails, it expects 2 bytes per character. 在基于MS的printfs中,%S表示Unicode字符,因此这就是123字符串失败的原因,它期望每个字符2个字节。 (note %S is not part of the standard, so Symbian may be different here) (请注意,%S不是标准的一部分,因此Symbian在这里可能有所不同)

Actually, I think that still applies to Symbian . 实际上,我认为这仍然适用于Symbian

You could try changing the %S format specifier to %ls . 您可以尝试将%S格式说明符更改为%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). 请注意, vswprintf函数是在C99标准中定义的,并且由于还没有完全符合标准的C99编译器(我相信),因此任何给定的vswprintf实现都很可能不完全符合规范,或者它包含错误(前者比后者更有可能)。

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

I've now "solved" this problem by using Symbian functions to perform this task: 我现在通过使用Symbian函数执行此任务来“解决”此问题:

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 ) (在这种情况下,您实际上必须使用%s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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