Valgrind:条件跳转或移动取决于未初始化的值-打开文件

LAKY911

[编辑]:我添加了完整的代码。

我必须在C语言的unix系统上创建一个简单版本的“ grep”命令。一切工作正常,只有Valgrind说Conditional jump or move depends on uninitialised value(s)
我认为,它可能已连接到我要打开的文件。请参阅下面的代码。
请注意,我不能<string.h>在我的代码中使用
我在Ubuntu上用clang编译代码:

cc -pedantic -Wall -Werror -g -std=c99 grep.c -o program

这就是瓦尔格隆德所说的:

lukas@lukas-VirtualBox:~/Desktop/shared/Lab04/prg-hw04$ valgrind --track-origins=yes ./program Mem /proc/meminfo
==2588== Memcheck, a memory error detector
==2588== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2588== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2588== Command: ./program Mem /proc/meminfo
==2588== 
==2588== Conditional jump or move depends on uninitialised value(s)
==2588==    at 0x4C32D08: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2588==    by 0x4EBC9D1: puts (ioputs.c:35)
==2588==    by 0x108970: check (grep.c:14)
==2588==    by 0x108AA9: read (grep.c:50)
==2588==    by 0x108B66: main (grep.c:71)
==2588==  Uninitialised value was created by a heap allocation
==2588==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2588==    by 0x108A04: read (grep.c:33)
==2588==    by 0x108B66: main (grep.c:71)
==2588== 
MemTotal:       10461696 kB
MemFree:         7701488 kB
MemAvailable:    8480772 kB
==2588== 
==2588== HEAP SUMMARY:
==2588==     in use at exit: 0 bytes in 0 blocks
==2588==   total heap usage: 4 allocs, 4 frees, 2,700 bytes allocated
==2588== 
==2588== All heap blocks were freed -- no leaks are possible
==2588== 
==2588== For counts of detected and suppressed errors, rerun with: -v
==2588== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

您能帮我解决问题吗?
这是我的grep.c文件。

#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
int printed = 1;    // return value -> 0 for patter found, 1 for pattern not found
char *pattern;
char *dest;

void check(char *line, int length, int size) {
    for (int i = 0; i < length; i++) {
        if (line[i] == pattern[0]) {
            for (int j = 1; j < size && (i+j) < length; j++) {
                if (line[i+j] == pattern[j]) {
                    if (j==size-1) {
                        printf("%s\n", line);   // print line
                        printed = 0;    // pattern found
                        goto END;
                    }
                } else {
                    break;
                }
            }
        }
    }
    END: ;
}

void read(void) {       // read lines, then check individual lines
    int c;
    int lengthPat = 0; 
    while(pattern[++lengthPat] != '\0');    // check length of pattern - I can't use string.h library
    FILE *file = fopen(dest, "r");
    size_t size =100;
    char *line = (char*)malloc(size * sizeof(char));
    if (line == NULL)   //succesfully created malloc?
        exit(102);
    int last = 0;
    if (file != NULL) {     // file succesfully opened
        while ((c = getc(file)) != EOF) {   
             if (c != '\n') {       // read line until \n
                if(last ==size)  {
                    char *p_line = realloc(line, 2*size*sizeof(char));
                    if (p_line == NULL)
                        free(line);
                    line = p_line;
                    size *= 2;
                }
                line[last++] = (char)c;
             }
             else {             // end of line, check for pattern
                check(line, last, lengthPat);
                last = 0; 
                for (int i = 0; i < size; i++) {    
                    line[i] = '\0';
                }
             }
        }
        fclose(file);
        free(line);
    }
    else {
    fprintf(stderr, "Error: Could not open file!\n");
    }
}

/* The main program */
int main(int argc, char *argv[])
{   
    if (argc == 3) {
        pattern = argv[1];
        dest = argv[2];
        read();
    }
    return printed;
}
约书亚记

“连接到文件”是关键。

您的输入文件中的行长超过100个字符。用动态增长的堆数组替换堆栈数组。

size_t size =100;
char c_line = malloc(size);

...
if(last ==size) 
     line = c_line = realloc(c_line, size<<=1);

在解决此问题时,此行上的错误是:

printf("%s\n", line);   // print line

line不是以null终止的,因此使用printf是高级主题。我们改为这样做:

for (int k = 0; k < length; k++)
    putc(line[k], stdout);
putc(line[k], stdout);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Valgrind 消息:条件跳转或移动取决于未解析的未初始化值

fread() 导致 Valgrind 错误:“条件跳转或移动取决于未初始化的值”

查明“条件跳转或移动取决于未初始化的值” valgrind消息

条件跳转或移动取决于使用valgrind的未初始化值

错误:条件跳转或移动取决于未初始化的值-C valgrind

Valgrind + C:条件跳转或移动取决于未初始化的值

Valgrind:使用哈希函数时,条件跳转或移动取决于未初始化的值

条件跳转或移动取决于未初始化的值

CPP中的Valgrind和内存泄漏:“有条件的跳转或移动取决于未初始化的值”

二进制搜索树,Valgrind条件跳转或移动取决于未初始化的值

Valgrind:libnvidia-glcore.so.346.47条件跳转或移动取决于未初始化的值

Valgrind:使用atomic :: compare_exchange_weak时,条件跳转或移动取决于未初始化的值

条件跳转或移动取决于未初始化的值strcat

条件跳转或移动取决于std :: wistringstream的未初始化值

释放字符时,“有条件的跳转或移动取决于未初始化的值”

C ++中的动态数组-条件跳转或移动取决于未初始化的值

条件跳转或移动取决于 for 循环中带有 strcat 的未初始化值

如何修复C中的“条件跳转或移动取决于未初始化的值”错误

条件跳转或移动取决于 strcpy 处的未初始化值

有条件的跳转或移动取决于未初始化的值和无效的大小写

如何解决“有条件的跳跃或移动取决于未初始化的值” valgrind错误而导致的strlen错误?

打开文件取决于是否是.gz

Valgrind:条件跳转,未初始化的值,C ++基本代码

初始化上下文:属性占位符取决于 Maven 配置文件

以未打开的形式打开文件ThreadStateException

Eclipse> .html文件未打开

如何删除未打开的文件?

Quickfix列表未打开文件

MIME文件附件未打开