From 15f86e54c97ef5971642a2c75a2b2d9b60b53cfc Mon Sep 17 00:00:00 2001 From: Matthieu Patou Date: Thu, 9 Nov 2023 18:20:39 -0800 Subject: [PATCH] Ensure that we are dereferencing elements that exists Summary We were derefencing variables that could be nil. For instance if the query don't return an error but just no lines then doing err.Error() was causing a nil/null derefencing. Similarly if the field array in the frame was has only one element doing ``` frame.Fields[1].Len() ``` Would result in a crash because we would derefence a nil pointer. We are fixing this here. Also we are returning a nicer message when the query results are empty. Testing Used a query that is returning no line, before the change this would crash the plug-in after the change it's printing a nice(r) message about the fact that the query is not returning lines. --- pkg/rockset.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/rockset.go b/pkg/rockset.go index 2ad0c45..d02697e 100644 --- a/pkg/rockset.go +++ b/pkg/rockset.go @@ -145,7 +145,12 @@ func (d *Datasource) query(ctx context.Context, rs *rockset.RockClient, query ba log.DefaultLogger.Error("query error", "error", errMessage) return backend.ErrDataResponse(statusCode, errMessage) } - log.DefaultLogger.Info("query response", "elapsedTime", qr.Stats.ElapsedTimeMs, "results", len(qr.Results)) + log.DefaultLogger.Info("query response", "elapsedTime", qr.Stats.ElapsedTimeMs, "# of results", len(qr.Results)) + + if len(qr.Results) == 0 { + errMsg := "Query returned no lines" + return backend.ErrDataResponse(backend.StatusValidationFailed, errMsg) + } labelValues, err := generateLabelValues(qm.QueryLabelColumn, qr.Results) if err != nil { @@ -171,14 +176,19 @@ func (d *Datasource) query(ctx context.Context, rs *rockset.RockClient, query ba } // only add the frame if it contains any useful data - if frame.Fields[1].Len() > 0 { + if len(frame.Fields) > 1 && frame.Fields[1].Len() > 0 { response.Frames = append(response.Frames, frame) } } } if len(response.Frames) == 0 { - errMsg := fmt.Sprintf("no usable columns found in query response: %s", err.Error()) + var errMsg string + if err == nil { + errMsg = "no usable columns found in query response" + } else { + errMsg = fmt.Sprintf("no usable columns found in query response: %s", err.Error()) + } return backend.ErrDataResponse(backend.StatusValidationFailed, errMsg) }