Skip to contentSkip to conversation

lexer waits for space to terminate the varname

BUGFIX Vladimir 'phcoder' Serbinenko 2 participants 8 messages May 2, 2009 – May 3, 2009

lore
Share & tools
lore
#0 lore raw

Hello. A varname may be terminated by any character which isn't in a set [A-Za-z0-9_] and not only space. Here is a fix

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
kern/parser.c +4 -1
index e931853..feaee09 100644
@@ -71,7 +71,10 @@ grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
   for (transition = state_transitions; transition->from_state; transition++)
     {
       /* An exact match was found, use it.  */
-      if (transition->from_state == state && transition->input == c)
+      if (transition->from_state == state && 
+	  (transition->input == c || 
+	   (transition->input == ' ' && ! grub_isalpha (c) 
+	    && ! grub_isdigit (c) && c != '_')))
 	{
 	  found = 1;
 	  break;
#1 lore raw

Hi,

I think there is problem with this patch. Consider ${aa}, the closing character "}" would be left out.

Although you can remedy this by swapping:

{ GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1}, { GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},

so that '}' would be handled before ' ', but it's still not a good practice, as you have added a logic in code that would depend on the order of state_transitions, which is not apparent for casual code viewer. Also, ' ' might be used for other transition in the future, this code could break that. I suggest you use {} to enclose the variable that doesn't terminated with space, or add the terminating character explicitly in the transition table:

{ GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, '/ ', 1},

Quoted 13 lines · Vladimir 'phcoder' Serbinenko

Hello. A varname may be terminated by any character which isn't in a set [A-Za-z0-9_] and not only space. Here is a fix

--
Regards
Vladimir 'phcoder' Serbinenko

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

Signature · 1 line
Bean
#2 lore raw

Hello

Quoted 10 lines · Bean

Hi,

I think there is problem with this patch. Consider ${aa}, the closing character "}" would be left out.

Although you can remedy this by swapping:

{ GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1}, { GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},

It's not the same starting state. However similar issue exists with ' ' and '\"'

Quoted 5 lines

so that '}' would be handled before ' ', but it's still not a good practice, as you have added a logic in code that would depend on the order of state_transitions, which is not apparent for casual code viewer.

Perhaps we should switch to just ordering the transition rules instead of separate code for default value

Quoted 3 lines

Also, ' ' might be used for other transition in the future, this code could break that. I suggest you use {} to enclose the variable that doesn't terminated with space,

It can be a workaround but the bug is still here. The following pieces of code fail:
1) echo $hello;
2) echo $hello

or add the terminating

Quoted 33 lines

character explicitly in the transition table:

{ GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, '/ ', 1},

Quoted 13 lines · Vladimir 'phcoder' Serbinenko

Hello. A varname may be terminated by any character which isn't in a set [A-Za-z0-9_] and not only space. Here is a fix

--
Regards
Vladimir 'phcoder' Serbinenko

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

--
Bean

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

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
#3 lore raw

Hi,

Quoted 9 lines · Vladimir 'phcoder' Serbinenko

Hello

Quoted 6 lines · Bean

Hi,

I think there is problem with this patch. Consider ${aa}, the closing character "}" would be left out.

Quoted 7 lines
Quoted 4 lines

Although you can remedy this by swapping:

 { GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1}, { GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},

It's not the same starting state. However similar issue exists with ' ' and '\"'

Oh right, I copy the wrong sample.

Quoted 9 lines
Quoted 5 lines

so that '}' would be handled before ' ', but it's still not a good practice, as you have added a logic in code that would depend on the order of state_transitions, which is not apparent for casual code viewer.

Perhaps we should switch to just ordering the transition rules instead of separate code for default value

Quoted 1 line
Quoted 9 lines
Quoted 3 lines

Also, ' ' might be used for other transition in the future, this code could break that. I suggest you use {} to enclose the variable that doesn't terminated with space,

It can be a workaround  but the bug is still here. The following pieces of code fail:
1) echo $hello;
2) echo $hello

Yeah, and there are other varieties as well, such as -. Perhaps it'd be simpler to use your fix for now, but you should add some comment in the source code to indicate its purpose.

Quoted 46 lines
Quoted 25 lines

or add the terminating
character explicitly in the transition table:

 { GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, '/ ', 1},

Quoted 13 lines · Vladimir 'phcoder' Serbinenko

Hello. A varname may be terminated by any character which isn't in a set [A-Za-z0-9_] and not only space. Here is a fix

--
Regards
Vladimir 'phcoder' Serbinenko

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

