Valgrind在调用wcustoms时报告未初始化的值

Valgrind reports uninitialized value when calling wcstombs

本文关键字:初始化 报告 调用 wcustoms Valgrind      更新时间:2023-10-16

我无意中发现了一个我无法自己修复的Valgrind报告。我有一个函数从文件中读取Microsoft"unicode"字符串(一系列以大小为前缀的双字节对齐的wchar_t)。示例文件可能如下所示:

0004 0041 0041 0041 0041                 ..A.A.A.A.

下面的代码示例从文件中读取"unicode"字符串,并使用wcstombs从中生成一个std::字符串。

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <boost/shared_array.hpp>
#include <boost/cstdint.hpp>
std::string read_unicode_string(FILE* f)
{
    std::wstring s = std::wstring();
    wchar_t c;
    boost::uint16_t size;
    if (2 !=fread(&size, 1, 2, f)) {
        return "";
    }
    // Microsoft's "unicode" strings are word aligned.
    for (unsigned int i = 0 ; i < size ; ++i)
    {
        if (2 != fread(&c, 1, 2, f)) {
            break;
        }
        s += c;
    }
    s += L'';
    // Convert the wstring into a string
    boost::shared_array<char> conv = boost::shared_array<char>(new char[s.size() + 1]);
    memset(conv.get(), 0, sizeof(char) * (s.size() + 1));
    wcstombs(conv.get(), s.c_str(), s.size());
    return std::string(conv.get());
}
int main(int argc, char** argv)
{
    FILE* f = fopen("test", "rb");
    if (f == NULL) {
        return 1;
    }
    std::cout << read_unicode_string(f) << std::endl;
    fclose(f);
    return 0;
}

虽然它似乎工作,valgrind报告说,一些跳转在wcustoms依赖于一个初始值:

==8440== Conditional jump or move depends on uninitialised value(s)
==8440==    at 0x56606C2: wcsnlen (wcsnlen.c:40)
==8440==    by 0x565FCF0: wcsrtombs (wcsrtombs.c:110)
==8440==    by 0x56101A0: wcstombs (wcstombs.c:35)
==8440==    by 0x401488: read_unicode_string(_IO_FILE*) (test.cpp:32)
==8440==    by 0x40157C: main (test.cpp:42)
==8440== 
==8440== Conditional jump or move depends on uninitialised value(s)
==8440==    at 0x55F2D5B: __gconv_transform_internal_ascii (loop.c:332)
==8440==    by 0x565FD41: wcsrtombs (wcsrtombs.c:116)
==8440==    by 0x56101A0: wcstombs (wcstombs.c:35)
==8440==    by 0x401488: read_unicode_string(_IO_FILE*) (test.cpp:32)
==8440==    by 0x40157C: main (test.cpp:42)

我一直在寻找,但我觉得我已经正确初始化了每个变量。有人看到我代码中的问题了吗?

提前感谢您的帮助!

这是一个严重的错误!如果sizeof(wchar_t)大于2(假设为4),宽字符串's'将获得(2)个未初始化的字节,这些字节在wcstombs中报告为未初始化的值(s)。

问题不在您的代码中。调用栈显示未初始化的变量在wcstombs实现中的某个地方使用-您所能做的就是尝试告诉valgrind不要检查该库或从valgrind的输出中过滤这两条消息。