Skip to contentSkip to conversation

Fixes for some out of memory cases in GRUB script

PATCH New BVK Chaitanya 1 participant 1 message Dec 27, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fixes for some out of memory cases in GRUB script

Hi,

Below patch fixes some out of memory cases, that I came across when I was working on lexer rewrite. This patch is based on people/bvk/while-loop-support branch, and is also available as people/bvk/oom-fixes branch.

Let me know your comments.

thanks,

Signature · 1 line
bvk.chaitanya

=== modified file 'ChangeLog.scripting'

ChangeLog.scripting +23 -0
@@ -1,3 +1,26 @@
+2009-12-27  BVK Chaitanya  <bvk@dbook>
+
+	* include/grub/script_sh.h (grub_script_cmd_menuentry): Member
+	sourcecode type is reverted back to pointer to constant.
+	* script/script.c (grub_script_malloc): Fixed it to handle out of
+	memory cases gracefully.
+	(grub_script_add_cmd): Likewise.
+	(grub_script_arg_add): Likewise.
+	(grub_script_add_arglist): Likewise.
+	(grub_script_create_cmdline): Likewise.
+	(grub_script_create_cmdif): Likewise.
+	(grub_script_create_cmdmenu): Likewise, and sourcecode parameter
+	is used as it is.
+	* script/parser.y (arguments1): Added null pointer check to handle
+	out of memory cases.
+	(grubcmd): Likewise.
+	(commands1): Likewise.
+	* script/lexer.c (grub_script_lexer_record_stop): Allocates
+	sourcecode using grub_script_alloc, so that memory is recollected
+	automatically and refactored it to trim the source code.
+	* util/grub-script-check.c (grub_script_execute_menuentry):
+	Freeing sourcecode is now unnecessary, so is removed.
+
 2009-12-26  BVK Chaitanya  <[email protected]>
 
 	* include/grub/script_sh.h: New function prototypes for

=== modified file 'include/grub/script_sh.h'

grub/script_sh.h +1 -1
@@ -145,7 +145,7 @@
   struct grub_script_arglist *arglist;
 
   /* The sourcecode the entry will be generated from.  */
-  char *sourcecode;
+  const char *sourcecode;
 
   /* Options.  XXX: Not used yet.  */
   int options;

=== modified file 'script/lexer.c'

lexer.c +25 -14
@@ -63,29 +63,40 @@
 grub_script_lexer_record_stop (struct grub_parser_param *parser)
 {
   char *ptr;
+  char *result;
   struct grub_lexer_param *lexer = parser->lexerstate;
 
+  auto char *compact (char *start, char *end);
+  char *compact (char *start, char *end)
+  {
+    /* Delete '{' and '}' characters and whitespaces.  */
+    while (*start && grub_isspace (*start)) start++;
+    if (*start == '{') start++;
+    while (*start && grub_isspace (*start)) start++;
+
+    while (*end && grub_isspace (*end)) end--;
+    if (*end == '}') end--;
+    while (*end && grub_isspace (*end)) end--;
+    *end = '\0';
+
+    return start;
+  }
+
   if (!lexer->record || !lexer->recording)
     return 0;
 
-  /* Delete '{' and '}' characters.  */
   /* XXX This is not necessary in BASH.  */
 
-  ptr = lexer->recording + lexer->recordpos - 1;
-  while (*ptr && grub_isspace (*ptr))
-    ptr--;
-  if (*ptr == '}')
-    *ptr = '\0';
-
-  ptr = lexer->recording;
-  while (*ptr && grub_isspace (*ptr))
-    ptr++;
-  if (*ptr == '{')
-    ptr++;
-
+  ptr = compact (lexer->recording, lexer->recording + lexer->recordpos - 1);
   lexer->record = 0;
   lexer->recordpos = 0;
-  return (*ptr ? grub_strdup (ptr) : 0);
+
+  /* This memory would be freed by, grub_script_free.  */
+  result = grub_script_malloc (parser, grub_strlen (ptr) + 1);
+  if (result)
+    grub_strcpy (result, ptr);
+
+  return result;
 }
 
 

=== modified file 'script/parser.y'

parser.y +12 -8
@@ -158,19 +158,19 @@
 ;
 arguments1: argument arguments0
             {
-	      if ($2)
+	      if ($1 && $2)
 		{
 		  $1->next = $2;
 		  $1->argcount += $2->argcount;
 		  $2->argcount = 0;
 		}
 	      $$ = $1;
-            }      
+            }
 ;
 
 grubcmd: word arguments0
          {
-           if ($2) {
+           if ($1 && $2) {
              $1->next = $2;
              $1->argcount += $2->argcount;
              $2->argcount = 0;
@@ -197,11 +197,15 @@
 	     struct grub_script_cmd *cmd;
              struct grub_script_cmdblock *cmdblock;
 
-             cmdblock = (struct grub_script_cmdblock *) $1;
-	     cmd = cmdblock->cmdlist;
-	     while (cmd->next)
-	       cmd = cmd->next;
-	     cmd->next = $3;
+	     if ($1)
+	       {
+		 cmdblock = (struct grub_script_cmdblock *) $1;
+		 cmd = cmdblock->cmdlist;
+		 while (cmd->next)
+		   cmd = cmd->next;
+		 cmd->next = $3;
+	       }
+	     $$ = $1;
            }
 ;
 

=== modified file 'script/script.c'

script.c +28 -25

108 lines not on this page · raw message

=== modified file 'util/grub-script-check.c'

grub-script-check.c +1 -9
@@ -90,16 +90,8 @@
 }
 
 grub_err_t
-grub_script_execute_menuentry (struct grub_script_cmd *cmd)
+grub_script_execute_menuentry (struct grub_script_cmd *cmd __attribute__ ((unused)))
 {
-  struct grub_script_cmd_menuentry *menu;
-  menu = (struct grub_script_cmd_menuentry *)cmd;
-
-  if (menu->sourcecode)
-    {
-      grub_free (menu->sourcecode);
-      menu->sourcecode = 0;
-    }
   return 0;
 }