diff mbox series

[11/34] kernelshark: Prevent potential divide by zero in Shape::center()

Message ID 20240114171723.14092-12-dev@benjarobin.fr (mailing list archive)
State New
Headers show
Series Fix kernelshark issues introduced by the migration to Qt6 | expand

Commit Message

Benjamin ROBIN Jan. 14, 2024, 5:17 p.m. UTC
Signed-off-by: Benjamin ROBIN <dev@benjarobin.fr>
---
 src/KsPlotTools.cpp | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

Comments

Yordan Karadzhov Jan. 21, 2024, 7:49 p.m. UTC | #1
On 1/14/24 19:17, Benjamin ROBIN wrote:
> Signed-off-by: Benjamin ROBIN <dev@benjarobin.fr>
> ---
>   src/KsPlotTools.cpp | 14 ++++++++------
>   1 file changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/src/KsPlotTools.cpp b/src/KsPlotTools.cpp
> index 1d63a9b..8011329 100644
> --- a/src/KsPlotTools.cpp
> +++ b/src/KsPlotTools.cpp
> @@ -318,13 +318,15 @@ ksplot_point Shape::center() const
>   {
>   	ksplot_point c = {0, 0};
>   
> -	for (size_t i = 0; i < _nPoints; ++i) {
> -		c.x += _points[i].x;
> -		c.y += _points[i].y;
> -	}
> +	if (_nPoints > 0) {

Can we just have

if (_nPoints == 0)
	return c;


Thanks,
Y.

> +		for (size_t i = 0; i < _nPoints; ++i) {
> +			c.x += _points[i].x;
> +			c.y += _points[i].y;
> +		}
>   
> -	c.x /= _nPoints;
> -	c.y /= _nPoints;
> +		c.x /= _nPoints;
> +		c.y /= _nPoints;
> +	}
>   
>   	return c;
>   }
diff mbox series

Patch

diff --git a/src/KsPlotTools.cpp b/src/KsPlotTools.cpp
index 1d63a9b..8011329 100644
--- a/src/KsPlotTools.cpp
+++ b/src/KsPlotTools.cpp
@@ -318,13 +318,15 @@  ksplot_point Shape::center() const
 {
 	ksplot_point c = {0, 0};
 
-	for (size_t i = 0; i < _nPoints; ++i) {
-		c.x += _points[i].x;
-		c.y += _points[i].y;
-	}
+	if (_nPoints > 0) {
+		for (size_t i = 0; i < _nPoints; ++i) {
+			c.x += _points[i].x;
+			c.y += _points[i].y;
+		}
 
-	c.x /= _nPoints;
-	c.y /= _nPoints;
+		c.x /= _nPoints;
+		c.y /= _nPoints;
+	}
 
 	return c;
 }