Quoted 8 lines

--
Bean

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

--
Regards
Vladimir 'phcoder' Serbinenko

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

Signature · 1 line
Bean
#4 lore raw
Quoted 39 lines · Bean

Hi,

Quoted 9 lines · Vladimir 'phcoder' Serbinenko

Hello

Quoted 6 lines · Bean

Hi,

I think there is problem with this patch. Consider ${aa}, the closing character "}" would be left out.

Quoted 6 lines
Quoted 4 lines

Although you can remedy this by swapping:

{ GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1}, { GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},

It's not the same starting state. However similar issue exists with ' '

and

Quoted 1 line

'\"'

Oh right, I copy the wrong sample.

Quoted 6 lines
Quoted 5 lines

so that '}' would be handled before ' ', but it's still not a good practice, as you have added a logic in code that would depend on the order of state_transitions, which is not apparent for casual code viewer.

Quoted 8 lines

Perhaps we should switch to just ordering the transition rules instead of separate code for default value

Quoted 4 lines

Also, ' ' might be used for other transition in the future, this code could break that. I suggest you use {} to enclose the variable that doesn't terminated with space,

It can be a workaround but the bug is still here. The following pieces

of

Quoted 8 lines
Quoted 4 lines

code fail:
1) echo $hello;
2) echo $hello

Yeah, and there are other varieties as well, such as -. Perhaps it'd be simpler to use your fix for now, but you should add some comment in the source code to indicate its purpose.

What about making it rely on order of rules? It should make it more compact

Quoted 61 lines
Quoted 6 lines
Quoted 6 lines

or add the terminating
character explicitly in the transition table:

Quoted 6 lines
Quoted 6 lines

{ GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, '/ ', 1},

Quoted 1 line · Vladimir 'phcoder' Serbinenko

Hello. A varname may be terminated by any character which isn't in a

set

Quoted 26 lines
Quoted 15 lines
Quoted 12 lines

[A-Za-z0-9_] and not only space. Here is a fix

--
Regards
Vladimir 'phcoder' Serbinenko

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

Quoted 8 lines

--
Bean

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

Quoted 10 lines

--
Regards
Vladimir 'phcoder' Serbinenko

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

--
Bean

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

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
#5 lore raw
Quoted 49 lines · Vladimir 'phcoder' Serbinenko
Quoted 46 lines · Bean

Hi,

Quoted 9 lines · Vladimir 'phcoder' Serbinenko

Hello

Quoted 6 lines · Bean

Hi,

I think there is problem with this patch. Consider ${aa}, the closing character "}" would be left out.

Quoted 8 lines
Quoted 4 lines

Although you can remedy this by swapping:

 { GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1}, { GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},

It's not the same starting state. However similar issue exists with ' ' and
'\"'

Oh right, I copy the wrong sample.

Quoted 20 lines
Quoted 5 lines

so that '}' would be handled before ' ', but it's still not a good practice, as you have added a logic in code that would depend on the order of state_transitions, which is not apparent for casual code viewer.

Perhaps we should switch to just ordering the transition rules instead of
separate code for default value

Quoted 4 lines

Also, ' ' might be used for other transition in the future, this code could break that. I suggest you use {} to enclose the variable that doesn't terminated with space,

It can be a workaround  but the bug is still here. The following pieces of
code fail:
1) echo $hello;
2) echo $hello

Quoted 5 lines
Quoted 3 lines

Yeah, and there are other varieties as well, such as -. Perhaps it'd be simpler to use your fix for now, but you should add some comment in the source code to indicate its purpose.

What about making it rely on order of rules? It should make it more compact

Hi,

I'm sorry I don't quite follow, could you give an example ?

Signature · 1 line
Bean
#6 lore raw
Quoted 57 lines · Bean
Quoted 17 lines · Vladimir 'phcoder' Serbinenko
Quoted 14 lines · Bean

Hi,

Quoted 9 lines · Vladimir 'phcoder' Serbinenko

Hello

Quoted 6 lines · Bean

Hi,

I think there is problem with this patch. Consider ${aa}, the closing character "}" would be left out.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Although you can remedy this by swapping:

{ GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1}, { GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},

It's not the same starting state. However similar issue exists with '

'

Quoted 15 lines
Quoted 15 lines
Quoted 2 lines

and
'\"'

Oh right, I copy the wrong sample.

Quoted 10 lines
Quoted 5 lines

so that '}' would be handled before ' ', but it's still not a good practice, as you have added a logic in code that would depend on the order of state_transitions, which is not apparent for casual code viewer.

