]> git.ozlabs.org Git - petitboot/commitdiff
grub2: fix empty file handling
authorJeremy Kerr <jk@ozlabs.org>
Wed, 4 Jun 2014 07:45:53 +0000 (15:45 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 5 Jun 2014 07:25:10 +0000 (15:25 +0800)
Currently, we have a bug when parsing zero-length files: we subtract one
from the length to exclude the trailing NUL (added by read_file), but a
zero-length file will result in a length of -1.

This change adds an explicit exit if we're attempting to parse an empty
file.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/grub2/parser.y
test/parser/Makefile.am
test/parser/test-grub2-empty.c [new file with mode: 0644]

index 23bf4276818932613b3df07e14a9392861a1e2eb..9d79d9b9b136aeeed1abf010ffe81db568f24586 100644 (file)
@@ -329,6 +329,9 @@ void grub2_parser_parse(struct grub2_parser *parser, const char *filename,
        YY_BUFFER_STATE bufstate;
        int rc;
 
+       if (!len)
+               return;
+
        parser->script->filename = filename;
 
        bufstate = yy_scan_bytes(buf, len - 1, parser->scanner);
index b36889df12bdd4301bfb9014784b6984cc994cea..283b284a621c84a3802f5cfefb5d9c3fcecf2e0c 100644 (file)
@@ -29,6 +29,7 @@ TESTS = \
        test-null \
        test-grub2-single \
        test-grub2-default \
+       test-grub2-empty \
        test-grub2-default-index \
        test-grub2-default-multiword \
        test-grub2-multiple-resolve \
diff --git a/test/parser/test-grub2-empty.c b/test/parser/test-grub2-empty.c
new file mode 100644 (file)
index 0000000..c7ebbeb
--- /dev/null
@@ -0,0 +1,10 @@
+
+#include "parser-test.h"
+
+void run_test(struct parser_test *test)
+{
+       __test_read_conf_data(test, test->ctx->device,
+                       "/grub2/grub.cfg", "", 0);
+       test_run_parser(test, "grub2");
+       check_boot_option_count(test->ctx, 0);
+}