diff --git a/grub-core/commands/menuentry.c b/grub-core/commands/menuentry.c index f64378724..c0743d1ed 100644 --- a/grub-core/commands/menuentry.c +++ b/grub-core/commands/menuentry.c @@ -228,7 +228,7 @@ setparams_prefix (int argc, char **args) { *p++ = ' '; *p++ = '\''; - p = grub_script_escape_squotes (p, args[j], grub_strlen (args[j])); + p = grub_strchrsub (p, args[j], '\'', "'\\''"); *p++ = '\''; } *p++ = '\n'; diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 8fe60b03d..d3a133591 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -323,16 +323,23 @@ struct legacy_command legacy_commands[] = char * grub_legacy_escape (const char *in, grub_size_t len) { - const char *ptr; + char saved; + char *ptr; char *outptr; int overhead = 0; - for (ptr = in; ptr < in + len && *ptr; ptr++) + + for (ptr = (char*)in; ptr < in + len && *ptr; ptr++) if (*ptr == '\'') overhead += 3; outptr = grub_malloc (ptr - in + overhead + 1); if (!outptr) return NULL; - grub_script_escape_squotes (outptr, in, len); + + ptr = (char*)in; + saved = ptr[len]; + ptr[len] = '\0'; + grub_strchrsub (outptr, in, '\'', "'\\''"); + ptr[len] = saved; return outptr; } diff --git a/grub-core/script/script.c b/grub-core/script/script.c index 45c3222b2..ad3018357 100644 --- a/grub-core/script/script.c +++ b/grub-core/script/script.c @@ -22,27 +22,6 @@ #include #include -/* Escape single quotes in first `len' characters of `in' into a GRUB - script argument form into `out'; return address of the end in - `out'. */ -char * -grub_script_escape_squotes (char *out, const char *in, grub_size_t len) -{ - while (*in && len--) - { - *out++ = *in; - if (*in == '\'') - { - *out++ = '\\'; - *out++ = '\''; - *out++ = '\''; - } - in++; - } - *out = '\0'; - return out; -} - /* It is not possible to deallocate the memory when a syntax error was found. Because of that it is required to keep track of all memory allocations. The memory is freed in case of an error, or assigned diff --git a/include/grub/misc.h b/include/grub/misc.h index 27f15e44a..4980281a6 100644 --- a/include/grub/misc.h +++ b/include/grub/misc.h @@ -193,6 +193,26 @@ grub_strncasecmp (const char *s1, const char *s2, grub_size_t n) return (int) grub_tolower (*s1) - (int) grub_tolower (*s2); } +/* Replace all `ch' characters of `input' with `with' and copy the + result into `output'; return address of the end in `output'. */ +static inline char * +grub_strchrsub (char *output, const char *input, char ch, const char *with) +{ + grub_size_t grub_strlen (const char *s); + while (*input) + { + if (*input == ch) + { + grub_strcpy (output, with); + output += grub_strlen (with); + input++; + continue; + } + *output++ = *input++; + } + *output = '\0'; + return output; +} unsigned long EXPORT_FUNC(grub_strtoul) (const char *str, char **end, int base); unsigned long long EXPORT_FUNC(grub_strtoull) (const char *str, char **end, int base); diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index f5dcd881e..6d31fca5a 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -382,9 +382,6 @@ grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *c grub_err_t grub_normal_parse_line (char *line, grub_reader_getline_t getline); -char * -grub_script_escape_squotes (char *out, const char *in, grub_size_t len); - static inline struct grub_script * grub_script_ref (struct grub_script *script) {