vswprintf crashes

vswprintf crashes

本文关键字:crashes vswprintf      更新时间:2023-10-16

使用10月2日发布的Symbian S60第5版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 (+ ) 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 (+ ) 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");

对我来说没有明显的原因(即使在阅读了 vswprintf 手册页一百次之后),我才能弄清楚为什么这段代码在长字符串的 vswprintf 调用中崩溃了:-(完全相同的代码在 Linux 机器上工作正常。为 str 分配了足够的内存,而且 vswprintf 无论如何都会检查缓冲区溢出。不幸的是...S60调试器在此崩溃时不会中断,所以我没有详细信息:-(

有人有什么想法吗?

假设 Symbian 的 vswprintf 例程中存在错误,那么使用 POSIX 兼容代码可能替换哪些函数?(这应该是一个跨平台的库)

谢谢。

对我来说,

这看起来像是进入vswprintf()电话的工作。 即使你只能进行程序集级调试,也应该通过监视进入str[]内存的内容来清楚或多或少发生了什么。

我碰巧在 vswprintf 的实现中找到了一个内部缓冲区,它被硬编码为 128 字节。这很可能导致长字符串发生这样的崩溃。

将 %S 更改为 %s - 大写更改为小写。

在基于 MS 的 printfs 中,%S 表示 unicode 字符,因此这就是 123 个字符字符串失败的原因,它期望每个字符 2 个字节。(注意 %S 不是标准的一部分,所以 Symbian 在这里可能有所不同)

实际上,我认为这仍然适用于Symbian。

您可以尝试将%S格式说明符更改为%ls 。 正如我之前的评论中提到的,它们应该是等效的,但实现中可能存在错误。 请注意,vswprintf函数是在 C99 标准中定义的,并且由于目前还没有任何完全符合 C99 编译器(我相信),因此很可能任何给定的 vswprintf 实现都不完全符合规范,或者它包含错误(前者比后者更有可能)。

你能尝试不调用test()并使用swprintf吗 - 以防错误与VARARGS处理有关?

我现在通过使用 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);
}

(在这种情况下,您实际上必须使用 %s)