Valgrind - 未初始化的价值

时间:2014-04-24 17:07:22

标签: c gcc initialization valgrind

以下代码是较大程序的一部分,因此我创建了一个测试文件来尝试隔离问题。代码完全按预期工作,但它抛出了一个valgrind错误。根据我的理解,我认为它很可能是指* inputStr。

Valgrind错误消息:

==2807== Conditional jump or move depends on uninitialised value(s)
==2807==    at 0x3156434819: ____strtol_l_internal (in /lib64/libc-2.5.so)
==2807==    by 0x3156431BD1: atoi (in /lib64/libc-2.5.so)
==2807==    by 0x400818: getInt (test.c:50)
==2807==    by 0x4008B5: main (test.c:70)
==2807==  Uninitialised value was created by a stack allocation
==2807==    at 0x400668: getInt (test.c:13)
==2807== 
==2807== Conditional jump or move depends on uninitialised value(s)
==2807==    at 0x315643482F: ____strtol_l_internal (in /lib64/libc-2.5.so)
==2807==    by 0x3156431BD1: atoi (in /lib64/libc-2.5.so)
==2807==    by 0x400818: getInt (test.c:50)
==2807==    by 0x4008B5: main (test.c:70)
==2807==  Uninitialised value was created by a stack allocation
==2807==    at 0x400668: getInt (test.c:13)
==2807== 
==2807== Use of uninitialised value of size 8
==2807==    at 0x31564348A8: ____strtol_l_internal (in /lib64/libc-2.5.so)
==2807==    by 0x3156431BD1: atoi (in /lib64/libc-2.5.so)
==2807==    by 0x400818: getInt (test.c:50)
==2807==    by 0x4008B5: main (test.c:70)
==2807==  Uninitialised value was created by a stack allocation
==2807==    at 0x400668: getInt (test.c:13)

我的代码:通过stdin解析输入命令(例如" i 5")," i"将在菜单开关中使用(但该代码被删除),然后调用getInt(),它开始扫描索引1处的inputStr以解析inputStr中的整数值。为清楚起见,我将错误行13,50和70标记为注释。

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

int getInt(char *inputStr, int *value, int *i) { // <-----line 13
    char num[5];
    int ctr = 0;

    /* Skip whitespace */
    while (!isdigit(*(inputStr+(*i))) && *(inputStr+(*i)) != '-') {
        /* Input with only command letter and no integer value */
        if (*(inputStr+(*i)) == '\n') {
            //printError(-1);
            return -1;
        }
        /* Input has invalid characters after command letter */
        if (!isspace(*(inputStr+(*i)))) {
            //printError(-2);
            return -2;
        }
        (*i)++;
    }

    if (*(inputStr+(*i)) == '-') {
        num[ctr++] = *(inputStr+(*i));
        (*i)++;
    }

    /* Copy number characters to another array */
    while (isdigit(*(inputStr+(*i)))) {
        num[ctr++] = *(inputStr+(*i));
        (*i)++;
    }

    /* Check if unwanted characters terminated the while loop above */
    if (!isspace(*(inputStr+(*i)))) {
        //printError(-2);
        return -2;
    }

    /* Convert number characters to integer */
    *value = atoi(num);                              // <----line 50

    //printf("Positive Integer: num = %s, value = %d\n",num, value);
    return 0;
}


int main () {
    int value, i, int1;
    char *inputStr;
    size_t sizeInput = 10;
    inputStr = malloc(sizeof(char) * sizeInput);

    for (i = 0; i < sizeInput; i++)
        inputStr[i] = '\0';

    value = 0;
    i = 1; //starting position for parsing number

    getline(&inputStr, &sizeInput, stdin);
    int1 = getInt(inputStr, &value, &i);  // <---line 70
    printf ("%d\n", value);

    free(inputStr);
    return 0;
}

感谢帮助。

[已解决]感谢@JonathanLeffler。在atoi()转换上方添加了num[ctr] = '\0';一行。现在没有错误。

1 个答案:

答案 0 :(得分:1)

您有多个错误:

  1. getline如果缓冲区不足以容纳该行,则通过第一个参数返回新内存,因此您不需要为inputStr分配内存,但您必须释放它。您只需将inputStr设置为NULL,getline将返回新​​内存。

  2. 当你调用函数getInt时,变量'i'初始化为1; C / C ++数组索引从0开始,所以它应该是0,我不知道你是否打算将1作为第一个传递。

  3. 在函数getInt中,如果while和if块没有被排除,则不会初始化变量'num',您可以将其更改为:

    char num [5] = {0};

  4. 同样在函数getInt中,没有代码可以检查字符串inputStr是否已经结束,因此你的循环可能会导致访问冲突。