From 530aa0c2c72b303cdf2d46cd6ffb1bcaa2f295be Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Tue, 10 Dec 2024 08:02:25 -0500 Subject: [PATCH] Chore: use strconv.ParseInt() with 32bit when parsing 32bit numbers (#10201) * Chore: use strconv.ParseInt() with 32bit when parsing ingester ID Signed-off-by: Marco Pracucci * Address parsing in NewMetadataHandler() Signed-off-by: Marco Pracucci * Fix linter issue Signed-off-by: Marco Pracucci * Remove noop casting Signed-off-by: Marco Pracucci --------- Signed-off-by: Marco Pracucci --- pkg/querier/metadata_handler.go | 26 ++++++++++++++++---------- pkg/storage/ingest/util.go | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/pkg/querier/metadata_handler.go b/pkg/querier/metadata_handler.go index ba6f8178b6f..1a56f01910a 100644 --- a/pkg/querier/metadata_handler.go +++ b/pkg/querier/metadata_handler.go @@ -48,28 +48,34 @@ type metadataErrorResult struct { // Mimir for a given tenant. It is kept and returned as a set. func NewMetadataHandler(m MetadataSupplier) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - limit := -1 + limit := int32(-1) if s := r.FormValue("limit"); s != "" { - var err error - if limit, err = strconv.Atoi(s); err != nil { + parsed, err := strconv.ParseInt(s, 10, 32) + if err != nil { w.WriteHeader(http.StatusBadRequest) util.WriteJSONResponse(w, metadataErrorResult{Status: statusError, Error: "limit must be a number"}) return } + + limit = int32(parsed) } - limitPerMetric := -1 + + limitPerMetric := int32(-1) if s := r.FormValue("limit_per_metric"); s != "" { - var err error - if limitPerMetric, err = strconv.Atoi(s); err != nil { + parsed, err := strconv.ParseInt(s, 10, 32) + if err != nil { w.WriteHeader(http.StatusBadRequest) util.WriteJSONResponse(w, metadataErrorResult{Status: statusError, Error: "limit_per_metric must be a number"}) return } + + limitPerMetric = int32(parsed) } + metric := r.FormValue("metric") req := &client.MetricsMetadataRequest{ - Limit: int32(limit), - LimitPerMetric: int32(limitPerMetric), + Limit: limit, + LimitPerMetric: limitPerMetric, Metric: metric, } @@ -87,11 +93,11 @@ func NewMetadataHandler(m MetadataSupplier) http.Handler { // We enforce this both here and in the ingesters. Doing it in the ingesters is // more efficient as it is earlier in the process, but since that one is per user, // we still need to do it here after all the results are merged. - if limitPerMetric > 0 && len(ms) >= limitPerMetric { + if limitPerMetric > 0 && len(ms) >= int(limitPerMetric) { continue } if !ok { - if limit >= 0 && len(metrics) >= limit { + if limit >= 0 && len(metrics) >= int(limit) { break } // Most metrics will only hold 1 copy of the same metadata. diff --git a/pkg/storage/ingest/util.go b/pkg/storage/ingest/util.go index 78ed565d0ca..f0387f6eca3 100644 --- a/pkg/storage/ingest/util.go +++ b/pkg/storage/ingest/util.go @@ -36,7 +36,7 @@ func IngesterPartitionID(ingesterID string) (int32, error) { } // Parse the ingester sequence number. - ingesterSeq, err := strconv.Atoi(match[1]) + ingesterSeq, err := strconv.ParseInt(match[1], 10, 32) if err != nil { return 0, fmt.Errorf("no ingester sequence number in ingester ID %s", ingesterID) }