读取大小为1无效

时间:2014-12-07 01:49:23

标签: c memory memory-leaks valgrind

我正在努力学习我的编程技巧。我无法弄清楚我做错了什么。 我基本上将指针从一个函数调用传递给下一个函数。 我看不出这有什么问题...... ??? 当我使用Valgrind时,我无法弄清楚错误是什么。

这是我的功能的提炼版本。

*/  CIcp.c   /*
153int CIcp_SetUsedIOs(CIcp this, char *str, int len, dataTypes type)
154{
155 ASSERT();
156 if(str == NULL)
157     return 0;
158 for(int i = 0; i < len; i++)
159 {
160     if(str[i] > '8' || str[i] < '0')
161         continue;
162     else
163     {
164         if((type == DI) || (type == AI))
165         {
166             this->usedInputs |= (0x01 << (str[i] - '0'));
167         }
168         else
169         {
170             this->usedOutputs |= (0x01 << (str[i] - '0'));
171         }
172     }
173 }
174 log_info( "ICP %d Used input [%s]", this->id, ctob(this->usedInputs));
175 return 0;
176}

char *getvalue (CParamList This, const char *name) {
    char *result = NULL;
    pnv   currnv = This->current->nvlist;
    while (currnv)
    {
        if ((strcmp(name,currnv->name)== 0 )) /* 0 = match */
        {
            result = currnv->value;
            break;
        }
        currnv = currnv->next;
    }

    return result;
}

int LoadParams(CSdcList this)
{
    pPtN tmpThread;
    CIcp icp;

    for(int i = 0; i < this->icp->count; i++)
    {
        tmpThread = findNodeByIdx(this->icp, i);
        icp = (CIcp)tmpThread->sdc;
        if (isname(this->cPl, tmpThread->name, this->keys.ICP_DO))
        {
            char str[MAX_STR_LEN] = {0};
            CIcp_SetUsedIOs(icp, getvalue(this->cPl, this->keys.ICP_DO), MAX_STR_LEN, DO);
        }
    }
}

Valgrind给了我这个输出。 当我阅读文档时,它说:&#34;当你的程序在Memcheck认为不应该的地方读取或写入内存时会发生这种情况。&#34; 但我无法看到我这样做...... !!

    ==5695== Invalid read of size 1
    ==5695==    at 0x406ABF: CIcp_SetUsedIOs (CIcp.c:160)
    ==5695==    by 0x40A9E2: LoadParams (CSdcList.c:1163)
    ==5695==    by 0x407B84: getCSdcList (CSdcList.c:396)
    ==5695==    by 0x40DC5F: main (main.c:48)
    ==5695==  Address 0x5b86e0c is 0 bytes after a block of size 12 alloc'd
    ==5695==    at 0x4C2AB80: malloc (vg_replace_malloc.c:292)
    ==5695==    by 0x40D47F: newnvpair (config.c:164)
    ==5695==    by 0x40D7A9: process (config.c:244)
    ==5695==    by 0x40D90B: readParamFile (config.c:276)
    ==5695==    by 0x407973: getCSdcList (CSdcList.c:351)
    ==5695==    by 0x40DC5F: main (main.c:48)
    ==5695== 
    ==5695== Invalid read of size 1
    ==5695==    at 0x406AD3: CIcp_SetUsedIOs (CIcp.c:160)
    ==5695==    by 0x40A9E2: LoadParams (CSdcList.c:1163)
    ==5695==    by 0x407B84: getCSdcList (CSdcList.c:396)
    ==5695==    by 0x40DC5F: main (main.c:48)
    ==5695==  Address 0x5b86e0c is 0 bytes after a block of size 12 alloc'd
    ==5695==    at 0x4C2AB80: malloc (vg_replace_malloc.c:292)
    ==5695==    by 0x40D47F: newnvpair (config.c:164)
    ==5695==    by 0x40D7A9: process (config.c:244)
    ==5695==    by 0x40D90B: readParamFile (config.c:276)
    ==5695==    by 0x407973: getCSdcList (CSdcList.c:351)
    ==5695==    by 0x40DC5F: main (main.c:48)
    ==5695== 

我绝望地把我的声音拉出来...... 提前谢谢。

2 个答案:

答案 0 :(得分:1)

我怀疑所有“名称”字符串的大小都是MAX_STR_LEN。但是,如果你在CIcp_SetUsedIOs中查看循环,则总是循环遍历MAX_STR_LEN个元素。您应该检查'\0'并跳出循环。要么是这样,要么将正确长度的字符串传递给函数。

答案 1 :(得分:-2)

'this' is a reserved word (in C++) so best to not use it.

what is a 'pnv'? Is it some kind of struct. 
Perhaps you could include the struct definition 

what is ASSERT();? I know of an assert( something is true);

what is a Cicp?

perhaps the code should initialize usedInputs and usedOutputs  to zero 
before setting bits

hopefully, usedInputs and usedOutputs are larger than a single char 
(though the call to function ctob() may mean char to binary?
because a single char only has 8 bits.  and the offsets allowed are
0 ... 8  I.E. a 9 bit range.

this code snippet: (0x01 << (str[i] - '0'));
since the shift value (str[i] - '0') can be 8, and the working size of the shifted value
is only 0x01 (a single byte), some the shifts can result in a bit of a problem.

the function loadparams() will not cleanly compile 
because the function indicates a return value of type int, 
but no such return statement is in the function

There is plenty of other problems with the code, but the valgrind message
is probably about ...inputs |= and ...outputs |= statements