0052-kern-parser-Fix-resource-leak-if-argc-0.patch 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. From d06161b035dde4769199ad65aa0a587a5920012b Mon Sep 17 00:00:00 2001
  2. From: Darren Kenny <darren.kenny@oracle.com>
  3. Date: Fri, 22 Jan 2021 12:32:41 +0000
  4. Subject: [PATCH] kern/parser: Fix resource leak if argc == 0
  5. After processing the command-line yet arriving at the point where we are
  6. setting argv, we are allocating memory, even if argc == 0, which makes
  7. no sense since we never put anything into the allocated argv.
  8. The solution is to simply return that we've successfully processed the
  9. arguments but that argc == 0, and also ensure that argv is NULL when
  10. we're not allocating anything in it.
  11. There are only 2 callers of this function, and both are handling a zero
  12. value in argc assuming nothing is allocated in argv.
  13. Fixes: CID 96680
  14. Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
  15. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  16. Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
  17. ---
  18. grub-core/kern/parser.c | 5 +++++
  19. 1 file changed, 5 insertions(+)
  20. diff --git a/grub-core/kern/parser.c b/grub-core/kern/parser.c
  21. index 619db31..d1cf061 100644
  22. --- a/grub-core/kern/parser.c
  23. +++ b/grub-core/kern/parser.c
  24. @@ -146,6 +146,7 @@ grub_parser_split_cmdline (const char *cmdline,
  25. int i;
  26. *argc = 0;
  27. + *argv = NULL;
  28. do
  29. {
  30. if (!rd || !*rd)
  31. @@ -207,6 +208,10 @@ grub_parser_split_cmdline (const char *cmdline,
  32. (*argc)++;
  33. }
  34. + /* If there are no args, then we're done. */
  35. + if (!*argc)
  36. + return 0;
  37. +
  38. /* Reserve memory for the return values. */
  39. args = grub_malloc (bp - buffer);
  40. if (!args)
  41. --
  42. 2.14.2