Skip to content

v1.16.0

@vidigoat vidigoat tagged this 15 Jul 09:33
When a resolver returns an integer for a `float32`/`float64` flag,
`floatDecoder` handled the integer case with
`target.Set(reflect.ValueOf(v))`. Since an `int` is not assignable to a
float field, this panics and crashes the whole program:

```
panic: reflect.Set: value of type int is not assignable to type float64
```

This is easy to hit because resolvers return `any` and typed config
sources commonly yield integers for numeric fields.

### Reproduction

```go
var cli struct {
	Rate float64
}
resolver := kong.ResolverFunc(func(_ *kong.Context, _ *kong.Path, flag *kong.Flag) (any, error) {
	if flag.Name == "rate" {
		return 5, nil // int, not float
	}
	return nil, nil
})
kong.Must(&cli, kong.Resolvers(resolver)).Parse(nil) // panics
```

### Fix

Convert the integer to a float and use `SetFloat`, matching the other
branches in `floatDecoder` and the existing pattern in
`durationDecoder`. The `default` branch error message was also a copy
from `intDecoder` ("expected an int") and is corrected to "expected a
float".

Added a regression test (`TestResolverWithFloatFromInt`) that panics
before the change and passes after. Full suite passes.
Assets 2
Loading