0128-kern-parser-Introduce-process_char-helper.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. From b1c9e9e889e4273fb15712051c887e6078511448 Mon Sep 17 00:00:00 2001
  2. From: Chris Coulson <chris.coulson@canonical.com>
  3. Date: Tue, 5 Jan 2021 22:17:28 +0000
  4. Subject: [PATCH] kern/parser: Introduce process_char() helper
  5. grub_parser_split_cmdline() iterates over each command line character.
  6. In order to add error checking and to simplify the subsequent error
  7. handling, split the character processing in to a separate function.
  8. Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
  9. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  10. Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
  11. ---
  12. grub-core/kern/parser.c | 74 ++++++++++++++++++++++++++++++-------------------
  13. 1 file changed, 46 insertions(+), 28 deletions(-)
  14. diff --git a/grub-core/kern/parser.c b/grub-core/kern/parser.c
  15. index 39e4df6..0d3582b 100644
  16. --- a/grub-core/kern/parser.c
  17. +++ b/grub-core/kern/parser.c
  18. @@ -1,7 +1,7 @@
  19. /* parser.c - the part of the parser that can return partial tokens */
  20. /*
  21. * GRUB -- GRand Unified Bootloader
  22. - * Copyright (C) 2005,2007,2009 Free Software Foundation, Inc.
  23. + * Copyright (C) 2005,2007,2009,2021 Free Software Foundation, Inc.
  24. *
  25. * GRUB is free software: you can redistribute it and/or modify
  26. * it under the terms of the GNU General Public License as published by
  27. @@ -129,6 +129,46 @@ add_var (char *varname, char **bp, char **vp,
  28. *((*bp)++) = *val;
  29. }
  30. +static grub_err_t
  31. +process_char (char c, char *buffer, char **bp, char *varname, char **vp,
  32. + grub_parser_state_t state, int *argc,
  33. + grub_parser_state_t *newstate)
  34. +{
  35. + char use;
  36. +
  37. + *newstate = grub_parser_cmdline_state (state, c, &use);
  38. +
  39. + /*
  40. + * If a variable was being processed and this character does
  41. + * not describe the variable anymore, write the variable to
  42. + * the buffer.
  43. + */
  44. + add_var (varname, bp, vp, state, *newstate);
  45. +
  46. + if (check_varstate (*newstate))
  47. + {
  48. + if (use)
  49. + *((*vp)++) = use;
  50. + }
  51. + else if (*newstate == GRUB_PARSER_STATE_TEXT &&
  52. + state != GRUB_PARSER_STATE_ESC && grub_isspace (use))
  53. + {
  54. + /*
  55. + * Don't add more than one argument if multiple
  56. + * spaces are used.
  57. + */
  58. + if (*bp != buffer && *((*bp) - 1) != '\0')
  59. + {
  60. + *((*bp)++) = '\0';
  61. + (*argc)++;
  62. + }
  63. + }
  64. + else if (use)
  65. + *((*bp)++) = use;
  66. +
  67. + return GRUB_ERR_NONE;
  68. +}
  69. +
  70. grub_err_t
  71. grub_parser_split_cmdline (const char *cmdline,
  72. grub_reader_getline_t getline, void *getline_data,
  73. @@ -172,35 +212,13 @@ grub_parser_split_cmdline (const char *cmdline,
  74. for (; *rp != '\0'; rp++)
  75. {
  76. grub_parser_state_t newstate;
  77. - char use;
  78. -
  79. - newstate = grub_parser_cmdline_state (state, *rp, &use);
  80. - /* If a variable was being processed and this character does
  81. - not describe the variable anymore, write the variable to
  82. - the buffer. */
  83. - add_var (varname, &bp, &vp, state, newstate);
  84. -
  85. - if (check_varstate (newstate))
  86. - {
  87. - if (use)
  88. - *(vp++) = use;
  89. - }
  90. - else
  91. + if (process_char (*rp, buffer, &bp, varname, &vp, state, argc,
  92. + &newstate) != GRUB_ERR_NONE)
  93. {
  94. - if (newstate == GRUB_PARSER_STATE_TEXT
  95. - && state != GRUB_PARSER_STATE_ESC && grub_isspace (use))
  96. - {
  97. - /* Don't add more than one argument if multiple
  98. - spaces are used. */
  99. - if (bp != buffer && *(bp - 1))
  100. - {
  101. - *(bp++) = '\0';
  102. - (*argc)++;
  103. - }
  104. - }
  105. - else if (use)
  106. - *(bp++) = use;
  107. + if (rd != cmdline)
  108. + grub_free (rd);
  109. + return grub_errno;
  110. }
  111. state = newstate;
  112. }
  113. --
  114. 2.14.2