Perhaps we should switch to just ordering the transition rules instead of
separate code for default value

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

Also, ' ' might be used for other transition in the future, this code could break that. I suggest you use {} to enclose the variable that doesn't terminated with space,

It can be a workaround but the bug is still here. The following

pieces

Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

of
code fail:
1) echo $hello;
2) echo $hello

Yeah, and there are other varieties as well, such as -. Perhaps it'd be simpler to use your fix for now, but you should add some comment in the source code to indicate its purpose.

Quoted 7 lines
Quoted 1 line

What about making it rely on order of rules? It should make it more

compact

Hi,

I'm sorry I don't quite follow, could you give an example ?

Something like this. Compile tested only

Quoted 10 lines

--
Bean

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

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
kern/parser.c +13 -17
index 685ab22..ee6b169 100644
@@ -47,8 +47,8 @@ static struct grub_parser_state_transition state_transitions[] =
 
   { GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME2, '{', 0},
   { GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME, 0, 1},
-  { GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, ' ', 1},
   { GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_TEXT, '\"', 0},
+  { GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, ' ', 1},
   { GRUB_PARSER_STATE_QVARNAME2, GRUB_PARSER_STATE_DQUOTE, '}', 0},
 
   { 0, 0, 0, 0}
@@ -60,9 +60,7 @@ grub_parser_state_t
 grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
 {
   struct grub_parser_state_transition *transition;
-  struct grub_parser_state_transition *next_match = 0;
   struct grub_parser_state_transition default_transition;
-  int found = 0;
 
   default_transition.to_state = state;
   default_transition.keep_value = 1;
@@ -70,26 +68,24 @@ grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
   /* Look for a good translation.  */
   for (transition = state_transitions; transition->from_state; transition++)
     {
+      if (transition->from_state != state)
+	continue;
       /* An exact match was found, use it.  */
-      if (transition->from_state == state && transition->input == c)
-	{
-	  found = 1;
-	  break;
-	}
+      if (transition->input == c)
+	break;
+
+      if (transition->input == ' ' && ! grub_isalpha (c) 
+	  && ! grub_isdigit (c) && c != '_')
+	break;
 
       /* A less perfect match was found, use this one if no exact
 	 match can be found.  */
-      if (transition->from_state == state && transition->input == 0)
-	next_match = transition;
+      if (transition->input == 0)
+	break;
     }
 
-  if (! found)
-    {
-      if (next_match)
-	transition = next_match;
-      else
-	transition = &default_transition;
-    }
+  if (! transition->from_state)
+    transition = &default_transition;
 
   if (transition->keep_value)
     *result = c;
#7 lore raw

Hi,

Oh right, it looks more compact. Although you should test the corner cases to make sure no problem is introduced by the new code.

Quoted 93 lines · Vladimir 'phcoder' Serbinenko
Quoted 60 lines · Bean
Quoted 57 lines · Vladimir 'phcoder' Serbinenko
Quoted 53 lines · Bean

Hi,

Quoted 12 lines · Vladimir 'phcoder' Serbinenko

Hello

Quoted 9 lines · Bean

Hi,

I think there is problem with this patch. Consider ${aa}, the closing
character "}" would be left out.

Although you can remedy this by swapping:

Quoted 7 lines
Quoted 2 lines

 { GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1}, { GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},

It's not the same starting state. However similar issue exists with ' '
and
'\"'

Oh right, I copy the wrong sample.

Quoted 15 lines
Quoted 5 lines

so that '}' would be handled before ' ', but it's still not a good practice, as you have added a logic in code that would depend on the order of state_transitions, which is not apparent for casual code viewer.

Perhaps we should switch to just ordering the transition rules instead
of
separate code for default value

Quoted 4 lines

Also, ' ' might be used for other transition in the future, this code could break that. I suggest you use {} to enclose the variable that doesn't terminated with space,

Quoted 7 lines

It can be a workaround  but the bug is still here. The following pieces
of
code fail:
1) echo $hello;
2) echo $hello

Yeah, and there are other varieties as well, such as -. Perhaps it'd be simpler to use your fix for now, but you should add some comment in the source code to indicate its purpose.

Quoted 6 lines
Quoted 2 lines

What about making it rely on order of rules? It should make it more compact

Hi,

I'm sorry I don't quite follow, could you give an example ?

Something like this. Compile tested only

Quoted 9 lines

--
Bean

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

--
Regards
Vladimir 'phcoder' Serbinenko

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

Signature · 1 line
Bean