types.LoadArgs does an unsafe typecast for env args key value pairs.
This can cause a panic if the config type fields do not implement
the encoding.TextUnmarshaler interface. This commit modifies it to
do a safe type cast and return an error in such scenarios.
unknownArgs = append(unknownArgs, pair)
continue
}
-
- u := keyField.Addr().Interface().(encoding.TextUnmarshaler)
+ keyFieldIface := keyField.Addr().Interface()
+ u, ok := keyFieldIface.(encoding.TextUnmarshaler)
+ if !ok {
+ return fmt.Errorf("ARGS: '%s' cannot be unmarshalled from textual form for pair %q", keyField, pair)
+ }
err := u.UnmarshalText([]byte(valueString))
if err != nil {
return fmt.Errorf("ARGS: error parsing value of pair %q: %v)", pair, err)
Expect(err).NotTo(HaveOccurred())
})
})
+
+ Context("When known arguments are passed and cannot be unmarshalled", func() {
+ It("LoadArgs should fail", func() {
+ conf := struct {
+ IP IPNet
+ }{}
+ err := LoadArgs("IP=10.0.0.0/24", &conf)
+ Expect(err).To(HaveOccurred())
+
+ })
+ })
})