]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
script/execute: Don't let trailing blank lines determine the return code
authorJames Le Cuirot <jlecuirot@microsoft.com>
Mon, 30 Dec 2024 10:52:23 +0000 (10:52 +0000)
committerDaniel Kiper <daniel.kiper@oracle.com>
Wed, 5 Mar 2025 11:11:09 +0000 (12:11 +0100)
The 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.

Signed-off-by: James Le Cuirot <jlecuirot@microsoft.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
grub-core/script/execute.c
tests/grub_script_eval.in

index a86e0051f2c5e79b3de445b7cd30a7beef3fee1c..da99dfa050e8c30cfff7076f809018d2d5c70dfd 100644 (file)
@@ -914,7 +914,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);
     }
index c97b78d77f799fd6c13ad882428e587ac285ab53..9c62110424abd951f26e8e837e818d9a185afe33 100644 (file)
@@ -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