kern/parser: Refactor grub_parser_split_cmdline() cleanup

Introduce a common function epilogue used for cleaning up on all
return paths, which will simplify additional error handling to be
introduced in a subsequent commit.

Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Chris Coulson 2021-01-06 13:54:26 +00:00 committed by Daniel Kiper
parent 3d157bbd06
commit 8bc817014c

View File

@ -221,19 +221,13 @@ grub_parser_split_cmdline (const char *cmdline,
if (process_char (*rp, buffer, &bp, varname, &vp, state, argc, if (process_char (*rp, buffer, &bp, varname, &vp, state, argc,
&newstate) != GRUB_ERR_NONE) &newstate) != GRUB_ERR_NONE)
{ goto fail;
if (rd != cmdline)
grub_free (rd);
return grub_errno;
}
state = newstate; state = newstate;
} }
} }
while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state)); while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
if (rd != cmdline)
grub_free (rd);
/* A special case for when the last character was part of a /* A special case for when the last character was part of a
variable. */ variable. */
add_var (varname, &bp, &vp, state, GRUB_PARSER_STATE_TEXT); add_var (varname, &bp, &vp, state, GRUB_PARSER_STATE_TEXT);
@ -243,20 +237,20 @@ grub_parser_split_cmdline (const char *cmdline,
/* If there are no args, then we're done. */ /* If there are no args, then we're done. */
if (!*argc) if (!*argc)
return 0; {
grub_errno = GRUB_ERR_NONE;
goto out;
}
/* Reserve memory for the return values. */ /* Reserve memory for the return values. */
args = grub_malloc (bp - buffer); args = grub_malloc (bp - buffer);
if (!args) if (!args)
return grub_errno; goto fail;
grub_memcpy (args, buffer, bp - buffer); grub_memcpy (args, buffer, bp - buffer);
*argv = grub_calloc (*argc + 1, sizeof (char *)); *argv = grub_calloc (*argc + 1, sizeof (char *));
if (!*argv) if (!*argv)
{ goto fail;
grub_free (args);
return grub_errno;
}
/* The arguments are separated with 0's, setup argv so it points to /* The arguments are separated with 0's, setup argv so it points to
the right values. */ the right values. */
@ -269,7 +263,18 @@ grub_parser_split_cmdline (const char *cmdline,
bp++; bp++;
} }
return 0; grub_errno = GRUB_ERR_NONE;
out:
if (rd != cmdline)
grub_free (rd);
return grub_errno;
fail:
grub_free (*argv);
grub_free (args);
goto out;
} }
/* Helper for grub_parser_execute. */ /* Helper for grub_parser_execute. */