tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tinycc-devel] Problem using alloca


From: Carlos Montiers
Subject: [Tinycc-devel] Problem using alloca
Date: Sun, 29 Jun 2014 21:38:33 -0400

Hello.
I requesting memory from the stack using the function alloca.
I know that the use of it is discourage, but I want use it anyways.

I write a code for windows that use it and I tested ok, but only when I compile with gcc. Compiling it with tiny c fails, the program crash.
Maybe is a bug of tiny c.


This is the code:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <malloc.h>
#include <setjmp.h>

int _resetstkoflw ( void );

static jmp_buf alloca_jmp_buf;

LONG WINAPI AllocaExceptionFilter(EXCEPTION_POINTERS *ExceptionInfo)
{
   
    switch(ExceptionInfo->ExceptionRecord->ExceptionCode) {
        case STATUS_STACK_OVERFLOW :
           
            // reset the stack
            if (0 == _resetstkoflw()) {
                printf("Could not reset the stack!\n");
                _exit(1);
            }
           
            longjmp(alloca_jmp_buf, 1);
           
            break;
    }
   
    return EXCEPTION_EXECUTE_HANDLER;
}

//Alloca survive. Programmed by Carlos Montiers.

int main()
{
    void * m;
    int alloca_jmp_res;
    LPTOP_LEVEL_EXCEPTION_FILTER prev;
   
    //replace the exception filter function saving the previous
    prev = SetUnhandledExceptionFilter(AllocaExceptionFilter);
   
    alloca_jmp_res = setjmp(alloca_jmp_buf);
    if ( (0 == alloca_jmp_res) ) {
        m = alloca( INT_MAX );
       
    } else if ( (1 == alloca_jmp_res) ) {
        m = NULL;
       
    }
   
    //restore exception filter function
    SetUnhandledExceptionFilter(prev);
   
    if (! m) {
        printf("alloca Failed\n");
    }
   
    printf("Bye\n");
    return 1;

}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]