Skip to content

Commit 304a9d9

Browse files
authored
fix: avoid crash due to fallthrough in *ConfFloat64 nil comparisons (#1277)
1 parent 9ff418d commit 304a9d9

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

data/frame.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,23 +347,19 @@ func FrameTestCompareOptions() []cmp.Option {
347347
if math.IsNaN(float64(*x)) {
348348
return true
349349
}
350-
if math.IsInf(float64(*x), 1) {
351-
return true
352-
}
353-
if math.IsInf(float64(*x), -1) {
350+
if math.IsInf(float64(*x), 0) {
354351
return true
355352
}
353+
return false
356354
}
357355
if x == nil {
358356
if math.IsNaN(float64(*y)) {
359357
return true
360358
}
361-
if math.IsInf(float64(*y), 1) {
362-
return true
363-
}
364-
if math.IsInf(float64(*y), -1) {
359+
if math.IsInf(float64(*y), 0) {
365360
return true
366361
}
362+
return false
367363
}
368364
return *x == *y
369365
})

data/frame_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,54 @@ func TestDataFrameFilterRowsByField(t *testing.T) {
536536
}
537537
}
538538

539+
func TestFrameTestCompareOptionsForConfFloat64(t *testing.T) {
540+
var f1 data.ConfFloat64 = 1.23456789
541+
var f2 data.ConfFloat64 = 2.3456789
542+
var nan = data.ConfFloat64(math.NaN())
543+
var posInf = data.ConfFloat64(math.Inf(1))
544+
var negInf = data.ConfFloat64(math.Inf(-1))
545+
var pnil *data.ConfFloat64
546+
t.Run("ConfFloat64 should compare without crashing", func(t *testing.T) {
547+
if diff := cmp.Diff(f1, f1, data.FrameTestCompareOptions()...); diff != "" {
548+
t.Fatal(diff)
549+
}
550+
if diff := cmp.Diff(f1, f2, data.FrameTestCompareOptions()...); diff == "" {
551+
t.Fatal("Expecting diff, got nothing")
552+
}
553+
})
554+
t.Run("*ConfFloat64 should compare without crashing", func(t *testing.T) {
555+
if diff := cmp.Diff(&f1, &f1, data.FrameTestCompareOptions()...); diff != "" {
556+
t.Fatal(diff)
557+
}
558+
if diff := cmp.Diff(&f1, &f2, data.FrameTestCompareOptions()...); diff == "" {
559+
t.Fatal("Expecting diff, got nothing")
560+
}
561+
})
562+
t.Run("nil *ConfFloat64 should compare without crashing", func(t *testing.T) {
563+
if diff := cmp.Diff(pnil, pnil, data.FrameTestCompareOptions()...); diff != "" {
564+
t.Fatal(diff)
565+
}
566+
})
567+
t.Run("nil and non-nil *ConfFloat64 should compare without crashing", func(t *testing.T) {
568+
if diff := cmp.Diff(&f1, pnil, data.FrameTestCompareOptions()...); diff == "" {
569+
t.Fatal("Expecting diff, got nothing")
570+
}
571+
if diff := cmp.Diff(pnil, &f1, data.FrameTestCompareOptions()...); diff == "" {
572+
t.Fatal("Expecting diff, got nothing")
573+
}
574+
})
575+
t.Run("nil *ConfFloat64 is equivalent to NaN/+Inf/-Inf", func(t *testing.T) {
576+
for _, p := range []*data.ConfFloat64{&nan, &posInf, &negInf} {
577+
if diff := cmp.Diff(pnil, p, data.FrameTestCompareOptions()...); diff != "" {
578+
t.Fatal(diff)
579+
}
580+
if diff := cmp.Diff(p, pnil, data.FrameTestCompareOptions()...); diff != "" {
581+
t.Fatal(diff)
582+
}
583+
}
584+
})
585+
}
586+
539587
func TestJSON(t *testing.T) {
540588
frames := data.Frames{
541589
data.NewFrame("http_requests_total",

0 commit comments

Comments
 (0)