|
From: Черкасов Э. <edg...@ya...> - 2012-08-18 19:52:23
|
Hello!
I checked my small prog with valgrind and faced with the problem: I can't understand which uses uninitialised value.
I tried to replace keyword.key = (char *) malloc (sizeof (char) * (strlen (key) + 1)); to keyword.key = (char *) calloc (sizeof (char), (strlen (key) + 1)); and there is no errors.
Function with errors:
struct key {
char *key;
int *index;
int length;
};
struct key
gen_key_struct (char *key)
{
int i;
struct key keyword;
prepare_key (key);
keyword.key = (char *) malloc (sizeof (char) * (strlen (key) + 1));
i = 0;
do {
keyword.key[i] = key[i];
i++;
} while (key[i] != '\0');
keyword.index = (int *) malloc (sizeof (int) * strlen (keyword.key));
for (i = 0; i < strlen (keyword.key); i++)
keyword.index[i] = i+1;
keyword.length = strlen (keyword.key);
return keyword;
}
And error summary:
==3242==
==3242== HEAP SUMMARY:
==3242== in use at exit: 0 bytes in 0 blocks
==3242== total heap usage: 25 allocs, 25 frees, 202 bytes allocated
==3242==
==3242== All heap blocks were freed -- no leaks are possible
==3242==
==3242== ERROR SUMMARY: 7 errors from 4 contexts (suppressed: 0 from 0)
==3242==
==3242== 1 errors in context 1 of 4:
==3242== Conditional jump or move depends on uninitialised value(s)
==3242== at 0x4092B8E: vfprintf (vfprintf.c:1624)
==3242== by 0x4097C2E: printf (printf.c:35)
==3242== by 0x804888B: main (main.c:161)
==3242== Uninitialised value was created by a heap allocation
==3242== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3242== by 0x8049266: gen_key_struct (main.c:354)
==3242== by 0x80487C7: main (main.c:144)
==3242==
==3242==
==3242== 1 errors in context 2 of 4:
==3242== Conditional jump or move depends on uninitialised value(s)
==3242== at 0x804931E: gen_key_struct (main.c:363)
==3242== by 0x80487C7: main (main.c:144)
==3242== Uninitialised value was created by a heap allocation
==3242== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3242== by 0x8049266: gen_key_struct (main.c:354)
==3242== by 0x80487C7: main (main.c:144)
==3242==
==3242==
==3242== 1 errors in context 3 of 4:
==3242== Conditional jump or move depends on uninitialised value(s)
==3242== at 0x80492AB: gen_key_struct (main.c:360)
==3242== by 0x80487C7: main (main.c:144)
==3242== Uninitialised value was created by a heap allocation
==3242== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3242== by 0x8049266: gen_key_struct (main.c:354)
==3242== by 0x80487C7: main (main.c:144)
==3242==
==3242==
==3242== 4 errors in context 4 of 4:
==3242== Conditional jump or move depends on uninitialised value(s)
==3242== at 0x80492FB: gen_key_struct (main.c:361)
==3242== by 0x80487C7: main (main.c:144)
==3242== Uninitialised value was created by a heap allocation
==3242== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3242== by 0x8049266: gen_key_struct (main.c:354)
==3242== by 0x80487C7: main (main.c:144)
==3242==
==3242== ERROR SUMMARY: 7 errors from 4 contexts (suppressed: 0 from 0)
My system: Linux edgar-1015BX 3.2.0-29-generic-pae #46-Ubuntu SMP Fri Jul 27 17:25:43 UTC 2012 i686 athlon i386 GNU/Linux
|
|
From: John R. <jr...@bi...> - 2012-08-18 20:40:48
|
> keyword.key = (char *) malloc (sizeof (char) * (strlen (key) + 1));
> i = 0;
> do {
> keyword.key[i] = key[i];
> i++;
> } while (key[i] != '\0');
The [very bad!] strategic error is that you wrote your own code
instead of using a library function such as 'strdup':
keyword.key = strdup(key);
or at least 'strncpy':
i= sizeof (char) * (strlen (key) + 1);
keyword.key = malloc (i);
strncpy(keyword.key, key, i);
[You also forgot to check for out-of-memory: 0==malloc(...) etc.]
The tactical error is that you did not copy the terminating '\0'
because the loop termination test (for a given index) occurs
before the copying of the _corresponding_ 'char'.
--
|
|
From: David C. <dcc...@ac...> - 2012-08-18 21:06:24
|
On 8/18/2012 12:52 PM, Черкасов Эдгар wrote:
> Hello!
> I checked my small prog with valgrind and faced with the problem: I can't understand which uses uninitialised value.
> I tried to replace keyword.key = (char *) malloc (sizeof (char) * (strlen (key) + 1)); to keyword.key = (char *) calloc (sizeof (char), (strlen (key) + 1)); and there is no errors.
>
> Function with errors:
> struct key {
> char *key;
> int *index;
> int length;
> };
>
> struct key
> gen_key_struct (char *key)
> {
> int i;
> struct key keyword;
>
> prepare_key (key);
> keyword.key = (char *) malloc (sizeof (char) * (strlen (key) + 1));
> i = 0;
> do {
> keyword.key[i] = key[i];
> i++;
> } while (key[i] != '\0');
Try replacing this loop with:
strcpy(keyword.key,key);
If necessary, I can explain why there is a problem with your original
implementation but feel it is best that you try to figure it out on your
own first. This is important to understand; your implementation is not
equivalent to strcpy().
> keyword.index = (int *) malloc (sizeof (int) * strlen (keyword.key));
> for (i = 0; i < strlen (keyword.key); i++)
> keyword.index[i] = i+1;
> keyword.length = strlen (keyword.key);
> return keyword;
> }
>
> And error summary:
> ==3242==
> ==3242== HEAP SUMMARY:
> ==3242== in use at exit: 0 bytes in 0 blocks
> ==3242== total heap usage: 25 allocs, 25 frees, 202 bytes allocated
> ==3242==
> ==3242== All heap blocks were freed -- no leaks are possible
> ==3242==
> ==3242== ERROR SUMMARY: 7 errors from 4 contexts (suppressed: 0 from 0)
> ==3242==
> ==3242== 1 errors in context 1 of 4:
> ==3242== Conditional jump or move depends on uninitialised value(s)
> ==3242== at 0x4092B8E: vfprintf (vfprintf.c:1624)
> ==3242== by 0x4097C2E: printf (printf.c:35)
> ==3242== by 0x804888B: main (main.c:161)
> ==3242== Uninitialised value was created by a heap allocation
> ==3242== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
> ==3242== by 0x8049266: gen_key_struct (main.c:354)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242==
> ==3242==
> ==3242== 1 errors in context 2 of 4:
> ==3242== Conditional jump or move depends on uninitialised value(s)
> ==3242== at 0x804931E: gen_key_struct (main.c:363)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242== Uninitialised value was created by a heap allocation
> ==3242== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
> ==3242== by 0x8049266: gen_key_struct (main.c:354)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242==
> ==3242==
> ==3242== 1 errors in context 3 of 4:
> ==3242== Conditional jump or move depends on uninitialised value(s)
> ==3242== at 0x80492AB: gen_key_struct (main.c:360)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242== Uninitialised value was created by a heap allocation
> ==3242== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
> ==3242== by 0x8049266: gen_key_struct (main.c:354)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242==
> ==3242==
> ==3242== 4 errors in context 4 of 4:
> ==3242== Conditional jump or move depends on uninitialised value(s)
> ==3242== at 0x80492FB: gen_key_struct (main.c:361)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242== Uninitialised value was created by a heap allocation
> ==3242== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
> ==3242== by 0x8049266: gen_key_struct (main.c:354)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242==
> ==3242== ERROR SUMMARY: 7 errors from 4 contexts (suppressed: 0 from 0)
>
> My system: Linux edgar-1015BX 3.2.0-29-generic-pae #46-Ubuntu SMP Fri Jul 27 17:25:43 UTC 2012 i686 athlon i386 GNU/Linux
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Valgrind-users mailing list
> Val...@li...
> https://lists.sourceforge.net/lists/listinfo/valgrind-users
>
>
--
David Chapman dcc...@ac...
Chapman Consulting -- San Jose, CA
Software Development Done Right.
www.chapman-consulting-sj.com
|