C 库中的 Valgrind 错误?

发布于 2024-09-18 21:55:18 字数 1744 浏览 9 评论 0原文

Valgrind 显示大小为 8 的错误的未初始化值。 有时,以下条件跳转会出现未初始化值错误。

我所做的就是使用 gcc 附带的 stdc++ 库打印格式化字符串 和内置的 vsnprintf。

这是一个名为 format 的方法,它是自定义字符串类的一部分。 现在怎么办?一切看起来都是正确的。错误似乎在 _itoa.c 内部。但我在外面能想到的就是不要使用这个功能,这不太可能!

==4229== Memcheck, a memory error detector
==4229== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==4229== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==4229== Command: ./test
==4229== 
==4229== Use of uninitialised value of size 8
==4229==    at 0x54A3DF1: _itoa_word (_itoa.c:196)
==4229==    by 0x54A5138: vfprintf (vfprintf.c:1613)
==4229==    by 0x555C74F: __vsnprintf_chk (vsnprintf_chk.c:65)
==4229==    by 0x407E57: myString::format(char const*, ...) (stdio2.h:79)
==4229==    by 0x419D14: ID::toString() (id.cpp:151)
==4229==    by 0x41D03D: main (test.cpp:126)
==4229== 
==4229== Conditional jump or move depends on uninitialised value(s)
==4229==    at 0x54A3DF8: _itoa_word (_itoa.c:196)
==4229==    by 0x54A5138: vfprintf (vfprintf.c:1613)
==4229==    by 0x555C74F: __vsnprintf_chk (vsnprintf_chk.c:65)
==4229==    by 0x407E57: myString::format(char const*, ...) (stdio2.h:79)
==4229==    by 0x419D14: ID::toString() (uuid.cpp:151)
==4229==    by 0x41D03D: main (test.cpp:126)
==4229== 
==4229== 
==4229== HEAP SUMMARY:
==4229==     in use at exit: 0 bytes in 0 blocks
==4229==   total heap usage: 6 allocs, 6 frees, 1,340 bytes allocated
==4229== 
==4229== All heap blocks were freed -- no leaks are possible
==4229== 
==4229== For counts of detected and suppressed errors, rerun with: -v
==4229== Use --track-origins=yes to see where uninitialised values come from
==4229== ERROR SUMMARY: 3 errors from 2 contexts (suppressed: 4 from 4)

Valgrind is showing an uninitialised value of size 8 error.
And occasionally, the below conditional jump on uninitialised value error.

All I'm doing is printing a formatted string using the stdc++ library that comes with gcc
and the built in vsnprintf.

This is inside a method called format which is part of a custom string class.
What now? everything looks correct. Error seems to be inside _itoa.c. But all I can think of to do on the outside is not use this function, which is not very possible!

==4229== Memcheck, a memory error detector
==4229== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==4229== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==4229== Command: ./test
==4229== 
==4229== Use of uninitialised value of size 8
==4229==    at 0x54A3DF1: _itoa_word (_itoa.c:196)
==4229==    by 0x54A5138: vfprintf (vfprintf.c:1613)
==4229==    by 0x555C74F: __vsnprintf_chk (vsnprintf_chk.c:65)
==4229==    by 0x407E57: myString::format(char const*, ...) (stdio2.h:79)
==4229==    by 0x419D14: ID::toString() (id.cpp:151)
==4229==    by 0x41D03D: main (test.cpp:126)
==4229== 
==4229== Conditional jump or move depends on uninitialised value(s)
==4229==    at 0x54A3DF8: _itoa_word (_itoa.c:196)
==4229==    by 0x54A5138: vfprintf (vfprintf.c:1613)
==4229==    by 0x555C74F: __vsnprintf_chk (vsnprintf_chk.c:65)
==4229==    by 0x407E57: myString::format(char const*, ...) (stdio2.h:79)
==4229==    by 0x419D14: ID::toString() (uuid.cpp:151)
==4229==    by 0x41D03D: main (test.cpp:126)
==4229== 
==4229== 
==4229== HEAP SUMMARY:
==4229==     in use at exit: 0 bytes in 0 blocks
==4229==   total heap usage: 6 allocs, 6 frees, 1,340 bytes allocated
==4229== 
==4229== All heap blocks were freed -- no leaks are possible
==4229== 
==4229== For counts of detected and suppressed errors, rerun with: -v
==4229== Use --track-origins=yes to see where uninitialised values come from
==4229== ERROR SUMMARY: 3 errors from 2 contexts (suppressed: 4 from 4)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别理我 2024-09-25 21:55:18

这是 C 库中实际查看您的数字以便将其格式化为字符串的位置,它表明您正在格式化的数字来自未初始化的存储。

添加 valgrind 选项 --track-origins=yes 以获取有关未初始化值来源的更多详细信息。

因为在未初始化的内存中进行复制是很常见的,例如结构中的填充,所以 valgrind 会跟踪未初始化值的复制,并且不会抱怨,直到实际使用该值的方式可能会影响程序的外部可见行为。这可能会使确定未初始化值的原始来源变得混乱,因为在对其进行任何其他操作之前它可能已被复制了多次。选项--track-origins=yes跟踪附加信息以查明未初始化值的来源,以便在最终使用未初始化值时显示该信息。

This is the place in the C library where it is actually looking at your number in order to format it as a string, and it indicates that the number you are formatting came from uninitialized storage.

Add the valgrind option --track-origins=yes for more details on the origin of the uninitialized value.

Because it is common to copy around uninitialized memory, e.g. padding in structures, valgrind tracks copying of uninitialized values and does not complain until the point where the value is actually used in a way that might affect your program's externally-visible behavior. This can make it confusing to determine the original source of the uninitialized value, since it may have been copied several times before anything else was done with it. The option --track-origins=yes tracks additional information to pinpoint the origin of the uninitialized value, so that this can be displayed in the event that the uninitialized value ends up being used.

饭团 2024-09-25 21:55:18

如果它说它在标准库之一中,则意味着您传递的内容未正确设置。因此,为了进行调试,请转到层次结构中的第一行,即您的代码...因此:ID::toString() (id.cpp:151)。

环顾一下那里返回的内容,您就会找到罪魁祸首。

If it says it is in one of the standard libraries, it means something you are passing in isn't set up properly. So in order to debug, go to the first line in the hierarchy which is your code ... so: ID::toString() (id.cpp:151).

Look around what is being returned there, and you will find your culprit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文