Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Invalid write Valgrind

Reply
Thread Tools

Invalid write Valgrind

 
 
sanket
Guest
Posts: n/a
 
      09-21-2011
Hello all,

I am trying to read in the contents of a file into the data structure
trie (http://en.wikipedia.org/wiki/Trie). The file contains word-
meaning pairs separated by '\t'. If a word has two different meaning,
the second meaning should be appended to the existing meaning with a
comma between the two. Here is the function that adds a word into the
trie.

int add_word(const char * word, char * translation)
{
unsigned i = 0;
int len = strlen(word);
unsigned index = 0;
int retval = 0;

struct s_trie_node * curr = proot;

for(i = 0; i < len; i++)
{
index = word[i];
if(curr->children[index] != NULL) curr = curr-
>children[index];

else
{
curr->children[index] = new_node();
curr = curr->children[index];
retval++;
}
}

if(curr->translation == NULL)
{
int s = strlen(translation);
curr->translation = (char*)malloc((s+1) *
sizeof(char));
if(curr->translation == NULL){
printf("malloc failed\n");
exit(EXIT_FAILURE);
}
strcpy(curr->translation, translation);
}
else
{
char * t = curr->translation;
size_t newlen = strlen(translation) + strlen(curr-
>translation) + 1;

curr->translation = (char*)realloc(curr->translation,
newlen);

printf("%s\n", curr->translation);
printf("%s\n", translation);

if(curr->translation != NULL) t = NULL;
else
{
printf("realloc fails\n");
curr->translation = t;
t = NULL;
}

strcat(curr->translation, ",");
strcat(curr->translation, translation);
}

return retval;
}


Here are the contents of the file to read in from

Adam and Eve Adam et Eve
Albanian albanais
Albanian Albanais
Adam Adam
Adam Adamo

The program leads to memory error. When I execute the program using
Valgrind with memcheck option, I notice that when the second meaning
of a word is encountered and is appended to the existing meaning with
a comma in between, I receive the following error.

==1356== Invalid write of size 1
==1356== at 0x4C224DF: strcat (mc_replace_strmem.c:176)
==1356== by 0x400E65: add_word (prob2.c:147)
==1356== by 0x400F76: load_dictionary (prob2.c:185)
==1356== by 0x400AEA: main (prob2.c:44)
==1356== Address 0x51839b1 is 0 bytes after a block of size 17
alloc'd
==1356== at 0x4C215E2: realloc (vg_replace_malloc.c:525)
==1356== by 0x400DDE: add_word (prob2.c:133)
==1356== by 0x400F76: load_dictionary (prob2.c:185)
==1356== by 0x400AEA: main (prob2.c:44)


But the strange thing is that the error is encountered when second
meaning of the word 'Albanian' is added, though no such error is
encountered when the second meaning of 'Adam' is added. But if you
reverse them, then the error will be encountered with 'Adam' and not
'Albanian'. So, it seems that the word whose repetition is encountered
the first encounters the error.

Thanks.

 
Reply With Quote
 
 
 
 
Harald van Dijk
Guest
Posts: n/a
 
      09-21-2011
On Sep 21, 8:27*am, sanket <(E-Mail Removed)> wrote:
[...]
> * * * * * * * * size_t newlen = strlen(translation) + strlen(curr->translation) + 1;
>
> * * * * * * * * curr->translation = (char*)realloc(curr->translation,
> newlen);

[...]
> * * * * * * * * strcat(curr->translation, ",");
> * * * * * * * * strcat(curr->translation, translation);


You're writing strlen(curr->translation) + strlen(",") +
strlen(translation) + sizeof((char)'\0') bytes into curr->translation,
but newlen is one less than that. Depending on how realloc gets its
memory, this error may go undetected.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'strlen' : Why valgrind reports invalid read ? m.labanowicz@gmail.com C Programming 12 01-19-2013 10:13 AM
Annoying problem with Valgrind: write binary data Alberto C++ 7 07-09-2010 04:26 PM
Invalid read of size 1 [valgrind warning] Flash Gordon C Programming 5 12-13-2005 02:54 PM
Valgrind says "Invalid read of size 4". What's the problem? hvaisane@cc.joensuu.fi C++ 2 02-17-2005 12:45 PM
leak detected using valgrind Alexandre C++ 1 11-13-2003 07:43 AM



Advertisments