Why first page of memory unmapped :NULL pointer bugs

On Solaris,application text loads at 0x00001000.
See below code we can understand this with a problem

#include iostream

#include string
using namespace std;
int main()
{
char *string = NULL;
printf("\nString length = %d", strlen(string));
return 0; }

What will happen running above code ,a segmentation fault error.
The cause is simple that we are de-referencing a null pointer,finding strlen(string).
About first 64K of the address space between 0x00000000 and 0x0000FFFF is not used, so our pointer is locating
0x00000000 will dump a segmentation error.

what we can do for it,

--> one way we can do it avoiding NULL assignment to char pointers with NULL value.
-->or just set the environment variable before u run above code,
% setenv LD_PRELOAD_32 /usr/lib/0@0.so.1
./test
string length = 0
Note:the decision to leave the first page of memory unmapped was made intentionally to catch poorly written code. For example, there may be string pointers inside uninitialized malloced blocks. The string pointers might be NULL 99% of the time, but as soon as malloc returns nonzeroed memory (because a block was previously used and freed) then blammo! Your code is trying to read garbage.

It's better to find such code and initialize the variables in question to a pointer to an empty string.

No comments :

Post a Comment