From 3ccb1d54810b6266d8a3908bdef62aeafa7a0f24 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 24 Apr 2022 13:25:54 +0300 Subject: [PATCH 48/48] count_*_near_tile(): Fix clang analyzer div by zero warning See osdn #44449 Signed-off-by: Marko Lindqvist --- common/road.c | 6 ++++-- common/terrain.c | 27 +++++++++++++++------------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/common/road.c b/common/road.c index da7514f41e..e7982b2a51 100644 --- a/common/road.c +++ b/common/road.c @@ -343,9 +343,10 @@ int count_river_type_tile_card(const struct tile *ptile, total++; } cardinal_adjc_iterate_end; - if (percentage) { + if (percentage && count > 0) { /* Latter condition avoids div by zero */ count = count * 100 / total; } + return count; } @@ -368,9 +369,10 @@ int count_river_type_near_tile(const struct tile *ptile, total++; } adjc_iterate_end; - if (percentage) { + if (percentage && count > 0) { /* Latter condition avoids div by zero */ count = count * 100 / total; } + return count; } diff --git a/common/terrain.c b/common/terrain.c index 162a3151b6..8c34e6988e 100644 --- a/common/terrain.c +++ b/common/terrain.c @@ -358,8 +358,8 @@ bool is_terrain_near_tile(const struct tile *ptile, Return the number of adjacent tiles that have the given terrain. **************************************************************************/ int count_terrain_near_tile(const struct tile *ptile, - bool cardinal_only, bool percentage, - const struct terrain *pterrain) + bool cardinal_only, bool percentage, + const struct terrain *pterrain) { int count = 0, total = 0; @@ -370,9 +370,10 @@ int count_terrain_near_tile(const struct tile *ptile, total++; } variable_adjc_iterate_end; - if (percentage) { + if (percentage && count > 0) { /* Latter condition avoids div by zero */ count = count * 100 / total; } + return count; } @@ -380,8 +381,8 @@ int count_terrain_near_tile(const struct tile *ptile, Return the number of adjacent tiles that have the given terrain property. **************************************************************************/ int count_terrain_property_near_tile(const struct tile *ptile, - bool cardinal_only, bool percentage, - enum mapgen_terrain_property prop) + bool cardinal_only, bool percentage, + enum mapgen_terrain_property prop) { int count = 0, total = 0; @@ -394,9 +395,10 @@ int count_terrain_property_near_tile(const struct tile *ptile, total++; } variable_adjc_iterate_end; - if (percentage) { + if (percentage && count > 0) { /* Latter condition avoids div by zero */ count = count * 100 / total; } + return count; } @@ -445,7 +447,7 @@ bool is_resource_near_tile(const struct tile *ptile, given flag (does not check ptile itself). **************************************************************************/ bool is_terrain_flag_card_near(const struct tile *ptile, - enum terrain_flag_id flag) + enum terrain_flag_id flag) { cardinal_adjc_iterate(&(wld.map), ptile, adjc_tile) { struct terrain* pterrain = tile_terrain(adjc_tile); @@ -464,7 +466,7 @@ bool is_terrain_flag_card_near(const struct tile *ptile, (does not check ptile itself). **************************************************************************/ bool is_terrain_flag_near_tile(const struct tile *ptile, - enum terrain_flag_id flag) + enum terrain_flag_id flag) { adjc_iterate(&(wld.map), ptile, adjc_tile) { struct terrain* pterrain = tile_terrain(adjc_tile); @@ -483,8 +485,8 @@ bool is_terrain_flag_near_tile(const struct tile *ptile, (not including ptile itself). **************************************************************************/ int count_terrain_flag_near_tile(const struct tile *ptile, - bool cardinal_only, bool percentage, - enum terrain_flag_id flag) + bool cardinal_only, bool percentage, + enum terrain_flag_id flag) { int count = 0, total = 0; @@ -498,9 +500,10 @@ int count_terrain_flag_near_tile(const struct tile *ptile, total++; } variable_adjc_iterate_end; - if (percentage) { + if (percentage && count > 0) { /* Latter condition avoids div by zero */ count = count * 100 / total; } + return count; } @@ -649,7 +652,7 @@ int count_terrain_class_near_tile(const struct tile *ptile, total++; } variable_adjc_iterate_end; - if (percentage) { + if (percentage && count > 0) { /* Latter condition avoids div by zero */ count = count * 100 / total; } -- 2.35.1