knik0/faac

cppcheck -f -v . 2>cppcheck.log >/dev/null

Closed this issue · 6 comments

char *tempVal=new char[slen];
if(RegQueryValueEx(regKey, keyStr, NULL, NULL, (BYTE *)tempVal, &len )!=ERROR_SUCCESS ||
strcmpi(tempVal,valStr))
RegSetValueEx(regKey, keyStr, 0, REG_SZ, (BYTE *)valStr, slen);
delete tempVal;

Maybe:

        BYTE *tempVal=new BYTE[slen];
...
       delete *tempVal;

?

BYTE *tempVal=new BYTE[size];
if(RegQueryValueEx(regKey, keyStr, NULL, NULL, (BYTE *)tempVal, &len )!=ERROR_SUCCESS ||
memcmp(tempVal,addr,len))
RegSetValueEx(regKey, keyStr, 0, REG_BINARY, addr, size);
delete tempVal;

Maybe:

...
       delete *tempVal;

?

dest=NULL;
if((retVal=RegQueryValueEx(regKey , keyStr , NULL , NULL, NULL, &size))==ERROR_SUCCESS)
if(*dest=(BYTE *)malloc(size+1))

Maybe:

    *dest=NULL;
...

?

What exactly does cppcheck print? It doesn't help if you just post the code lines.

@fabiangreffrath say> What exactly does cppcheck print?

git/knik0/faac/common/Cfaac$ cat cppcheck.log 
[CRegistry.cpp:209]: (error) Mismatching allocation and deallocation: tempVal
[CRegistry.cpp:229]: (error) Mismatching allocation and deallocation: tempVal
[CRegistry.cpp:345]: (error) Null pointer dereference

In the first two cases, replace delete tempVal with delete[] tempVal. You are pretty sure right with your fix to the third error.

@fabiangreffrath say> You are pretty sure right with your fix to the third error.

See:

char *dest=NULL;
if((retVal=RegQueryValueEx(regKey , keyStr , NULL , NULL, NULL, &Len))==ERROR_SUCCESS)
if(dest=(char *)malloc(Len+1))

dest=NULL;
if((retVal=RegQueryValueEx(regKey , keyStr , NULL , NULL, NULL, &size))==ERROR_SUCCESS)
if(*dest=(BYTE *)malloc(size+1))

Maybe:

int CRegistry::GetSetValN(char *keyStr, BYTE *defData, DWORD defSize, BYTE **dest)
{
long	retVal;
DWORD	size;

	*dest=NULL;
	if((retVal=RegQueryValueEx(regKey , keyStr , NULL , NULL, NULL, &size))==ERROR_SUCCESS)
		if(*dest=(BYTE *)malloc(size+1))
			retVal=RegQueryValueEx(regKey , keyStr , NULL , NULL, (BYTE *)*dest , &size);
	if(retVal!=ERROR_SUCCESS)
	{
		if(*dest)
			free(*dest);
		if(!defData)
			return 0;

		size=defSize;
		if(!(*dest=(BYTE *)malloc(size)))
			return 0;
		memcpy(*dest,defData,size);
		RegSetValueEx(regKey , keyStr , NULL , REG_BINARY , (BYTE *)*dest , size);
	}
	return size;
}

?

Would you create a PR for this?

@fabiangreffrath say> Would you create a PR for this?

No time.