我有这个简单的程序:
#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。 我哪里做错了什么?
任何人都可以向我解释一下如何解决一般错误吗?
你也需要移动条件来保护
strcpy
。
int main(void) {
char *str;
str = malloc(20);
if(str)
{
strcpy(str, "hello");
printf("%s\n", str);
}
}