我想修复Valgrind错误

时间:2017-05-13 00:05:17

标签: c valgrind

我没有编译器警告并获得正确的输出。即便如此,valgrind对我的节目抱怨很多。

$ valgrind ./a.out < sample.in 
==8718== Memcheck, a memory error detector
==8718== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==8718== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==8718== Command: ./a.out
==8718== 
==8718== Conditional jump or move depends on uninitialised value(s)
==8718==    at 0x4C30F78: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8718==    by 0x4009F5: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
==8718== Conditional jump or move depends on uninitialised value(s)
==8718==    at 0x4C30F78: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8718==    by 0x400862: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
==8718== Conditional jump or move depends on uninitialised value(s)
==8718==    at 0x4C31068: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8718==    by 0x40088E: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
==8718== Invalid write of size 1
==8718==    at 0x4C3106F: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8718==    by 0x4008EC: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718==  Address 0x5204195 is 0 bytes after a block of size 21 alloc'd
==8718==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8718==    by 0x4008CC: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
==8718== Invalid read of size 1
==8718==    at 0x4C30F74: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8718==    by 0x400964: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718==  Address 0x5204195 is 0 bytes after a block of size 21 alloc'd
==8718==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8718==    by 0x4008CC: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
==8718== Invalid read of size 1
==8718==    at 0x4C31063: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8718==    by 0x40091D: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718==  Address 0x5204195 is 0 bytes after a block of size 21 alloc'd
==8718==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8718==    by 0x4008CC: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
300
707
300
707
300
==8718== Use of uninitialised value of size 8
==8718==    at 0x4EC7C5D: strtok_r (strtok.S:172)
==8718==    by 0x4009A3: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
==8718== Conditional jump or move depends on uninitialised value(s)
==8718==    at 0x4EC7C60: strtok_r (strtok.S:173)
==8718==    by 0x4009A3: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
707
==8718== Conditional jump or move depends on uninitialised value(s)
==8718==    at 0x4EC7BDC: strtok_r (strtok.S:94)
==8718==    by 0x4009A3: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
==8718== Use of uninitialised value of size 8
==8718==    at 0x4EC7C1B: strtok_r (strtok.S:137)
==8718==    by 0x4009A3: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
==8718== Use of uninitialised value of size 8
==8718==    at 0x4EC7C4B: strtok_r (strtok.S:163)
==8718==    by 0x4009A3: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
==8718== Conditional jump or move depends on uninitialised value(s)
==8718==    at 0x4EC7C76: strtok_r (strtok.S:184)
==8718==    by 0x4009A3: solve (in /home/developer/CLionProjects/carrots/a.out)
==8718==    by 0x400A3F: main (in /home/developer/CLionProjects/carrots/a.out)
==8718== 
==8718== 
==8718== HEAP SUMMARY:
==8718==     in use at exit: 0 bytes in 0 blocks
==8718==   total heap usage: 9 allocs, 9 frees, 5,480 bytes allocated
==8718== 
==8718== All heap blocks were freed -- no leaks are possible
==8718== 
==8718== For counts of detected and suppressed errors, rerun with: -v
==8718== Use --track-origins=yes to see where uninitialised values come from
==8718== ERROR SUMMARY: 396 errors from 12 contexts (suppressed: 0 from 0)

Valgrind输出

var a = location.href; 
var userNameVar = a.substring(a.indexOf('?username=')+1);

如何让它没有错误?

1 个答案:

答案 0 :(得分:1)

int i1 = 0;
…
str[0] = 'c';
while (EOF != (c = fgetc(input))) {
    str[i1++] = c;
}

此循环从不将\0终止符写入str,因此后续对字符串函数的调用(如strlen()strcpy())将在字符串的末尾运行,触发valgrind错误。

解决这个问题应该解决大部分(如果不是全部)错误。