Skip to content

Commit 8e14243

Browse files
committed
Don't use the result of yaml.Unmarshal after it returns an error
We calll yaml.UnmarshalWithOptions twice, to detect non-strict YAML, and overwrite the result of the first unmarshal with the second one. This seems to work, but there is no guarantee that it couldn't have left the struct in an undefined state. Also the prototype should use `*LibYAML` instead of `any` because that is the only valid type you should pass in. Signed-off-by: Jan Dubois <[email protected]>
1 parent 3c32012 commit 8e14243

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

pkg/limayaml/marshal.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ func unmarshalDisk(dst *Disk, b []byte) error {
3535
return yaml.Unmarshal(b, dst)
3636
}
3737

38-
func Unmarshal(data []byte, v any, comment string) error {
39-
if err := yaml.UnmarshalWithOptions(data, v, yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
38+
func Unmarshal(data []byte, y *LimaYAML, comment string) error {
39+
if err := yaml.UnmarshalWithOptions(data, y, yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
4040
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
4141
}
4242
// the go-yaml library doesn't catch all markup errors, unfortunately
4343
// make sure to get a "second opinion", using the same library as "yq"
4444
if err := yqutil.ValidateContent(data); err != nil {
4545
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
4646
}
47-
if err := yaml.UnmarshalWithOptions(data, v, yaml.Strict(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
47+
var ignore LimaYAML
48+
if err := yaml.UnmarshalWithOptions(data, &ignore, yaml.Strict(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
4849
logrus.WithField("comment", comment).WithError(err).Warn("Non-strict YAML detected; please check for typos")
4950
}
5051
return nil

0 commit comments

Comments
 (0)