Re: Crash by allocationg small blocks

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Severian (severian_at_chlamydia-is-not-a-flower.com)
Date: 01/15/05


Date: Sat, 15 Jan 2005 13:38:58 GMT

On Thu, 13 Jan 2005 02:05:08 -0800, "Sebastien LEGUET" <Sebastien
LEGUET@discussions.microsoft.com> wrote:

>Hello,
>
>I try to exeute this code and I have a crash during the execution. The
>realloc function return NULL.
>#include "stdio.h"
>#include "stdlib.h"
>
>int main()
>{
> char** pData = NULL;
> char *pszFicDescName = NULL;
>
> for( int i=0; i< 7000000; i++ )
> {
> pszFicDescName = (char*)malloc(1041);
> strcpy(pszFicDescName,"Ceci est un test");
> if (pData == NULL)
> pData = (char**)malloc( sizeof(char*) );

/* You know, you might not crash if you CHECK FOR
** failure HERE: */

> pData = (char**)realloc( (void*)pData, (i+1)*sizeof(char*) );

/* Or here!! */

>//access violation in small-block allocator
> pData[i] = pszFicDescName;
> printf("Cpt = %d\n",i);
> }
>
> return(0);
>} //end main

Check for failure when mallocing/reallocing your data, and don't
expect the system to handle every naive memory request you might ever
make.

How much memory do you think you have?

The CRT allocater is probably not designed for what you want; however,
designing a general allocator for your case may not be simple.

>Could you tell me if you know about that problem and how can I resolved that
>because I need to use standard C code.
>I tried this code on VC6 and VC7 and I have the same result. I know that
>there is a resolution by declaring _set_sbh_threshold(0); but it doesn't work
>and I have the SP5 of VC6 so the realloc function has been corrected.
>
>In fact if I comment the line
> pData[i] = pszFicDescName;
>the program is correctly executed to the end.

Well, duh. That's because you're ignoring the return from both
malloc() and realloc() and -- if you comment the line -- never
writing to the NULL pointer (returned when memory is exhausted).

The CRT memory system is fairly limited. You either need to redesign
your process or at least redesign your memory allocations.

>Thanks
>
>Sebastien
>

--
Sev


Relevant Pages

  • Re: Crash by allocationg small blocks
    ... >>I try to exeute this code and I have a crash during the execution. ... >>realloc function return NULL. ... >>int main ...
    (microsoft.public.vc.language)
  • Re: [Full-disclosure] [Dailydave] What RedHat doesnt want you to know about ExecShield (without
    ... buffer overflow attacks by performing executable memory checks. ... This is not the case with ExecShield without NX. ... code execution, in the other you do not. ...
    (Full-Disclosure)
  • [NT] Defeating Microsoft Windows XP SP2 Heap Protection and DEP Bypass
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... and bypassing DEP (Data Execution Prevention). ... Buffer overrun attacks are among the most common mechanisms, or vectors, ... a long string to an input stream or control longer than the memory ...
    (Securiteam)
  • Re: Time to fix my PC
    ... intermittently bad) is the power supply 'system'. ... Intermittent memory often passes all tests at room temperature. ... Responsible computer manufacturers provide comprehensive hardware ... such as the crash code or data from system logs in the OS ...
    (comp.os.linux.hardware)
  • Re: Time to fix my PC
    ... Intermittent memory often passes all tests at room temperature. ... such as the crash code or data from system logs in the OS ... only certain hardware can crash a properly designed ... I'll pull out the audio and DVD drives while testing to simplify ...
    (comp.os.linux.hardware)