From 251d504d40ed9111f7ef62e116e810f62c050966 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 23 Apr 2014 13:23:58 +0300 Subject: [PATCH] fix for callcost out of bounds merge (thanks DigiDaz) --- engine/callcost.go | 2 +- engine/callcost_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/engine/callcost.go b/engine/callcost.go index dff4be5c5..3dd1a4aab 100644 --- a/engine/callcost.go +++ b/engine/callcost.go @@ -46,7 +46,7 @@ type CallCost struct { // Merges the received timespan if they are similar (same activation period, same interval, same minute info. func (cc *CallCost) Merge(other *CallCost) { - if len(cc.Timespans)-1 < 0 { + if len(cc.Timespans)-1 < 0 || len(other.Timespans) == 0 { return } ts := cc.Timespans[len(cc.Timespans)-1] diff --git a/engine/callcost_test.go b/engine/callcost_test.go index 0b9919d0e..5b8fa78e2 100644 --- a/engine/callcost_test.go +++ b/engine/callcost_test.go @@ -125,6 +125,18 @@ func TestMultipleInputRightMerge(t *testing.T) { } } +func TestCallCostMergeEmpty(t *testing.T) { + t1 := time.Date(2012, time.February, 2, 17, 58, 0, 0, time.UTC) + t2 := time.Date(2012, time.February, 2, 17, 59, 0, 0, time.UTC) + cd := &CallDescriptor{Direction: OUTBOUND, TOR: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} + cc1, _ := cd.GetCost() + cc2 := &CallCost{} + cc1.Merge(cc2) + if len(cc1.Timespans) != 1 { + t.Error("Error mergin empty call cost: ", len(cc1.Timespans)) + } +} + func TestCallCostGetDurationZero(t *testing.T) { cc := &CallCost{} if cc.GetDuration().Seconds() != 0 {