From 9e024c3ff0e6d6feb094c66c6897553a5e1ea6c1 Mon Sep 17 00:00:00 2001 From: Patrick Pacher Date: Thu, 21 Sep 2023 14:20:23 +0200 Subject: [PATCH] Fix panic in toSQLConditionClause when no values are provided --- netquery/query.go | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/netquery/query.go b/netquery/query.go index 804f85fbc..f62cd958b 100644 --- a/netquery/query.go +++ b/netquery/query.go @@ -341,25 +341,28 @@ func (match Matcher) toSQLConditionClause(ctx context.Context, suffix string, co params[uniqKey] = encodedValue } - // NOTE(ppacher): for now we assume that the type of each element of values - // is the same. We also can be sure that there is always at least one value. - // - // TODO(ppacher): if we start supporting values of different types here - // we need to revisit the whole behavior as we might need to do more boolean - // expression nesting to support that. - kind := orm.NormalizeKind(reflect.TypeOf(values[0]).Kind()) - isNumber := slices.Contains([]reflect.Kind{ - reflect.Uint, - reflect.Int, - reflect.Float64, - }, kind) - - // if this is a time column that is stored in "text" format and the provided - // value is a number type, we need to wrap the property in a strftime() method - // call. nameStmt := colDef.Name - if colDef.IsTime && colDef.Type == sqlite.TypeText && isNumber { - nameStmt = fmt.Sprintf("strftime('%%s', %s)+0", nameStmt) + + if len(values) > 0 { + // NOTE(ppacher): for now we assume that the type of each element of values + // is the same. We also can be sure that there is always at least one value. + // + // TODO(ppacher): if we start supporting values of different types here + // we need to revisit the whole behavior as we might need to do more boolean + // expression nesting to support that. + kind := orm.NormalizeKind(reflect.TypeOf(values[0]).Kind()) + isNumber := slices.Contains([]reflect.Kind{ + reflect.Uint, + reflect.Int, + reflect.Float64, + }, kind) + + // if this is a time column that is stored in "text" format and the provided + // value is a number type, we need to wrap the property in a strftime() method + // call. + if colDef.IsTime && colDef.Type == sqlite.TypeText && isNumber { + nameStmt = fmt.Sprintf("strftime('%%s', %s)+0", nameStmt) + } } if len(placeholder) == 1 && !list {