wchar_t valgrind issue - 大小为8的读取无效

Jos*_*ose 8 c valgrind wchar-t

我无法弄清楚Valgrind Invalid read of size 8在使用时打印的原因wchar_t.我正在使用valgrind-3.7.0和gcc 4.7.2运行64位Ubuntu(3.5.0-25)系统.

#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    // const wchar_t *text = L"This is a t"; // no Valgrind error
    // const wchar_t *text = L"This is a teeeeeeee"; // no Valgrind error
    const wchar_t *text = L"This is a test"; // Valgrind ERRROR

    wchar_t *new_text = NULL;

    new_text = (wchar_t*) malloc( (wcslen(text) + 1) * sizeof(wchar_t));
    wcsncpy(new_text, text, wcslen(text));
    new_text[wcslen(text)] = L'\0';

    printf("new_text: %ls\n", new_text);

    free(new_text);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译:

$ gcc -g -std=c99 test.c -o test
$ valgrind --tool=memcheck --leak-check=full --track-origins=yes --show-reachable=yes ./test
Run Code Online (Sandbox Code Playgroud)

Valgrind结果:

==19495== Memcheck, a memory error detector
==19495== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==19495== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==19495== Command: ./test
==19495== 
==19495== Invalid read of size 8
==19495==    at 0x4ED45A7: wcslen (wcslen.S:55)
==19495==    by 0x4ED5C0E: wcsrtombs (wcsrtombs.c:74)
==19495==    by 0x4E7D160: vfprintf (vfprintf.c:1630)
==19495==    by 0x4E858D8: printf (printf.c:35)
==19495==    by 0x4006CC: main (test.c:16)
==19495==  Address 0x51f1078 is 56 bytes inside a block of size 60 alloc'd
==19495==    at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19495==    by 0x40066F: main (test.c:12)
==19495== 
new_text: This is a test
==19495== 
==19495== HEAP SUMMARY:
==19495==     in use at exit: 0 bytes in 0 blocks
==19495==   total heap usage: 1 allocs, 1 frees, 60 bytes allocated
==19495== 
==19495== All heap blocks were freed -- no leaks are possible
==19495== 
==19495== For counts of detected and suppressed errors, rerun with: -v
==19495== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
Run Code Online (Sandbox Code Playgroud)

现在,如果我运行相同但使用"工作字符串",那么就说吧

const wchar_t *text = L"This is a t"; // no Valgrind error
// const wchar_t *text = L"This is a teeeeeeee"; // no Valgrind error
// const wchar_t *text = L"This is a test"; // Valgrind ERRROR
Run Code Online (Sandbox Code Playgroud)

我没有问题:

==19571== Memcheck, a memory error detector
==19571== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==19571== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==19571== Command: ./test
==19571== 
new_text: This is a t
==19571== 
==19571== HEAP SUMMARY:
==19571==     in use at exit: 0 bytes in 0 blocks
==19571==   total heap usage: 1 allocs, 1 frees, 48 bytes allocated
==19571== 
==19571== All heap blocks were freed -- no leaks are possible
==19571== 
==19571== For counts of detected and suppressed errors, rerun with: -v
==19571== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
Run Code Online (Sandbox Code Playgroud)

起初我以为字符串大小应该总是8的倍数(可能有些wcs读取8块)但是有些情况失败了,然后我认为我必须为NULL终止符追加8个字节((wcslen(item) + 2) * sizeof(wchar_t)),它工作但是没有没有任何意义,因为sizeof(wchar_t)- 在我的系统中 - 是4个字节,应该足以处理L'\0'终结符.

我也阅读了glibc wcslen源代码,但没什么新东西.我现在正在考虑Valgrind的问题.你们可以在这里投光吗?是否值得为Valgrind提交错误?

谢谢

eca*_*mur 6

这可能是由SSE优化wcslen功能引起的; 请参阅https://bugzilla.redhat.com/show_bug.cgi?id=798968https://bugs.archlinux.org/task/30643.

优化时wcslen,一次读取多个宽字符并使用矢量化指令(SSE)将它们进行比较会更快L'\0'.不幸的是,valgrind认为这是一个未初始化的读取 - 它是,但它是无害的,因为结果wcslen不依赖于未初始化的值.

修复是更新valgrind,希望更新的版本将抑制误报.