Re: pointer one past malloc.ed memory

From: Joe Wright (joewwright_at_comcast.net)
Date: 07/11/04


Date: Sun, 11 Jul 2004 10:50:06 -0400

Sushil wrote:

> Hi Gurus
>
> I've tried to come up with a small logical example of my problem.
> The problem is platform specific (MIPS) which I understand should not
> be discussed here.
>
> So here goes my example:
>
> Code is doing malloc of variable sizes.
> The last byte of malloc.ed memory is written a magic.
> Since the size if variable (and iit ncludes magic), the code adds size
> and subtracts sizeof magic to access the magic.
> Now this is where the problem comes.
> The calculation of address generates an overflow exception.
>
> Let me give an example (it's a corner case I ran into)
> 0x8000 0000 is end of the malloc.ed memory and magic is 8 bytes
> so it is a lw $v0, -8($v0)
>
> 0x8000 0000 + -8 is generating an overflow exception for address.
> The address 0x7fff fff8, is perfectly valid to dereference but
> *computation* of the address is overflowing.
>
> Sorry i had to give some platform specific details.
> But I am hoping it will clarify my question.
>
> Now my question to you about C language is:
> Can we have the address 1 byte beyond the malloc.ed area (no it is not
> dereferenced) and then a negative offset into it to legitimately
> dereference the memory malloc.ed by malloc()?
> What does the standard say here?
> Or is it just another case of overflow and we can not blame MIPS
> load-store unit for overflown address generation (thats what I think).
>
> I've solved the problem by first subtracting 8 then adding size . i.e.
> instead of load from -8(ptr) I forced the code to do ptr to point to 8
> bytes earlier and then load from 0(ptr). But I want to know what the
> standard says about the address of malloc.ed memory.
>
> Thanks
> - Ramachandran

We generally don't know MIPS assembler and as a C programmer, you
shouldn't care. If you care to post some C code which exhibits the
problem, several people here might be more able to help you.

Note that 0x80000000 is -2147483648 or INT_MIN on 32-bit systems.

-- 
Joe Wright                            mailto:joewwright@comcast.net
"Everything should be made as simple as possible, but not simpler."
                     --- Albert Einstein ---


Relevant Pages

  • pointer one past malloc.ed memory
    ... The last byte of malloc.ed memory is written a magic. ... Sorry i had to give some platform specific details. ... Or is it just another case of overflow and we can not blame MIPS ...
    (comp.lang.c)
  • Re: [PATCH] Only show RESOURCES_64BIT on relevant architectures
    ... bool "64 bit Memory and IO resources " if ... On MIPS it would be a per platform thing. ...
    (Linux-Kernel)
  • FS: O200 Dual 270 R12K + Gigachannel + SI+Tram
    ... FPU: MIPS R12010 Floating Point Chip Revision: 2.3 ... Main memory size: 1152 Mbytes ... 4 Mbytes ...
    (comp.sys.sgi.marketplace)
  • ALSA on MIPS platform
    ... I'm using PCI Sound cards on MIPS platform which has noncoherent ... There are some issues in ALSA for these platform. ... ALSA uses virt_to_pageto get 'struct page' for DMA area which was ...
    (Linux-Kernel)
  • Re: ALSA on MIPS platform
    ... I'm using PCI Sound cards on MIPS platform which has noncoherent ... There are some issues in ALSA for these platform. ... > ALSA uses virt_to_pageto get 'struct page' for DMA area which was ... > How do we fix this? ...
    (Linux-Kernel)