grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: escape strings in grub.cfg


From: address@hidden
Subject: Re: escape strings in grub.cfg
Date: Mon, 12 Apr 2010 12:23:01 -0500

On Sun, Apr 11, 2010 at 7:09 PM, Colin Watson <address@hidden> wrote:
> On Sat, Apr 10, 2010 at 10:10:15PM +0100, Carles Pina i Estany wrote:
>> Last weekend, the Asturian team found a bug in Grub. Thanks for the
>> debugging. The string that appears in grub.cfg for "Loading the initram"
>> in Asturian is "Cargando'l discu RAM inicial...". So grub.cfg looks
>> like:
>>         echo    Cargando'l discu RAM inicial...
>>
>> This is syntactically invalid because the character "'".
>>
>> Easy work around: generate the line like:
>>       echo    "Cargando'l discu RAM inicial..."
>>
>> New problem: in some language, this string could have a double quote
>> inside.
>>
>> New solution: escape the double quotes strings when they are used in
>> gettext and always use these strings with double quotes.
>>
>> See the attached patch.
>>
>> I'm not convinced of the patch, does anybody has a cleaner idea/patch?
>
> This doesn't cover everything - you've just moved the goalposts a bit.
> Here's the relevant bit of the lexer:
>
> <SQUOTE>{
>  \'            {
>                  yy_pop_state (yyscanner);
>                  ARG (GRUB_SCRIPT_ARG_TYPE_SQSTR);
>                }
>  [^\']+        { COPY (yytext, yyleng); }
> }
>
> <DQUOTE>{
>  \"            {
>                  yy_pop_state (yyscanner);
>                  ARG (GRUB_SCRIPT_ARG_TYPE_DQSTR);
>                }
>  \$            {
>                  yy_push_state (VAR, yyscanner);
>                  ARG (GRUB_SCRIPT_ARG_TYPE_DQSTR);
>                }
>  \\\\          { COPY ("\\", 1); }
>  \\\"          { COPY ("\"", 1); }
>  \\\n          { /* ignore */ }
>  [^\"$\\\n]+   { COPY (yytext, yyleng); }
>  (.|\n)        { COPY (yytext, yyleng); }
> }
>
> In other words, only ' is special (as a terminator) when reading a
> single-quoted string, and the sequences " $ \\ \" \n are special inside
> a double-quoted string.  (Incidentally, I think there's another bug
> here; if $ is special, \$ should be special as well.)
>
> Given this, wouldn't you be better off using single-quotes instead?
> You'd then end up with:
>
>          echo    'Cargando'\''l discu RAM inicial...'
>
> ... where the construction used to escape ' is "close quotes, literal ',
> reopen quotes".  (Tested in grub-emu.)
>
>> +gettext_escape_double_quotes ()
>> +{
>> +  echo -n $(gettext $@) | sed 's/\"/\\\"/'
>> +}
>
> This is underquoted, and in any case the 'echo -n' is redundant since
> gettext itself already writes to standard output and $() substitution
> strips trailing newlines.  $@ requires special treatment, and that sed
> s/// command needs /g.  With my suggestion, I'd recommend:
>
>  gettext_quoted () {
>    gettext "$@" | sed "s/'/'\\\\''/g"
>  }

I haven't tried it myself, but you just said the newline is stripped
by $() and then took out the $(), seems like the newline would be
present again.


>
> ... and then make sure it's enclosed in '' rather than "" wherever it's
> used (or modify the sed to put single-quotes at the start and end, if
> that works out neater).
>
> --
> Colin Watson                                       address@hidden
>
>
> _______________________________________________
> Grub-devel mailing list
> address@hidden
> http://lists.gnu.org/mailman/listinfo/grub-devel
>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]