Skip to contentSkip to conversation

script/execute: Don't let trailing blank lines determine the return code

PATCH In review James Le Cuirot 2 participants 2 messages Dec 30, 2024 – Feb 28, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 script/execute: Don't let trailing blank lines determine the return code

From: James Le Cuirot <[email protected]>

grub_script_execute_sourcecode() parses and executes code one line at a time, updating the return code each time because only the last line determines the final status. However, trailing new lines were also executed, masking any failure on the previous line. Fix this by only trying to execute the command when there is actually one present.

This has presumably never been noticed because this code is not used by regular functions, only in special cases like eval and menu entries. The latter generally don't return at all, having booted an OS. When failing to boot, upstream GRUB triggers the fallback mechanism regardless of the return code.

We noticed the problem while using Red Hat's patches, which change this behaviour to take account of the return code. In that case, a failure takes you back to the menu rather than triggering a fallback.

---
 grub-core/script/execute.c |  5 ++++-
 tests/grub_script_eval.in  | 10 +++++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)
grub-core/script/execute.c +4 -1
index 14ff09094..8cb1e779e 100644
@@ -897,7 +897,10 @@ grub_script_execute_sourcecode (const char *source)
 	  break;
 	}
 
-      ret = grub_script_execute (parsed_script);
+      /* Don't let trailing blank lines determine the return code.  */
+      if (parsed_script->cmd)
+	ret = grub_script_execute (parsed_script);
+
       grub_script_free (parsed_script);
       grub_free (line);
     }
tests/grub_script_eval.in +9 -1
index c97b78d77..9c6211042 100644
@@ -3,4 +3,12 @@
 eval echo "Hello world"
 valname=tst
 eval $valname=hi
-echo $tst
\ No newline at end of file
+echo $tst
+
+if eval "
+false
+"; then
+  echo should have failed
+else
+  echo failed as expected
+fi
Signature · 7 lines
2.47.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · James Le Cuirot

From: James Le Cuirot <[email protected]>

grub_script_execute_sourcecode() parses and executes code one line at a time, updating the return code each time because only the last line determines the final status. However, trailing new lines were also executed, masking any failure on the previous line. Fix this by only trying to execute the command when there is actually one present.

This has presumably never been noticed because this code is not used by regular functions, only in special cases like eval and menu entries. The latter generally don't return at all, having booted an OS. When failing to boot, upstream GRUB triggers the fallback mechanism regardless of the return code.

Quoted 5 lines

We noticed the problem while using Red Hat's patches, which change this behaviour to take account of the return code. In that case, a failure takes you back to the menu rather than triggering a fallback.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel