diff --git a/cts/lxc_autogen.sh.in b/cts/lxc_autogen.sh.in index 8c97fc11498..e8984657783 100755 --- a/cts/lxc_autogen.sh.in +++ b/cts/lxc_autogen.sh.in @@ -175,7 +175,7 @@ generate() - + diff --git a/include/crm/common/util.h b/include/crm/common/util.h index 9092708af5f..b07a5c11bdd 100644 --- a/include/crm/common/util.h +++ b/include/crm/common/util.h @@ -50,6 +50,7 @@ /* Status of an offline client */ # endif +char *crm_itoa_stack(int an_int, char *buf, size_t len); char *crm_itoa(int an_int); gboolean crm_is_true(const char *s); int crm_str_to_boolean(const char *s, int *ret); @@ -58,6 +59,7 @@ long long crm_get_msec(const char *input); unsigned long long crm_get_interval(const char *input); int char2score(const char *score); char *score2char(int score); +char *score2char_stack(int score, char *buf, size_t len); int compare_version(const char *version1, const char *version2); diff --git a/lib/common/utils.c b/lib/common/utils.c index ac91a683888..93dcceb0848 100644 --- a/lib/common/utils.c +++ b/lib/common/utils.c @@ -215,6 +215,20 @@ char2score(const char *score) return score_f; } +char * +score2char_stack(int score, char *buf, size_t len) +{ + if (score >= node_score_infinity) { + strncpy(buf, INFINITY_S, 9); + } else if (score <= -node_score_infinity) { + strncpy(buf, MINUS_INFINITY_S , 10); + } else { + return crm_itoa_stack(score, buf, len); + } + + return buf; +} + char * score2char(int score) { @@ -366,6 +380,17 @@ generate_hash_key(const char *crm_msg_reference, const char *sys) return hash_key; } + +char * +crm_itoa_stack(int an_int, char *buffer, size_t len) +{ + if (buffer != NULL) { + snprintf(buffer, len, "%d", an_int); + } + + return buffer; +} + char * crm_itoa(int an_int) { diff --git a/lib/pengine/utils.c b/lib/pengine/utils.c index 5772aaab102..1a46b29fb87 100644 --- a/lib/pengine/utils.c +++ b/lib/pengine/utils.c @@ -186,6 +186,8 @@ dump_node_scores_worker(int level, const char *file, const char *function, int l } if (level == 0) { + char score[128]; + int len = sizeof(score); /* For now we want this in sorted order to keep the regression tests happy */ GListPtr gIter = NULL; GListPtr list = g_hash_table_get_values(hash); @@ -195,7 +197,8 @@ dump_node_scores_worker(int level, const char *file, const char *function, int l gIter = list; for (; gIter != NULL; gIter = gIter->next) { node_t *node = (node_t *) gIter->data; - char *score = score2char(node->weight); + /* This function is called a whole lot, use stack allocated score */ + score2char_stack(node->weight, score, len); if (rsc) { printf("%s: %s allocation score on %s: %s\n", @@ -203,15 +206,17 @@ dump_node_scores_worker(int level, const char *file, const char *function, int l } else { printf("%s: %s = %s\n", comment, node->details->uname, score); } - free(score); } g_list_free(list); } else if (hash) { + char score[128]; + int len = sizeof(score); g_hash_table_iter_init(&iter, hash); while (g_hash_table_iter_next(&iter, NULL, (void **)&node)) { - char *score = score2char(node->weight); + /* This function is called a whole lot, use stack allocated score */ + score2char_stack(node->weight, score, len); if (rsc) { do_crm_log_alias(LOG_TRACE, file, function, line, @@ -221,7 +226,6 @@ dump_node_scores_worker(int level, const char *file, const char *function, int l do_crm_log_alias(LOG_TRACE, file, function, line + 1, "%s: %s = %s", comment, node->details->uname, score); } - free(score); } } diff --git a/pengine/master.c b/pengine/master.c index 9d5e2fb59a5..37ed805826e 100644 --- a/pengine/master.c +++ b/pengine/master.c @@ -277,6 +277,8 @@ master_promotion_order(resource_t * rsc, pe_working_set_t * data_set) node_t *node = NULL; node_t *chosen = NULL; clone_variant_data_t *clone_data = NULL; + char score[33]; + size_t len = sizeof(score); get_clone_variant_data(clone_data, rsc); @@ -298,7 +300,6 @@ master_promotion_order(resource_t * rsc, pe_working_set_t * data_set) gIter = rsc->children; for (; gIter != NULL; gIter = gIter->next) { resource_t *child = (resource_t *) gIter->data; - char *score = NULL; chosen = child->fns->location(child, NULL, FALSE); if (chosen == NULL || child->sort_index < 0) { @@ -309,10 +310,9 @@ master_promotion_order(resource_t * rsc, pe_working_set_t * data_set) node = (node_t *) pe_hash_table_lookup(rsc->allowed_nodes, chosen->details->id); CRM_ASSERT(node != NULL); /* adds in master preferences and rsc_location.role=Master */ - score = score2char(child->sort_index); + score2char_stack(child->sort_index, score, len); pe_rsc_trace(rsc, "Adding %s to %s from %s", score, node->details->uname, child->id); - free(score); node->weight = merge_weights(child->sort_index, node->weight); } @@ -638,6 +638,9 @@ master_color(resource_t * rsc, node_t * prefer, pe_working_set_t * data_set) node_t *cons_node = NULL; enum rsc_role_e next_role = RSC_ROLE_UNKNOWN; + char score[33]; + size_t len = sizeof(score); + clone_variant_data_t *clone_data = NULL; get_clone_variant_data(clone_data, rsc); @@ -742,7 +745,7 @@ master_color(resource_t * rsc, node_t * prefer, pe_working_set_t * data_set) gIter = rsc->children; for (; gIter != NULL; gIter = gIter->next) { resource_t *child_rsc = (resource_t *) gIter->data; - char *score = score2char(child_rsc->sort_index); + score2char_stack(child_rsc->sort_index, score, len); chosen = child_rsc->fns->location(child_rsc, NULL, FALSE); if (show_scores) { @@ -753,7 +756,6 @@ master_color(resource_t * rsc, node_t * prefer, pe_working_set_t * data_set) do_crm_log(scores_log_level, "%s promotion score on %s: %s", child_rsc->id, chosen ? chosen->details->uname : "none", score); } - free(score); chosen = NULL; /* nuke 'chosen' so that we don't promote more than the * required number of instances diff --git a/pengine/native.c b/pengine/native.c index 8fdebf441d5..87d6dc91a54 100644 --- a/pengine/native.c +++ b/pengine/native.c @@ -213,7 +213,9 @@ native_choose_node(resource_t * rsc, node_t * prefer, pe_working_set_t * data_se if (multiple > 1) { int log_level = LOG_INFO; - char *score = score2char(chosen->weight); + static char score[33]; + + score2char_stack(chosen->weight, score, sizeof(score)); if (chosen->weight >= INFINITY) { log_level = LOG_WARNING; @@ -222,7 +224,6 @@ native_choose_node(resource_t * rsc, node_t * prefer, pe_working_set_t * data_se do_crm_log(log_level, "%d nodes with equal score (%s) for" " running %s resources. Chose %s.", multiple, score, rsc->id, chosen->details->uname); - free(score); } result = native_assign_node(rsc, nodes, chosen, FALSE); @@ -1637,11 +1638,12 @@ colocation_match(resource_t * rsc_lh, resource_t * rsc_rh, rsc_colocation_t * co work = NULL; } else { - char *score = score2char(constraint->score); + static char score[33]; + + score2char_stack(constraint->score, score, sizeof(score)); pe_rsc_info(rsc_lh, "%s: Rolling back scores from %s (%d, %s)", rsc_lh->id, rsc_rh->id, do_check, score); - free(score); } if (work) {