[GSoC,1/2] sequencer: add NULL checks under read_author_script
diff mbox series

Message ID 20190712185357.21211-2-rohit.ashiwal265@gmail.com
State New
Headers show
Series
  • [GSoC,1/2] sequencer: add NULL checks under read_author_script
Related show

Commit Message

Rohit Ashiwal July 12, 2019, 6:53 p.m. UTC
read_author_script reads name, email and author date from the author
script. However, it does not check if the arguments are NULL. Adding
NULL checks will allow us to selectively get the required value, for
example:

    char *date;
    if (read_author_script(_path_, NULL, NULL, &date, _int_))
	    die(_("failed to read author date"));
    /* needs to be free()'d */
    return date;

Add NULL checks for better control over the information retrieved.

Signed-off-by: Rohit Ashiwal <rohit.ashiwal265@gmail.com>
---
 sequencer.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

Comments

Junio C Hamano July 15, 2019, 6:04 p.m. UTC | #1
Rohit Ashiwal <rohit.ashiwal265@gmail.com> writes:

> +	if (name)
> +		*name = kv.items[name_i].util;
> +	else
> +		free(kv.items[name_i].util);
> +	if (email)
> +		*email = kv.items[email_i].util;
> +	else
> +		free(kv.items[email_i].util);
> +	if (date)
> +		*date = kv.items[date_i].util;
> +	else
> +		free(kv.items[date_i].util);
>  	retval = 0;

Some of the .util field may have been freed and others may have
taken possession by the caller at this point.

>  finish:
>  	string_list_clear(&kv, !!retval);

And we tell string_list_clear() not to free the .util field, so
kv.items[].util won't leak, which is good.

Patch
diff mbox series

diff --git a/sequencer.c b/sequencer.c
index f88a97fb1..a2d7b0925 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -821,9 +821,19 @@  int read_author_script(const char *path, char **name, char **email, char **date,
 		error(_("missing 'GIT_AUTHOR_DATE'"));
 	if (date_i < 0 || email_i < 0 || date_i < 0 || err)
 		goto finish;
-	*name = kv.items[name_i].util;
-	*email = kv.items[email_i].util;
-	*date = kv.items[date_i].util;
+
+	if (name)
+		*name = kv.items[name_i].util;
+	else
+		free(kv.items[name_i].util);
+	if (email)
+		*email = kv.items[email_i].util;
+	else
+		free(kv.items[email_i].util);
+	if (date)
+		*date = kv.items[date_i].util;
+	else
+		free(kv.items[date_i].util);
 	retval = 0;
 finish:
 	string_list_clear(&kv, !!retval);