Plugins: filter nan values (#20137)

This commit is contained in:
andig 2025-03-26 17:44:49 +01:00 • committed by GitHub
parent 0ea32cc82a
commit aeb3aba168
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 3 deletions

View file

@ -1,8 +1,6 @@
package plugin package plugin
import ( import (
"strconv"
"github.com/spf13/cast" "github.com/spf13/cast"
) )
@ -30,7 +28,7 @@ func (p *getter) FloatGetter() (func() (float64, error), error) {
return 0, err return 0, err
} }
f, err := strconv.ParseFloat(s, 64) f, err := parseFloat(s)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View file

@ -3,6 +3,8 @@ package plugin
import ( import (
"context" "context"
"fmt" "fmt"
"math"
"strconv"
"github.com/evcc-io/evcc/api" "github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util" "github.com/evcc-io/evcc/util"
@ -42,3 +44,12 @@ func contextLogger(ctx context.Context, log *util.Logger) *util.Logger {
return log return log
} }
// parseFloat rejects NaN and Inf values
func parseFloat(payload string) (float64, error) {
f, err := strconv.ParseFloat(payload, 64)
if err == nil && (math.IsNaN(f) || math.IsInf(f, 0)) {
return 0, fmt.Errorf("invalid float value: %s", payload)
}
return f, err
}