From 61a13a3ed7b5a1d359af3ab1f8f99845e701aa53 Mon Sep 17 00:00:00 2001 From: vyzo Date: Wed, 22 Apr 2020 21:25:28 +0300 Subject: [PATCH] validate that TimeInMeshQuantum is non-zero otherwise we get a division by zero --- score_params.go | 3 +++ score_params_test.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/score_params.go b/score_params.go index 65a1919..4d2ffff 100644 --- a/score_params.go +++ b/score_params.go @@ -179,6 +179,9 @@ func (p *TopicScoreParams) validate() error { } // check P1 + if p.TimeInMeshQuantum == 0 { + return fmt.Errorf("invalid TimeInMeshQuantum; must be non zero") + } if p.TimeInMeshWeight < 0 { return fmt.Errorf("invalid TimeInMeshWeight; must be positive (or 0 to disable)") } diff --git a/score_params_test.go b/score_params_test.go index f481863..66482d2 100644 --- a/score_params_test.go +++ b/score_params_test.go @@ -32,6 +32,10 @@ func TestPeerScoreThresholdsValidation(t *testing.T) { } func TestTopicScoreParamsValidation(t *testing.T) { + if (&TopicScoreParams{}).validate() == nil { + t.Fatal("expected validation error") + } + if (&TopicScoreParams{TopicWeight: -1}).validate() == nil { t.Fatal("expected validation error") }