Skip to contentSkip to conversation

Realloc() problem with Efence

DISCUSSION Alain Birtz 3 participants 3 messages Jul 28, 1999

lore
Share & tools
lore
#1 lore raw
In reply to Alain Birtz

-----BEGIN PGP SIGNED MESSAGE-----

Hello...

realloc() may move the memory being realloated. To quote from the man page,

realloc() returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr...

It doesn't (and couldn't) mutate the pointer temp_c_buf directly.

Thus, you probably want to re-write your code to do

temp_c_buf = realloc(temp_c_buf, 5000 + 5000);

- --Nicholas Ingolia
[email protected]
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 5.0i for non-commercial use
Comment: Processed by Mailcrypt 3.5.3, an Emacs/PGP interface
Charset: noconv

iQCVAwUBN59h+gRHXKx72OUhAQEkNAP/bYZO+zzGI2bDBRuCYabEfaxo8/EAffTN hliGzKLwhhEetuvrUNv+pam5qBXGb3OLWWAZdRmypEvPXfYP/w3wGlJlGKWxAbGm WhBJkzwVWEkqKmqADiClP7ucJcMIYkMcLLVcCBnVEsjoPFDumoc2ncEe6beeRDIB gma/mrLVmZs=
=tyhG
-----END PGP SIGNATURE-----

[[ This message was sent via the linuxppc-dev mailing list. Replies are ]] [[ not forced back to the list, so be sure to Cc linuxppc-dev if your ]] [[ reply is of general interest. Please check http://lists.linuxppc.org/ ]]
[[ and http://www.linuxppc.org/ for useful information before posting. ]]

#2 lore raw
In reply to Alain Birtz

Alain Birtz <[email protected]> writes:
Alain> What is wrong with this code ?
Alain> Code is executed correctly without Electric Fence Alain> But linked with Efence library, dbg complaint:
Alain>

Alain> 	  Electric Fence 2.0.5 Copyright (C) 1987-1998 Bruce Perens.
Alain> 	  ...
Alain> 	  ...
Alain> 	  Program received signal SIGSEGV, Segmentation fault.

Alain>
Alain> when trying to write the first B, just after the first memory allocation Alain> buffer
Alain> (at the instruction (*c_ptr++ = 'B'))
Alain>
Alain>
Alain> void tst ()
Alain> {
Alain> int i;
Alain> char *c_buf, *c_ptr, *temp_c_buf;
Alain>
Alain> c_buf = (char *)malloc(5000);
Alain> if (c_buf != NULL)
Alain> {
Alain> c_ptr = c_buf;
Alain> for (i = 0; i < 5000; i++)
Alain> *c_ptr++ = 'A';
Alain> temp_c_buf = c_buf;
Alain> realloc(temp_c_buf, 5000 + 5000);
Alain> if (temp_c_buf != NULL)
Alain> {
Alain> for (i = 0; i < 5000; i++)
Alain> *c_ptr++ = 'B';
Alain> }
Alain> }
Alain> }
Alain>

realloc can (and did) move the block. Therefore, when you start writing 'B's, they are going into invalid memory. The fact that it works without Electric Fence is luck.

To do this, you need to set the pointer to the whole block to the output of realloc and then you can start writing B's at the new location + 5000.

Signature · 9 lines
Jerry Quinn                             Tel: (514) 761-8737
[email protected]               Fax: (514) 761-8505
Speech Recognition Research


[[ This message was sent via the linuxppc-dev mailing list.  Replies are ]]
[[ not  forced  back  to the list, so be sure to Cc linuxppc-dev if your ]]
[[ reply is of general interest. Please check http://lists.linuxppc.org/ ]]
[[ and http://www.linuxppc.org/ for useful information before posting.   ]]
#0 lore raw

What is wrong with this code ?
Code is executed correctly without Electric Fence
But linked with Efence library, dbg complaint:

Electric Fence 2.0.5 Copyright (C) 1987-1998 Bruce Perens.
...
...
Program received signal SIGSEGV, Segmentation fault.

when trying to write the first B, just after the first memory allocation buffer
(at the instruction (*c_ptr++ = 'B'))

void tst ()
{
int i;
char *c_buf, *c_ptr, *temp_c_buf;

c_buf = (char *)malloc(5000);
if (c_buf != NULL)
{
c_ptr = c_buf;
for (i = 0; i < 5000; i++)
*c_ptr++ = 'A';
temp_c_buf = c_buf;
realloc(temp_c_buf, 5000 + 5000);
if (temp_c_buf != NULL)
{
for (i = 0; i < 5000; i++)

    *c_ptr++ = 'B';

}
}
}

[[ This message was sent via the linuxppc-dev mailing list. Replies are ]] [[ not forced back to the list, so be sure to Cc linuxppc-dev if your ]] [[ reply is of general interest. Please check http://lists.linuxppc.org/ ]]
[[ and http://www.linuxppc.org/ for useful information before posting. ]]