R
Robbie Brown
I'm trying to understand the issues surrounding overwriting memory.
To this end I have the following (truncated) gdb session.
The main question is at the end and probably appears naive in the
extreme. I'm just checking my understanding.
First a deliberate mistake
I apparently declare an array of pointers to int
but only allocate enough space for int
int **pr4 = malloc(sizeof(int) * 5);
gdb print pr4
(int **) 0x602040
gdb x/1xg 0x602040
0x602040: 0x0000000000000000
0x602048: 0x0000000000000000
0x602050: 0x00000000 <- last int
54: 00000000 <- padding?
0x602058: 0x0000000000020fb1
I have actually allocated enough space to store
5*4 byte integers, I think the last 4 bytes at 54 is (64 bit)word align.
***
Is this correct?
***
I then declare and init an int and assign it's address
to the 3rd slot of the array. This effectively overwrites the padding bytes.
int i2 = 14;
pr4[2] = &i2; //address should overwrite the padding
gdb print pr4
(int **) 0x602040
gdb x/1xg 0x602040
0x602040: 0x0000000000000000
0x602048: 0x0000000000000000
0x602050: 0x00007fffffffe5d8 <- overwrites unallocated 4 bytes
0x602058: 0x0000000000020fb1
I can view the value and print it out
gdb x/1xw 0x00007fffffffe5d8
0x7fffffffe5d8: 0x0000000e <- pr4[2] i2 (14)
and I can keep going
....
int i4 = 16;
pr4[4] = &i4;
gdb print pr4
(int **) 0x602040
gdb x/1xg 0x602040
0x602040: 0x0000000000000000
0x602048: 0x0000000000000000
0x602050: 0x00007fffffffe5d0 <- alloc'd mem stops at 602054
0x602058: 0x00007fffffffe5d4
0x602060: 0x00007fffffffe5d8
gdbx/1xw 0x00007fffffffe5d0
0x7fffffffe5d0: 0x0000000e <- pr4[2] i2 (14)
0x7fffffffe5d4: 0x0000000d <- pr4[3] i3 (13)
0x7fffffffe5d8: 0x00000010 <- pr4[4] i4 (16)
I have now allocated 20 bytes more that I declared for.
I can access this memory, dereference the pointer and print
out the stored value
printf("%d\n", *pr4[4]);
If you have got this far, kudos
The question is this
It appears that I can go well beyond the allocated space and still
access the memory without problem, it doesn't appear to be an issue
What *does* appear to be the issue however is that the additional memory
I have 'stolen' may be in use by another part of the program. I have
overwritten this memory despite not asking for it and that may cause
problems elsewhere. *This* appears to be the issue.
***
Is this correct
***
Thank you for your indulgence.
To this end I have the following (truncated) gdb session.
The main question is at the end and probably appears naive in the
extreme. I'm just checking my understanding.
First a deliberate mistake
I apparently declare an array of pointers to int
but only allocate enough space for int
int **pr4 = malloc(sizeof(int) * 5);
gdb print pr4
(int **) 0x602040
gdb x/1xg 0x602040
0x602040: 0x0000000000000000
0x602048: 0x0000000000000000
0x602050: 0x00000000 <- last int
54: 00000000 <- padding?
0x602058: 0x0000000000020fb1
I have actually allocated enough space to store
5*4 byte integers, I think the last 4 bytes at 54 is (64 bit)word align.
***
Is this correct?
***
I then declare and init an int and assign it's address
to the 3rd slot of the array. This effectively overwrites the padding bytes.
int i2 = 14;
pr4[2] = &i2; //address should overwrite the padding
gdb print pr4
(int **) 0x602040
gdb x/1xg 0x602040
0x602040: 0x0000000000000000
0x602048: 0x0000000000000000
0x602050: 0x00007fffffffe5d8 <- overwrites unallocated 4 bytes
0x602058: 0x0000000000020fb1
I can view the value and print it out
gdb x/1xw 0x00007fffffffe5d8
0x7fffffffe5d8: 0x0000000e <- pr4[2] i2 (14)
and I can keep going
....
int i4 = 16;
pr4[4] = &i4;
gdb print pr4
(int **) 0x602040
gdb x/1xg 0x602040
0x602040: 0x0000000000000000
0x602048: 0x0000000000000000
0x602050: 0x00007fffffffe5d0 <- alloc'd mem stops at 602054
0x602058: 0x00007fffffffe5d4
0x602060: 0x00007fffffffe5d8
gdbx/1xw 0x00007fffffffe5d0
0x7fffffffe5d0: 0x0000000e <- pr4[2] i2 (14)
0x7fffffffe5d4: 0x0000000d <- pr4[3] i3 (13)
0x7fffffffe5d8: 0x00000010 <- pr4[4] i4 (16)
I have now allocated 20 bytes more that I declared for.
I can access this memory, dereference the pointer and print
out the stored value
printf("%d\n", *pr4[4]);
If you have got this far, kudos
The question is this
It appears that I can go well beyond the allocated space and still
access the memory without problem, it doesn't appear to be an issue
What *does* appear to be the issue however is that the additional memory
I have 'stolen' may be in use by another part of the program. I have
overwritten this memory despite not asking for it and that may cause
problems elsewhere. *This* appears to be the issue.
***
Is this correct
***
Thank you for your indulgence.