diff options
author | Stefan Fritsch <sf@apache.org> | 2011-09-19 18:25:42 +0200 |
---|---|---|
committer | Stefan Fritsch <sf@apache.org> | 2011-09-19 18:25:42 +0200 |
commit | 01ed21af9d4a60239d12f2fb183a804ad46459ea (patch) | |
tree | dfcc795128d8fc349b4cd7ebe35b8f12dc73f01d /server | |
parent | Punctuation error. (diff) | |
download | apache2-01ed21af9d4a60239d12f2fb183a804ad46459ea.tar.xz apache2-01ed21af9d4a60239d12f2fb183a804ad46459ea.zip |
Add wrappers for malloc, calloc, realloc that check for out of memory
situations. Use them in most places where malloc, and friends are used.
This results in clean error messages in an out of memory situation instead of
segfaulting or silently malfunctioning. In some places, it just allows to
remove some logging code.
PR 51568, PR 51569, PR 51571.
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1172686 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'server')
-rw-r--r-- | server/config.c | 4 | ||||
-rw-r--r-- | server/main.c | 6 | ||||
-rw-r--r-- | server/mpm/event/event.c | 25 | ||||
-rw-r--r-- | server/mpm/worker/worker.c | 19 | ||||
-rw-r--r-- | server/mpm_unix.c | 2 | ||||
-rw-r--r-- | server/scoreboard.c | 10 | ||||
-rw-r--r-- | server/util.c | 33 |
7 files changed, 49 insertions, 50 deletions
diff --git a/server/config.c b/server/config.c index 1068b1f9a2..0c8e04cd34 100644 --- a/server/config.c +++ b/server/config.c @@ -772,10 +772,10 @@ AP_DECLARE(const char *) ap_setup_prelinked_modules(process_rec *process) ap_loaded_modules = (module **)apr_palloc(process->pool, sizeof(module *) * conf_vector_length); if (!ap_module_short_names) - ap_module_short_names = calloc(sizeof(char *), conf_vector_length); + ap_module_short_names = ap_calloc(sizeof(char *), conf_vector_length); if (!merger_func_cache) - merger_func_cache = calloc(sizeof(merger_func), conf_vector_length); + merger_func_cache = ap_calloc(sizeof(merger_func), conf_vector_length); if (ap_loaded_modules == NULL || ap_module_short_names == NULL || merger_func_cache == NULL) diff --git a/server/main.c b/server/main.c index 3b38bdde15..2fdd4a1ad8 100644 --- a/server/main.c +++ b/server/main.c @@ -265,14 +265,10 @@ static void destroy_and_exit_process(process_rec *process, exit(process_exit_value); } -#define OOM_MESSAGE "[crit] Memory allocation failed, " \ - "aborting process." APR_EOL_STR - /* APR callback invoked if allocation fails. */ static int abort_on_oom(int retcode) { - write(STDERR_FILENO, OOM_MESSAGE, strlen(OOM_MESSAGE)); - abort(); + ap_abort_on_oom(); return retcode; /* unreachable, hopefully. */ } diff --git a/server/mpm/event/event.c b/server/mpm/event/event.c index 82401658d9..2e4ca38453 100644 --- a/server/mpm/event/event.c +++ b/server/mpm/event/event.c @@ -1007,7 +1007,7 @@ static apr_status_t s_socket_add(void *user_baton, { s_baton_t *s = (s_baton_t*)user_baton; /* XXXXX: recycle listener_poll_types */ - listener_poll_type *pt = malloc(sizeof(*pt)); + listener_poll_type *pt = ap_malloc(sizeof(*pt)); pt->type = PT_SERF; pt->baton = serf_baton; pfd->client_data = pt; @@ -1187,7 +1187,7 @@ static apr_status_t event_register_timed_callback(apr_time_t t, } else { /* XXXXX: lol, pool allocation without a context from any thread.Yeah. Right. MPMs Suck. */ - te = malloc(sizeof(timer_event_t)); + te = ap_malloc(sizeof(timer_event_t)); APR_RING_ELEM_INIT(te, link); } @@ -1766,7 +1766,7 @@ static void create_listener_thread(thread_starter * ts) proc_info *my_info; apr_status_t rv; - my_info = (proc_info *) malloc(sizeof(proc_info)); + my_info = (proc_info *) ap_malloc(sizeof(proc_info)); my_info->pid = my_child_num; my_info->tid = -1; /* listener thread doesn't have a thread slot */ my_info->sd = 0; @@ -1865,12 +1865,7 @@ static void *APR_THREAD_FUNC start_threads(apr_thread_t * thd, void *dummy) continue; } - my_info = (proc_info *) malloc(sizeof(proc_info)); - if (my_info == NULL) { - ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf, - "malloc: out of memory"); - clean_child_exit(APEXIT_CHILDFATAL); - } + my_info = (proc_info *) ap_malloc(sizeof(proc_info)); my_info->pid = my_child_num; my_info->tid = i; my_info->sd = 0; @@ -2047,16 +2042,8 @@ static void child_main(int child_num_arg) /* clear the storage; we may not create all our threads immediately, * and we want a 0 entry to indicate a thread which was not created */ - threads = (apr_thread_t **) calloc(1, - sizeof(apr_thread_t *) * - threads_per_child); - if (threads == NULL) { - ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf, - "malloc: out of memory"); - clean_child_exit(APEXIT_CHILDFATAL); - } - - ts = (thread_starter *) apr_palloc(pchild, sizeof(*ts)); + threads = ap_calloc(threads_per_child, sizeof(apr_thread_t *)); + ts = apr_palloc(pchild, sizeof(*ts)); apr_threadattr_create(&thread_attr, pchild); /* 0 means PTHREAD_CREATE_JOINABLE */ diff --git a/server/mpm/worker/worker.c b/server/mpm/worker/worker.c index 4f80f9a370..5b9fea1aff 100644 --- a/server/mpm/worker/worker.c +++ b/server/mpm/worker/worker.c @@ -1006,7 +1006,7 @@ static void create_listener_thread(thread_starter *ts) proc_info *my_info; apr_status_t rv; - my_info = (proc_info *)malloc(sizeof(proc_info)); + my_info = (proc_info *)ap_malloc(sizeof(proc_info)); my_info->pid = my_child_num; my_info->tid = -1; /* listener thread doesn't have a thread slot */ my_info->sd = 0; @@ -1072,12 +1072,7 @@ static void * APR_THREAD_FUNC start_threads(apr_thread_t *thd, void *dummy) continue; } - my_info = (proc_info *)malloc(sizeof(proc_info)); - if (my_info == NULL) { - ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf, - "malloc: out of memory"); - clean_child_exit(APEXIT_CHILDFATAL); - } + my_info = (proc_info *)ap_malloc(sizeof(proc_info)); my_info->pid = my_child_num; my_info->tid = i; my_info->sd = 0; @@ -1271,14 +1266,8 @@ static void child_main(int child_num_arg) /* clear the storage; we may not create all our threads immediately, * and we want a 0 entry to indicate a thread which was not created */ - threads = (apr_thread_t **)calloc(1, - sizeof(apr_thread_t *) * threads_per_child); - if (threads == NULL) { - ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf, - "malloc: out of memory"); - clean_child_exit(APEXIT_CHILDFATAL); - } - + threads = (apr_thread_t **)ap_calloc(1, + sizeof(apr_thread_t *) * threads_per_child); ts = (thread_starter *)apr_palloc(pchild, sizeof(*ts)); apr_threadattr_create(&thread_attr, pchild); diff --git a/server/mpm_unix.c b/server/mpm_unix.c index 2dd43558bb..9675d2fa5c 100644 --- a/server/mpm_unix.c +++ b/server/mpm_unix.c @@ -75,7 +75,7 @@ static extra_process_t *extras; void ap_register_extra_mpm_process(pid_t pid, ap_generation_t gen) { - extra_process_t *p = (extra_process_t *)malloc(sizeof(extra_process_t)); + extra_process_t *p = (extra_process_t *)ap_malloc(sizeof(extra_process_t)); p->next = extras; p->pid = pid; diff --git a/server/scoreboard.c b/server/scoreboard.c index 22ce86224b..4d2e5a3aaa 100644 --- a/server/scoreboard.c +++ b/server/scoreboard.c @@ -149,7 +149,7 @@ void ap_init_scoreboard(void *shared_score) ap_calc_scoreboard_size(); ap_scoreboard_image = - calloc(1, sizeof(scoreboard) + server_limit * sizeof(worker_score *)); + ap_calloc(1, sizeof(scoreboard) + server_limit * sizeof(worker_score *)); more_storage = shared_score; ap_scoreboard_image->global = (global_score *)more_storage; more_storage += sizeof(global_score); @@ -325,13 +325,7 @@ int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type) #endif { /* A simple malloc will suffice */ - void *sb_mem = calloc(1, scoreboard_size); - if (sb_mem == NULL) { - ap_log_error(APLOG_MARK, APLOG_CRIT, 0, ap_server_conf, - "(%d)%s: cannot allocate scoreboard", - errno, strerror(errno)); - return HTTP_INTERNAL_SERVER_ERROR; - } + void *sb_mem = ap_calloc(1, scoreboard_size); ap_init_scoreboard(sb_mem); } diff --git a/server/util.c b/server/util.c index abecf23bb3..864a6b86a5 100644 --- a/server/util.c +++ b/server/util.c @@ -2583,3 +2583,36 @@ AP_DECLARE(void) ap_varbuf_free(struct ap_varbuf *vb) } vb->buf = NULL; } + +#define OOM_MESSAGE "[crit] Memory allocation failed, " \ + "aborting process." APR_EOL_STR + +AP_DECLARE(void) ap_abort_on_oom() +{ + write(STDERR_FILENO, OOM_MESSAGE, strlen(OOM_MESSAGE)); + abort(); +} + +AP_DECLARE(void *) ap_malloc(size_t size) +{ + void *p = malloc(size); + if (p == NULL && size != 0) + ap_abort_on_oom(); + return p; +} + +AP_DECLARE(void *) ap_calloc(size_t nelem, size_t size) +{ + void *p = calloc(nelem, size); + if (p == NULL && nelem != 0 && size != 0) + ap_abort_on_oom(); + return p; +} + +AP_DECLARE(void *) ap_realloc(void *ptr, size_t size) +{ + void *p = realloc(ptr, size); + if (p == NULL && size != 0) + ap_abort_on_oom(); + return p; +} |