05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

If you efree( ) something that was allocated using malloc( ) or some mechanism<br />

other than the <strong>PHP</strong> memory-management functions, you get the following:<br />

--------------------------------------foo.c(124)<br />

: Block 0x08219C94 status:<br />

Beginning: Overrun (magic=0x00000000, expected=0x7312F8DC)<br />

End: Unknown<br />

--------------------------------------foo.c(124)<br />

: Block 0x0821EB1C status:<br />

Beginning: Overrun (magic=0x00000000, expected=0x7312F8DC)<br />

End: Unknown<br />

---------------------------------------<br />

In this case, line 124 in foo.c is the call to efree( ). <strong>PHP</strong> knows it didn’t allocate this<br />

memory because it didn’t contain the magic token that indicates a <strong>PHP</strong> allocation.<br />

The emalloc( )/efree( ) safety net also catches overruns—e.g., if you emalloc(20) but<br />

write 21 bytes to that address. For example:<br />

123: s = emalloc(6);<br />

124: strcpy(s,"Rasmus");<br />

125: efree(s);<br />

Because this code failed to allocate enough memory to hold the string and the terminating<br />

NULL, <strong>PHP</strong> prints this warning:<br />

-------------------------------------foo.c(125)<br />

: Block 0x08219CB8 status:<br />

Beginning: OK (allocated on foo.c:123, 6 bytes)<br />

End: Overflown (magic=0x2A8FCC00 instead of 0x2A8FCC84)<br />

1 byte(s) overflown<br />

-------------------------------------foo.c(125)<br />

: Block 0x08219C40 status:<br />

Beginning: OK (allocated on foo.c:123, 6 bytes)<br />

End: Overflown (magic=0x2A8FCC00 instead of 0x2A8FCC84)<br />

1 byte(s) overflown<br />

---------------------------------------<br />

The warning shows where the overflowed memory was allocated (line 123) and<br />

where this overflow was detected (line 125 in the efree( ) call).<br />

These memory-handling functions can catch a lot of silly little mistakes that might<br />

otherwise waste your time, so do your development with the debug switch enabled.<br />

Don’t forget to recompile in non-debug mode when you are done testing, though, as<br />

the various tests done by the emalloc( ) type functions slow down <strong>PHP</strong>.<br />

An extension compiled in debug mode does not work in an instance of <strong>PHP</strong> not<br />

compiled in debug mode. When <strong>PHP</strong> loads an extension, it checks to see if the<br />

debug setting, the thread-safety setting, and the API version all match. If something<br />

doesn’t match, you will get a warning like this:<br />

Warning: foo: Unable to initialize module<br />

Module compiled with debug=0, thread-safety=0 module API=20010901<br />

<strong>PHP</strong> compiled with debug=1, thread-safety=0 module API=20010901<br />

330 | Chapter 14: Extending <strong>PHP</strong><br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!