From 4cc9cca3d61b7d75adbe32c6de7a47529b3ad58e Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Wed, 25 Oct 2023 07:01:10 +0300 Subject: [PATCH 24/24] Fix reqtree.c:improve() clang analyzer warning Clang analyzer was warning that crossings[layer] could be garbage. Refactored the code a bit, so analyzer sees that value is never garbage. See osdn #48904 Signed-off-by: Marko Lindqvist --- client/reqtree.c | 49 ++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/client/reqtree.c b/client/reqtree.c index 7418c6110b..fa4f1490a3 100644 --- a/client/reqtree.c +++ b/client/reqtree.c @@ -783,47 +783,48 @@ static void swap(struct reqtree *tree, int layer, int order1, int order2) *************************************************************************/ static void improve(struct reqtree *tree) { - int crossings[tree->num_layers - 1]; + int layers = tree->num_layers; + int crossings[layers - 1]; int i, x1, x2, layer; - for (i = 0; i < tree->num_layers - 1; i++) { + for (i = 0; i < layers - 1; i++) { crossings[i] = count_crossings(tree, i); } - for (layer = 0; layer < tree->num_layers; layer++) { + for (layer = 0; layer < layers; layer++) { int layer_size = tree->layer_size[layer]; int layer_sum = 0; if (layer > 0) { layer_sum += crossings[layer - 1]; } - if (layer < tree->num_layers - 1) { + if (layer < layers - 1) { layer_sum += crossings[layer]; } for (x1 = 0; x1 < layer_size; x1++) { for (x2 = x1 + 1; x2 < layer_size; x2++) { - int new_crossings = 0; - int new_crossings_before = 0; + int new_crossings = 0; + int new_crossings_before = 0; - swap(tree, layer, x1, x2); - if (layer > 0) { - new_crossings_before += count_crossings(tree, layer - 1); - } - if (layer < tree->num_layers - 1) { - new_crossings += count_crossings(tree, layer); - } - if (new_crossings + new_crossings_before > layer_sum) { - swap(tree, layer, x1, x2); - } else { - layer_sum = new_crossings + new_crossings_before; - if (layer > 0) { - crossings[layer - 1] = new_crossings_before; - } - if (layer < tree->num_layers - 1) { - crossings[layer] = new_crossings; - } - } + swap(tree, layer, x1, x2); + if (layer > 0) { + new_crossings_before += count_crossings(tree, layer - 1); + } + if (layer < layers - 1) { + new_crossings += count_crossings(tree, layer); + } + if (new_crossings + new_crossings_before > layer_sum) { + swap(tree, layer, x1, x2); + } else { + layer_sum = new_crossings + new_crossings_before; + if (layer > 0) { + crossings[layer - 1] = new_crossings_before; + } + if (layer < layers - 1) { + crossings[layer] = new_crossings; + } + } } } } -- 2.42.0