Skip to content

Commit

Permalink
fix divide by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
babos77 committed Sep 27, 2021
1 parent 2762851 commit 19d5602
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion config/codecommunicator/ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ func (c *iosCommunicator) getMemoryComponentMemoryUsage(ctx context.Context, poo
}

// usage = ( used / (free+used) ) * 100
usage, _ := used.DivRound(used.Add(free), 4).Mul(decimal.NewFromInt(100)).Float64()
total := used.Add(free)
if total.IsZero() {
return nil, errors.New("total memory is zero, division by zero not possible")
}

usage, _ := used.DivRound(total, 4).Mul(decimal.NewFromInt(100)).Float64()

pools = append(pools, device.MemoryPool{
Label: &poolLabel,
Expand Down
4 changes: 4 additions & 0 deletions internal/deviceclass/property/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,10 @@ func (m *divideNumberModifier) modify(ctx context.Context, v value.Value) (value
return nil, err
}

if b.IsZero() {
return nil, errors.New("divisor is zero, division by zero not possible")
}

result := a.DivRound(b, m.precision)
return value.New(result), nil
}
Expand Down

0 comments on commit 19d5602

Please sign in to comment.