简体   繁体   中英

malloc / new fails from within a function pointer

I have the following structure definitions:

struct  info_link {
    char                url[100];
    http_response       *(*worker)(http_request *request, char *arguments);
    long                url_length;
};

As you can see, "worker" is a function pointer.
Now, I have the following function:

http_response   *info_show_providers(http_request *request, char *arguments) {
    char        *test = new char[100];
    long        i = 5;
}

And I define a simple info_link array with 1 member in it, as follows:

info_link       internal_links[] = {
                {"/show_providers", info_show_providers, 15}
};

I'm trying to call:

internal_links[0].worker(x,x);

And the program does access the "info_show_providers" function, but it fails on the "new char[100]" function every time I do it.
it throws SIGABRT.

could it be that the way I'm calling the function pointer isn't right? Or corrupts the stack?
How should it be done properly?

Glibc's malloc can raise a SIGABRT if the heap is corrupted. Two scenarios pop to mind:

1) You are withing a signal handler (and you shouldn't call malloc).

2) You have memory corruption elsewhere that trashed your heap (eg: overflows/underflows are a common cause of heap corruption).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM