URL: https://github.com/freeipa/freeipa/pull/3976
Author: fcami
 Title: #3976: locations.py: fix divide by 0
Action: opened

PR body:
"""
If all IPA servers (or the first server) of a location have
service weights == 0, the sum of the weights is zero leading
to a division by zero in relative weight calculation.
Detect the situation and set relative weight to 0 in this case.

Fixes: https://pagure.io/freeipa/issue/8135
Signed-off-by: Fran├žois Cami <fc...@redhat.com>
"""

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/3976/head:pr3976
git checkout pr3976
From b03c56895b9fbe585f935c05de25ec14e08c5eac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= <fc...@redhat.com>
Date: Tue, 3 Dec 2019 12:05:21 +0100
Subject: [PATCH] locations.py: fix divide by 0
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If all IPA servers (or the first server) of a location have
service weights == 0, the sum of the weights is zero leading
to a division by zero in relative weight calculation.
Detect the situation and set relative weight to 0 in this case.

Fixes: https://pagure.io/freeipa/issue/8135
Signed-off-by: Fran├žois Cami <fc...@redhat.com>
---
 ipaserver/plugins/location.py | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/ipaserver/plugins/location.py b/ipaserver/plugins/location.py
index e24f7b7bc6..a12fd63178 100644
--- a/ipaserver/plugins/location.py
+++ b/ipaserver/plugins/location.py
@@ -225,10 +225,14 @@ def execute(self, *keys, **options):
                     dns_servers.append(s_name)
 
             for server in servers_additional_info.values():
-                server['service_relative_weight'] = [
-                    u'{:.1f}%'.format(
-                        int(server['ipaserviceweight'][0])*100.0/weight_sum)
-                ]
+                if weight_sum != 0:
+                    server['service_relative_weight'] = [
+                        u'{:.1f}%'.format(
+                            int(server['ipaserviceweight'][0]) * 100.0 /
+                            weight_sum)
+                    ]
+                else:
+                    server['service_relative_weight'] = 0
             if servers_name:
                 result['result']['servers_server'] = servers_name
 
_______________________________________________
FreeIPA-devel mailing list -- freeipa-devel@lists.fedorahosted.org
To unsubscribe send an email to freeipa-devel-le...@lists.fedorahosted.org
Fedora Code of Conduct: 
https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: 
https://lists.fedorahosted.org/archives/list/freeipa-devel@lists.fedorahosted.org

Reply via email to