From 6a00f63ff950c06372bd25c32dd8bbb2f11a773e Mon Sep 17 00:00:00 2001 From: Steven Dake Date: Fri, 28 Jul 2006 23:34:28 +0000 Subject: [PATCH] Patch so realloc reverts to old buffer if reallocation fails. git-svn-id: http://svn.fedorahosted.org/svn/corosync/trunk@1170 fd59a12c-fef9-0310-b244-a6a79926bd2f --- exec/aispoll.c | 5 +++-- exec/amfcomp.c | 17 +++++++++-------- exec/ckpt.c | 13 +++++++------ exec/cpg.c | 5 +++-- exec/evs.c | 6 +++--- exec/evt.c | 17 +++++++++++++---- exec/mainconfig.c | 20 ++++++++++++++++---- exec/totemip.c | 19 ++++++++++++++++--- exec/totempg.c | 2 +- include/hdb.h | 3 ++- lib/util.c | 3 ++- 11 files changed, 75 insertions(+), 35 deletions(-) diff --git a/exec/aispoll.c b/exec/aispoll.c index ae9f8c02..492bdb95 100644 --- a/exec/aispoll.c +++ b/exec/aispoll.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2003-2004 MontaVista Software, Inc. + * Copyright (c) 2006 Sun Microsystems, Inc. * * All rights reserved. * @@ -170,7 +171,7 @@ int poll_dispatch_add ( poll_entries = (struct poll_entry *)realloc (poll_instance->poll_entries, (poll_instance->poll_entry_count + 1) * sizeof (struct poll_entry)); - if (poll_entries == 0) { + if (poll_entries == NULL) { res = -ENOMEM; goto error_put; } @@ -179,7 +180,7 @@ int poll_dispatch_add ( ufds = (struct pollfd *)realloc (poll_instance->ufds, (poll_instance->poll_entry_count + 1) * sizeof (struct pollfd)); - if (ufds == 0) { + if (ufds == NULL) { res = -ENOMEM; goto error_put; } diff --git a/exec/amfcomp.c b/exec/amfcomp.c index 2bffbcbd..6edbd24f 100644 --- a/exec/amfcomp.c +++ b/exec/amfcomp.c @@ -1,19 +1,20 @@ /** @file amfcomp.c * * Copyright (c) 2002-2006 MontaVista Software, Inc. + * Copyright (c) 2006 Sun Microsystems, Inc. + * Copyright (c) 2006 Ericsson AB. + * + * All rights reserved. + * * Author: Steven Dake (sdake@mvista.com) * - * Copyright (c) 2006 Ericsson AB. - * Author: Hans Feldt + * Author: Hans Feldt * - Introduced AMF B.02 information model * - Use DN in API and multicast messages * - (Re-)Introduction of event based multicast messages * - Refactoring of code into several AMF files - * Author: Anders Eriksson, Lars Holm - * - Component/SU restart, SU failover - * - * All rights reserved. - * + * Author: Anders Eriksson, Lars Holm + * - Component/SU restart, SU failover * * This software licensed under BSD license, the text of which follows: * @@ -249,7 +250,7 @@ static int invocation_create ( if (invocation_addr == 0) { invocation_temp = (struct invocation *)realloc (invocation_entries, (invocation_entries_size + 1) * sizeof (struct invocation)); - if (invocation_temp == 0) { + if (invocation_temp == NULL) { return (-1); } invocation_entries = invocation_temp; diff --git a/exec/ckpt.c b/exec/ckpt.c index 4d9c4a11..b2773e01 100644 --- a/exec/ckpt.c +++ b/exec/ckpt.c @@ -2561,22 +2561,23 @@ static int recovery_section_create ( */ if (section_descriptor->section_size > checkpoint_section->section_descriptor.section_size) { + void *section_data_tmp; log_printf (LOG_LEVEL_NOTICE, "recovery_section_create reallocating data. Present Size: %d, New Size: %d\n", (int)checkpoint_section->section_descriptor.section_size, (int)section_descriptor->section_size); - checkpoint_section->section_data = + section_data_tmp = realloc (checkpoint_section->section_data, section_descriptor->section_size); - - if (checkpoint_section->section_data == 0) { + if (section_data_tmp == NULL) { log_printf (LOG_LEVEL_ERROR, - "recovery_section_create section_data realloc returned 0 Calling error_exit.\n"); + "recovery_section_create section_data realloc returned NULL Calling error_exit.\n"); error = SA_AIS_ERR_NO_MEMORY; checkpoint_section_release(checkpoint_section); goto error_exit; } + checkpoint_section->section_data = section_data_tmp; checkpoint_section->section_descriptor.section_size = section_descriptor->section_size; error = SA_AIS_OK; @@ -3107,8 +3108,8 @@ static void message_handler_req_exec_ckpt_sectionwrite ( req_exec_ckpt_sectionwrite->data_size; if (size_required > checkpoint_section->section_descriptor.section_size) { section_data = realloc (checkpoint_section->section_data, size_required); - if (section_data == 0) { - log_printf (LOG_LEVEL_ERROR, "section_data realloc returned 0 Calling error_exit.\n"); + if (section_data == NULL) { + log_printf (LOG_LEVEL_ERROR, "section_data realloc returned NULL Calling error_exit.\n"); error = SA_AIS_ERR_NO_MEMORY; goto error_exit; } diff --git a/exec/cpg.c b/exec/cpg.c index 08b82706..fa0f5dde 100644 --- a/exec/cpg.c +++ b/exec/cpg.c @@ -546,8 +546,9 @@ static void remove_node_from_groups( list_del(&gi->rg->list); newsize = gi->rg->left_list_size * 2; - newrg = realloc(gi->rg, sizeof(struct removed_group) + newsize*sizeof(mar_cpg_address_t)); - if (!newrg) { + newrg = realloc (gi->rg, + sizeof(struct removed_group) + newsize * sizeof(mar_cpg_address_t)); + if (newrg == NULL) { log_printf(LOG_LEVEL_CRIT, "Unable to realloc removed group struct. CPG callbacks will be junk."); return; } diff --git a/exec/evs.c b/exec/evs.c index 58d392de..b799bd7a 100644 --- a/exec/evs.c +++ b/exec/evs.c @@ -1,12 +1,12 @@ /* * Copyright (c) 2004-2006 MontaVista Software, Inc. * Copyright (c) 2006 Red Hat, Inc. - * Author: Steven Dake (sdake@mvista.com) - * * Copyright (c) 2006 Sun Microsystems, Inc. * * All rights reserved. * + * Author: Steven Dake (sdake@mvista.com) + * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without @@ -290,7 +290,7 @@ static void message_handler_req_evs_join (void *conn, void *msg) addr = realloc (evs_pd->groups, sizeof (struct evs_group) * (evs_pd->group_entries + req_lib_evs_join->group_entries)); - if (addr == 0) { + if (addr == NULL) { error = SA_AIS_ERR_NO_MEMORY; goto exit_error; } diff --git a/exec/evt.c b/exec/evt.c index 67e676f2..a199cd1d 100644 --- a/exec/evt.c +++ b/exec/evt.c @@ -1,7 +1,6 @@ /* * Copyright (c) 2004-2006 Mark Haverkamp * Copyright (c) 2004-2006 Open Source Development Lab - * * Copyright (c) 2006 Sun Microsystems, Inc. * * All rights reserved. @@ -869,14 +868,17 @@ static struct event_svr_channel_instance *create_channel(mar_name_t *cn) */ static int check_open_size(struct event_svr_channel_instance *eci) { + struct open_count *esc_node_opens_tmp; + if (total_member_count > eci->esc_oc_size) { - eci->esc_node_opens = realloc(eci->esc_node_opens, + esc_node_opens_tmp = realloc (eci->esc_node_opens, sizeof(struct open_count) * total_member_count); - if (!eci->esc_node_opens) { + if (esc_node_opens_tmp == NULL) { log_printf(LOG_LEVEL_WARNING, "Memory error realloc of node list\n"); return -1; } + eci->esc_node_opens = esc_node_opens_tmp; memset(&eci->esc_node_opens[eci->esc_oc_size], 0, sizeof(struct open_count) * (total_member_count - eci->esc_oc_size)); @@ -1636,8 +1638,15 @@ evt_delivered(struct event_data *evt, struct event_svr_channel_open *eco) log_printf(LOG_LEVEL_DEBUG, "delivered ID %llx to eco %p\n", (unsigned long long)evt->ed_event.led_event_id, eco); if (evt->ed_delivered_count == evt->ed_delivered_next) { - evt->ed_delivered = realloc(evt->ed_delivered, + struct event_svr_channel_open *ed_delivered_tmp; + + ed_delivered_tmp = realloc (evt->ed_delivered, DELIVER_SIZE * sizeof(struct event_svr_channel_open *)); + if (ed_delivered_tmp == NULL) { + log_printf(LOG_LEVEL_WARNING, "Memory error realloc\n"); + return; + } + evt->ed_delivered = ed_delivered_tmp; memset(evt->ed_delivered + evt->ed_delivered_next, 0, DELIVER_SIZE * sizeof(struct event_svr_channel_open *)); evt->ed_delivered_next = evt->ed_delivered_count; diff --git a/exec/mainconfig.c b/exec/mainconfig.c index 8016cf45..018b5a40 100644 --- a/exec/mainconfig.c +++ b/exec/mainconfig.c @@ -204,10 +204,14 @@ int openais_main_config_read ( "logger", strlen ("logger"), &object_logger_handle) == 0) { - main_config->logger = - realloc(main_config->logger, - sizeof(struct logger_config) * - (main_config->loggers + 1)); + struct logger_config *logger_tmp; + logger_tmp = realloc (main_config->logger, + sizeof(struct logger_config) * (main_config->loggers + 1)); + if (logger_tmp == NULL) { + error_reason = "no more memory"; + goto other_error; + } + main_config->logger = logger_tmp; i = main_config->loggers; main_config->loggers++; memset(&main_config->logger[i], 0, sizeof(struct logger_config)); @@ -296,6 +300,14 @@ int openais_main_config_read ( return 0; +other_error: + sprintf (error_string_response, + "error parsing config: %s.\n", + error_reason); + + *error_string = error_string_response; + return -1; + parse_error: sprintf (error_string_response, "parse error in config: %s.\n", diff --git a/exec/totemip.c b/exec/totemip.c index 4a4f9312..c6c469a3 100644 --- a/exec/totemip.c +++ b/exec/totemip.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2005 Red Hat Inc + * Copyright (c) 2006 Sun Microsystems, Inc. * * All rights reserved. * @@ -329,14 +330,24 @@ int totemip_iface_check(struct totem_ip_address *bindnet, * Generate list of local interfaces in ifc.ifc_req structure */ id_fd = socket (AF_INET, SOCK_DGRAM, 0); - ifc.ifc_buf = 0; + ifc.ifc_buf = NULL; do { + void *ifc_buf_tmp; numreqs += 32; ifc.ifc_len = sizeof (struct ifreq) * numreqs; - ifc.ifc_buf = (void *)realloc(ifc.ifc_buf, ifc.ifc_len); + ifc_buf_tmp = realloc (ifc.ifc_buf, ifc.ifc_len); + if (ifc_buf_tmp == NULL) { + close (id_fd); + if (ifc.ifc_buf != NULL) { + free (ifc.ifc_buf); + } + return -1; + } + ifc.ifc_buf = ifc_buf_tmp; res = ioctl (id_fd, SIOCGIFCONF, &ifc); if (res < 0) { close (id_fd); + free (ifc.ifc_buf); return -1; } } while (ifc.ifc_len == sizeof (struct ifreq) * numreqs); @@ -393,7 +404,9 @@ int totemip_iface_check(struct totem_ip_address *bindnet, } } } - free (ifc.ifc_buf); + if (ifc.ifc_buf != NULL) { + free (ifc.ifc_buf); + } close (id_fd); return (res); diff --git a/exec/totempg.c b/exec/totempg.c index 0b765970..e2d21a48 100644 --- a/exec/totempg.c +++ b/exec/totempg.c @@ -950,7 +950,7 @@ int totempg_groups_join ( new_groups = realloc (instance->groups, sizeof (struct totempg_group) * (instance->groups_cnt + group_cnt)); - if (new_groups == 0) { + if (new_groups == NULL) { res = ENOMEM; goto error_exit; } diff --git a/include/hdb.h b/include/hdb.h index 7cde3d07..3c220a25 100644 --- a/include/hdb.h +++ b/include/hdb.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2002-2006 MontaVista Software, Inc. + * Copyright (c) 2006 Sun Microsystems, Inc. * * All rights reserved. * @@ -99,7 +100,7 @@ static inline int hdb_handle_create ( handle_database->handle_count += 1; new_handles = (struct hdb_handle *)realloc (handle_database->handles, sizeof (struct hdb_handle) * handle_database->handle_count); - if (new_handles == 0) { + if (new_handles == NULL) { pthread_mutex_unlock (&handle_database->mutex); return (-1); } diff --git a/lib/util.c b/lib/util.c index e72a1997..4b7733ea 100644 --- a/lib/util.c +++ b/lib/util.c @@ -2,6 +2,7 @@ * vi: set autoindent tabstop=4 shiftwidth=4 : * * Copyright (c) 2002-2006 MontaVista Software, Inc. + * Copyright (c) 2006 Sun Microsystems, Inc. * * All rights reserved. * @@ -532,7 +533,7 @@ saHandleCreate ( handleDatabase->handleCount += 1; newHandles = (struct saHandle *)realloc (handleDatabase->handles, sizeof (struct saHandle) * handleDatabase->handleCount); - if (newHandles == 0) { + if (newHandles == NULL) { pthread_mutex_unlock (&handleDatabase->mutex); return (SA_AIS_ERR_NO_MEMORY); }