From 017e65cd9959988750cda0eaf8842196379b298d Mon Sep 17 00:00:00 2001 From: Seebs Date: Fri, 10 Apr 2020 13:24:08 -0500 Subject: [PATCH] Return empty rows for impossible ranges If the high end of a range is below the low end of the range, there's no values in it, so we can short-circuit that. If we don't, if the low end is zero or higher, and the high end is below zero, we can get very surprising behaviors, such as accepting values up to the inverse of the high end. Add a test case for this and treat it the same as a low range end above the field's maximum or a high end below the field's minimum, returning an empty row immediately. --- field.go | 2 +- field_internal_test.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/field.go b/field.go index 247c8a677..6f805256f 100644 --- a/field.go +++ b/field.go @@ -2233,7 +2233,7 @@ func (b *bsiGroup) baseValue(op pql.Token, value int64) (baseValue int64, outOfR // baseValueBetween adjusts the min/max value to align with the range for Field. func (b *bsiGroup) baseValueBetween(lo, hi int64) (baseValueLo, baseValueHi int64, outOfRange bool) { min, max := b.bitDepthMin(), b.bitDepthMax() - if hi < min || lo > max { + if hi < min || lo > max || hi < lo { return 0, 0, true } diff --git a/field_internal_test.go b/field_internal_test.go index ee13a269c..bb51a44e1 100644 --- a/field_internal_test.go +++ b/field_internal_test.go @@ -144,6 +144,7 @@ func TestBSIGroup_BaseValue(t *testing.T) { {b1, 5, 20, 5, 20, false}, {b1, 20, 1005, 20, 255, false}, {b1, 1005, 2000, 0, 0, true}, + {b1, 0, -1, 0, 0, true}, {b2, 5, 95, -95, -5, false}, {b2, 95, 120, -5, 20, false},