如何修复 valgrind“条件跳跃或移动取决于未初始化的值”错误?

How do I fix valgrind "conditional jump or move depends on uninitialized value(s)" error?

提问人:Proth 提问时间:8/22/2023 最后编辑:chqrlieProth 更新时间:8/22/2023 访问量:67

问:

我有这个简单的程序:

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

int main(void) {
    char *str;
    str = NULL;
    str = malloc(sizeof(char) * 20);
    strcpy(str, "hello");
    if (str != NULL) printf("%s\n", str);
}

并在此上运行 valgrind,给我带来了 .printf

==720== Memcheck, a memory error detector
==720== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==720== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==720== Command: ./p1.o --track-origins=yes --leak-check=full --show-leak-kinds=all --tool=memcheck
==720==
==720== Syscall param set_robust_list(head) points to uninitialised byte(s)
==720==    at 0x44B85A: __tls_init_tp (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4041F9: __libc_setup_tls (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x403355: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==  Address 0x40006b0 is in the brk data segment 0x4000000-0x4000dbf
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x45DEFE: getrandom (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x416C45: ptmalloc_init.part.0 (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x419A34: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x47B33A: _dl_get_origin (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44CC96: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x419841: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x47B33A: _dl_get_origin (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44CC96: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x419931: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x47B33A: _dl_get_origin (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44CC96: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x4185D1: _int_malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4190E4: tcache_init.part.0 (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x41993B: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x47B33A: _dl_get_origin (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44CC96: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x41985E: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x47B33A: _dl_get_origin (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44CC96: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x41985E: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x477E20: _dl_init_paths (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44D3C3: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x41985E: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x477E3D: _dl_init_paths (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44D3C3: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x41985E: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4780F9: _dl_init_paths (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44D3C3: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x43EF50: __strcspn_sse42 (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4861ED: strsep (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x477A0E: fillin_rpath.isra.0 (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x478124: _dl_init_paths (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44D3C3: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x43EF2A: __strcspn_sse42 (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4861ED: strsep (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x477A0E: fillin_rpath.isra.0 (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x478124: _dl_init_paths (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44D3C3: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x41985E: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x41BD1E: strdup (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x477A31: fillin_rpath.isra.0 (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x478124: _dl_init_paths (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44D3C3: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x41985E: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x477B44: fillin_rpath.isra.0 (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x478124: _dl_init_paths (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44D3C3: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x41985E: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x488D2E: _dl_find_object_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44D617: _dl_non_dynamic_init (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44E669: __libc_init_first (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4033F9: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x41ACA6: calloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44A4B2: monstartup (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4016B0: __gmon_start__ (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x401015: ??? (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4: ???
==720==    by 0x40345A: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x41AD58: calloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44A4B2: monstartup (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4016B0: __gmon_start__ (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x401015: ??? (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4: ???
==720==    by 0x40345A: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x41985E: malloc (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x46A7D4: _IO_file_doallocate (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x412230: _IO_doallocbuf (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x41136F: _IO_file_overflow (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x40FE46: _IO_file_xsputn (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x40C2AC: puts (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x40180F: main (p1.c:10)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x4473AE: write (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x40F68C: _IO_file_write (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4107FF: _IO_do_write (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x4112D2: _IO_file_overflow (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x40C341: puts (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x40180F: main (p1.c:10)
==720==
hello
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x46D5C1: pthread_mutex_lock (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44C736: dl_iterate_phdr (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x449E76: write_gmon (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44A67E: _mcleanup (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x40A9C8: __run_exit_handlers (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x40AB2F: exit (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x401C50: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720== Conditional jump or move depends on uninitialised value(s)
==720==    at 0x46DD52: pthread_mutex_unlock (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44C7FF: dl_iterate_phdr (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x449E76: write_gmon (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x44A67E: _mcleanup (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x40A9C8: __run_exit_handlers (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x40AB2F: exit (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==    by 0x401C50: (below main) (in /mnt/c/Users/guido/Desktop/API 2023/p1.o)
==720==
==720==
==720== HEAP SUMMARY:
==720==     in use at exit: 0 bytes in 0 blocks
==720==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==720==
==720== All heap blocks were freed -- no leaks are possible
==720==
==720== Use --track-origins=yes to see where uninitialised values come from
==720== For lists of detected and suppressed errors, rerun with: -s
==720== ERROR SUMMARY: 20 errors from 20 contexts (suppressed: 0 from 0)

我知道错误告诉我我使用了一个未初始化的变量,但我将 main 开头的 str 变量初始化为 NULL。 我在哪里做错了什么?

谁能向我解释一下如何解决一般错误?

c 马洛克 ·瓦尔格林德

评论

0赞 nielsen 8/22/2023
似乎错误来自标准库实现。
0赞 Proth 8/22/2023
你是什么意思?
0赞 nielsen 8/22/2023
似乎(所有)这些错误都与您的代码无关。如果你放弃并只是这样做有什么区别吗?- 顺便说一句,如果成功,你应该打电话。ifprintf("%s\n", str);freemalloc
0赞 Proth 8/22/2023
如果我删除 if,它不会改变错误。所以我不知道该怎么办?
1赞 Lundin 8/22/2023
这里没有任何东西是未初始化的(或者嗯,未分配的),所以这显然是一个误报。

答:

0赞 0___________ 8/22/2023 #1

您也需要移动条件以保护。strcpy

int main(void) {
    char *str;
    str = malloc(20);
    if(str)
    {
        strcpy(str, "hello");
        printf("%s\n", str);
    }
}

评论

0赞 Proth 8/22/2023
我也这样做了,同样的错误仍然存在
0赞 0___________ 8/22/2023
@Proth 所以你正在valgriding不同的程序。或者你的问题不是真的。
0赞 Proth 8/22/2023
不,我确信这是正确的程序。我编译了更新的程序并在可执行文件上使用 valgrind