diff --git a/boot/piboot/piboot.go b/boot/piboot/piboot.go
index 7bfc9ee37dd4d47dcae871413f318cda3ac64935..13900edbc72597031533ddd4211b3ef098d81036 100644
--- a/boot/piboot/piboot.go
+++ b/boot/piboot/piboot.go
@@ -148,7 +148,7 @@ func (pi *PiBoot) QueryActive() (slot boot.Slot, err error) {
 	// Find the configured kernel and kernel cmdline
 	var kernel, initramfs, cmdline string
 
-	err = visitSections(&configTxt, pi.FilterPredicate, func(sect *picfg.Section) {
+	err = visitSections(&configTxt, pi.FilterPredicate, func(sect *picfg.Section) error {
 		for _, param := range sect.Parameters {
 			switch param.Name {
 			case kernelParameter:
@@ -159,6 +159,7 @@ func (pi *PiBoot) QueryActive() (slot boot.Slot, err error) {
 				cmdline = param.Value
 			}
 		}
+		return nil
 	})
 	if err != nil {
 		return boot.InvalidSlot, &UnknownActiveSlotError{Err: err}
@@ -218,7 +219,7 @@ func (pi *PiBoot) TrySwitch(to boot.Slot) (err error) {
 	}
 
 	// Find the values to modify, swapping A and B around
-	err = visitSections(&configTxt, pi.FilterPredicate, func(sect *picfg.Section) {
+	err = visitSections(&configTxt, pi.FilterPredicate, func(sect *picfg.Section) error {
 		for paramIdx, param := range sect.Parameters {
 			switch param.Name {
 			case kernelParameter, cmdlineParameter, initramfsParameter:
@@ -236,6 +237,7 @@ func (pi *PiBoot) TrySwitch(to boot.Slot) (err error) {
 				sect.Parameters[paramIdx].Value = strings.Replace(param.Value, oldPrefix, newPrefix, -1)
 			}
 		}
+		return nil
 	})
 	if err != nil {
 		return err
@@ -296,7 +298,7 @@ func (pi *PiBoot) CancelSwitch() error {
 // An optional predicate function may be used to visit only matching sections.
 // The predicate is called with the effective fiters that are in effect at each
 // phase of the traversal process. Sections can be mutated by the visitor.
-func visitSections(configTxt *picfg.ConfigTxt, pred func([]condfilter.ConditionalFilter) bool, visitor func(sect *picfg.Section)) error {
+func visitSections(configTxt *picfg.ConfigTxt, pred func([]condfilter.ConditionalFilter) bool, visitor func(sect *picfg.Section) error) error {
 	var tracker condfilter.StateTracker
 
 	for _, sect := range *configTxt {
@@ -313,7 +315,9 @@ func visitSections(configTxt *picfg.ConfigTxt, pred func([]condfilter.Conditiona
 			}
 		}
 
-		visitor(&sect)
+		if err := visitor(&sect); err != nil {
+			return err
+		}
 	}
 
 	return nil