|
From: Ashley P. <api...@co...> - 2007-10-18 10:12:32
|
Odd, it appears this mail didn't make it to the list, lets try again...
-------- Forwarded Message --------
From: Ashley Pittman <api...@co...>
To: Tom Hughes <to...@co...>
Cc: val...@li...
Subject: Re: [Valgrind-users] problems with understanding valgrind
errors
Date: Thu, 18 Oct 2007 10:54:09 +0100
On Thu, 2007-10-18 at 10:39 +0100, Tom Hughes wrote:
> You probably want to do this:
>
> VALGRIND_CHECK_MEM_IS_DEFINED(filename, strlen(filename))
> VALGRIND_CHECK_MEM_IS_DEFINED(fname, strlen(fname))
Actually what you really want is this:
{
int len = strlen(fname);
VALGRIND_CHECK_VALUE_IS_DEFINED(filename);
VALGRIND_CHECK_VALUE_IS_DEFINED(fname);
VALGRIND_CHECK_MEM_IS_DEFINED(fname, len+1)
VALGRIND_CHECK_MEM_IS_DEFINED(filename, len+8)
}
Leaving in the VALGRIND_CHECK_VALUE_IS_DEFINED() calls is perfectly
reasonable, they will simply test for a different type of error.
Of course you have at least one bug in your code wrt the len paramater
to snprintf(), if you simply pass in the strlen() of the string you are
generating you might as well just use sprintf(), you should pass in the
allocated length of filename here, the intention is to prevent buffer
overruns after all. Your second bug is that the computed value that you
do pass in will truncate the result of filename to be exactly that of
fname, if you really want to append .states you should change the +1 to
be +8.
$ cat bob.c
int main () {
char *fname = "file";
char *filename = malloc(100);
snprintf(filename, strlen(fname)+1, "%s.states", fname);
printf("Result is \"%s\"\n",filename);
return 0;
}
$ cc bob.c
$ ./a.out
Result is "file"
Ashley,
|