# [PATCH] Problem in bulk-checkin.c:finish_bulk_checkin() Unable to fix

2 messages from 2014-02-28 to 2014-02-28. Participants: Faiz Kothari, Eric Sunshine.
Thread: https://gitlist.dev/t/35984

## Faiz Kothari, 2014-02-28 00:04

Subject: [PATCH] Problem in bulk-checkin.c:finish_bulk_checkin() Unable to fix
Message-ID: <1393545849-26775-1-git-send-email-faiz.off93@gmail.com>
URL: https://gitlist.dev/e/1393545849-26775-1-git-send-email-faiz.off93%40gmail.com

```
Signed-off-by: Faiz Kothari <faiz.off93@gmail.com>
---
Compiles without errors.
Fails in test t/t1050-large.sh ,fails 12/15 tests. Dumps memory map and backtrace.
Somewhere its not able to free(): invalid pointer.
Please somone pointout where I am doing it wrong.
Help is really appreciated.
Thanks.

 bulk-checkin.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/bulk-checkin.c b/bulk-checkin.c
index 118c625..c76cd6b 100644
--- a/bulk-checkin.c
+++ b/bulk-checkin.c
@@ -23,7 +23,7 @@ static struct bulk_checkin_state {
 static void finish_bulk_checkin(struct bulk_checkin_state *state)
 {
 	unsigned char sha1[20];
-	char packname[PATH_MAX];
+	struct strbuf packname = STRBUF_INIT;
 	int i;
 
 	if (!state->f)
@@ -42,9 +42,8 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
 					 state->offset);
 		close(fd);
 	}
-
-	sprintf(packname, "%s/pack/pack-", get_object_directory());
-	finish_tmp_packfile(packname, state->pack_tmp_name,
+	strbuf_addf(&packname, "%s/pack/pack-", get_object_directory());
+	finish_tmp_packfile(packname.buf, state->pack_tmp_name,
 			    state->written, state->nr_written,
 			    &state->pack_idx_opts, sha1);
 	for (i = 0; i < state->nr_written; i++)
@@ -53,6 +52,7 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
 clear_exit:
 	free(state->written);
 	memset(state, 0, sizeof(*state));
+	strbuf_release(&packname);
 
 	/* Make objects we just wrote available to ourselves */
 	reprepare_packed_git();
-- 
1.7.9.5

```

## Eric Sunshine, 2014-02-28 05:10

Subject: Re: [PATCH] Problem in bulk-checkin.c:finish_bulk_checkin() Unable to fix
Message-ID: <CAPig+cTMLEw0hkd2fvixyXbszMGrSd2xCa5qm6=cNfcE4fv9yA@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcTMLEw0hkd2fvixyXbszMGrSd2xCa5qm6%3DcNfcE4fv9yA%40mail.gmail.com
In-Reply-To: <1393545849-26775-1-git-send-email-faiz.off93@gmail.com>

```
On Thu, Feb 27, 2014 at 7:04 PM, Faiz Kothari <faiz.off93@gmail.com> wrote:
> Signed-off-by: Faiz Kothari <faiz.off93@gmail.com>
> ---
> Compiles without errors.
> Fails in test t/t1050-large.sh ,fails 12/15 tests. Dumps memory map and backtrace.
> Somewhere its not able to free(): invalid pointer.
> Please somone pointout where I am doing it wrong.
> Help is really appreciated.
> Thanks.

Hint: Pay close attention to the second sentence of the microproject
you've undertaken:

    Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for
    handling packname, and explain why this is useful. Also check if
    the first argument of pack-write.c:finish_tmp_packfile() can be
    made const.

It's the clue to understanding the crash.

>  bulk-checkin.c |    8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/bulk-checkin.c b/bulk-checkin.c
> index 118c625..c76cd6b 100644
> --- a/bulk-checkin.c
> +++ b/bulk-checkin.c
> @@ -23,7 +23,7 @@ static struct bulk_checkin_state {
>  static void finish_bulk_checkin(struct bulk_checkin_state *state)
>  {
>         unsigned char sha1[20];
> -       char packname[PATH_MAX];
> +       struct strbuf packname = STRBUF_INIT;
>         int i;
>
>         if (!state->f)
> @@ -42,9 +42,8 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
>                                          state->offset);
>                 close(fd);
>         }
> -
> -       sprintf(packname, "%s/pack/pack-", get_object_directory());
> -       finish_tmp_packfile(packname, state->pack_tmp_name,
> +       strbuf_addf(&packname, "%s/pack/pack-", get_object_directory());
> +       finish_tmp_packfile(packname.buf, state->pack_tmp_name,
>                             state->written, state->nr_written,
>                             &state->pack_idx_opts, sha1);
>         for (i = 0; i < state->nr_written; i++)
> @@ -53,6 +52,7 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
>  clear_exit:
>         free(state->written);
>         memset(state, 0, sizeof(*state));
> +       strbuf_release(&packname);
>
>         /* Make objects we just wrote available to ourselves */
>         reprepare_packed_git();
> --
> 1.7.9.5
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

```
