我从我的c代码调用一个bash脚本,让我们生成一个文本文件并返回它的链接。这就是为什么我使用popen而不是系统,因为我需要它的输出流 所以我解析链接并希望将它存储在C中的字符串中。这就是我的工作方式:
#define LINK_KEY "FILE LINK:"
-------------------
char *the_link;
int is_set = 0;
FILE *call_script;
call_script = popen("/location/of/script/script.sh", "r");
if (call_script == NULL) {
fprintf(stderr, "Could not script. Aborting...\n");
exit(EXIT_FAILURE);
}
char *line;
int line_len = 128;
line = malloc(sizeof(char)*line_len);
while (fgets(line, line_len, call_script) != NULL) {
int line_len = strlen(line);
if (line[line_len-1] == '\n') {
if (strstr(line, LINK_KEY)) {
int j;
for (j = 0; j < strlen(line); j++) {
if (line[j] == ':') {
is_set = 1;
break;
}
}
if (is_set) {
int link_len = line_len - j - 1;
if (link_len <= 0) {
fprintf(stderr, "Error in finding file. Aborting...\n");
exit(EXIT_FAILURE);
}
the_link = malloc(sizeof(char) * (link_len +1)); // <=== here is where valgrind complains
strncpy(the_link, &line[j + 1], link_len - 1);
the_link[link_len] = '\0';
}
}
} else {
line_len*=2;
line = realloc(line, sizeof(char)*line_len;
}
}
if (!is_set) //.... throw error and abort
我不知道为什么Valgrind抱怨变量没有初始化:
==7196== Uninitialised value was created by a heap allocation
==7196== at 0x4A0887C: malloc (vg_replace_malloc.c:270)
==7196== by 0x4018A7: main (jumping_al.c:172)
答案 0 :(得分:3)
此代码:
the_link = malloc(sizeof(char) * (link_len +1)); // <=== here is where valgrind complains
strncpy(the_link, &line[j + 1], link_len - 1);
the_link[link_len] = '\0';
您创建一个大小为link_len + 1
的缓冲区,然后将link_len - 1
个字符复制到其中(将the_link[0]
写入the_link[line_len-2]
,然后编写NULL
终止符到数组中的最后一个字节。这不会写入the_link[link_len-1]
。要么修复strncpy
调用,要么在该位置写一些其他已知数据。
答案 1 :(得分:0)
我找到了这个问题的原因。我复制了上面的代码,在我的机器上编译并执行了valgrind。 Valgrind提供输出作为明确的泄漏“肯定丢失:1个块中的128个字节”
==5121== HEAP SUMMARY:
==5121== in use at exit: 300 bytes in 2 blocks
==5121== total heap usage: 2 allocs, 0 frees, 300 bytes allocated
==5121==
==5121== Searching for pointers to 2 not-freed blocks
==5121== Checked 52,360 bytes
==5121==
==5121== 128 bytes in 1 blocks are definitely lost in loss record 1 of 2
==5121== at 0x400682F: malloc (vg_replace_malloc.c:236)
==5121== by 0x804865C: main (in /home/userm/code/a3.out)
==5121==
==5121== LEAK SUMMARY:
==5121== definitely lost: 128 bytes in 1 blocks
==5121== indirectly lost: 0 bytes in 0 blocks
==5121== possibly lost: 0 bytes in 0 blocks
==5121== still reachable: 172 bytes in 1 blocks
==5121== suppressed: 0 bytes in 0 blocks
==5121== Reachable blocks (those to which a pointer was found) are not shown.
==5121== To see them, rerun with: --leak-check=full --show-reachable=yes
==5121==
==5121== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 8)
==5121== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 8)
在进一步分析并阻止128字节泄漏时,我只是评论了被怀疑是导致此问题的代码段。
//char *the_link;
//the_link = malloc(sizeof(char) * (link_len +1)); // <=== here is where valgrind complains
//strncpy(the_link, &line[j + 1], link_len - 1);
//the_link[link_len] = '\0';
但是,在运行valgrind之后,它报告了同样的问题。所以经过仔细检查后,还有一个malloc在代码中完成,这可能是罪魁祸首。因此,在函数调用结束时添加free(line)后,解决了这个valgrind错误。
char *line;
int line_len = 128;
line = malloc(sizeof(char)*line_len);
free(line); //added at the end of code
现在valgrind输出是:
==5143== LEAK SUMMARY:
==5143== definitely lost: 0 bytes in 0 blocks
==5143== indirectly lost: 0 bytes in 0 blocks
==5143== possibly lost: 0 bytes in 0 blocks
==5143== still reachable: 172 bytes in 1 blocks
==5143== suppressed: 0 bytes in 0 blocks
==5143== Reachable blocks (those to which a pointer was found) are not shown.
==5143== To see them, rerun with: --leak-check=full --show-reachable=yes
==5143==
==5143== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 8)
==5143== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 8)
请尝试此更改,并让我知道结果。