threads / patch / 35981

patchGSoC2014 Microproject rewrite finish_bulk_checkin()

Subject: [PATCH] GSoC2014 Microproject rewrite finish_bulk_checkin()

## tl;dr

3 messages between Feb 27, 2014 and Feb 27, 2014. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Faiz Kothari· Feb 27, 2014, 19:02 UTC · lore
Signed-off-by: Faiz Kothari <faiz.off93@gmail.com>
---
 bulk-checkin.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
Show changes to bulk-checkin.c +7 −5
diff --git a/bulk-checkin.c b/bulk-checkin.c
index 118c625..feeff9f 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;
 	int i;
 
 	if (!state->f)
@@ -42,9 +42,11 @@ 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,
+	
+	packname.len = packname.alloc = 64 + strlen(get_object_directory());
+	packname.buf = (char *)malloc(packname.len * sizeof(char));
+	sprintf(packname.buf, "%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,7 +55,7 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
 clear_exit:
 	free(state->written);
 	memset(state, 0, sizeof(*state));
-
+	free(packname.buf);
 	/* Make objects we just wrote available to ourselves */
 	reprepare_packed_git();
 }
-- 
1.7.9.5

> 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.

Adding 64 to strlen(get_object_directory()) to accomodate sha1_to_hex(sha1) and itself.
Using the APIs for strbuf is giving me test failures(12/15) during t1050-large.sh 
So, I used the malloc() and free() instead.
Instead of having packname on stack and cause stackoverflow because of MAX_PATH ~ 4KB, have it on heap.
Can have first parameter to pack-write.c:finish_tmp_packfile() as const because packname is not required to be modified.

I apologise for my two earlier patches not being in proper format. I have finally got it working properly. Will make sure,
it does not happen again.
Michael Haggerty· Feb 27, 2014, 22:17 UTC · re: Faiz Kothari · lore

Re: [PATCH] GSoC2014 Microproject rewrite finish_bulk_checkin()

On 02/27/2014 08:02 PM, Faiz Kothari wrote:
Show 49 quoted lines
> Signed-off-by: Faiz Kothari <faiz.off93@gmail.com>
> ---
>  bulk-checkin.c |   12 +++++++-----
>  1 file changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/bulk-checkin.c b/bulk-checkin.c
> index 118c625..feeff9f 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;
>  	int i;
>  
>  	if (!state->f)
> @@ -42,9 +42,11 @@ 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,
> +	
> +	packname.len = packname.alloc = 64 + strlen(get_object_directory());
> +	packname.buf = (char *)malloc(packname.len * sizeof(char));
> +	sprintf(packname.buf, "%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,7 +55,7 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
>  clear_exit:
>  	free(state->written);
>  	memset(state, 0, sizeof(*state));
> -
> +	free(packname.buf);
>  	/* Make objects we just wrote available to ourselves */
>  	reprepare_packed_git();
>  }
> -- 1.7.9.5
>> 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.
> 
> Adding 64 to strlen(get_object_directory()) to accomodate sha1_to_hex(sha1) and itself.
> Using the APIs for strbuf is giving me test failures(12/15) during t1050-large.sh 
> So, I used the malloc() and free() instead.

This is not OK. I promise you, the strbuf API works correctly if it is used correctly. (And if it really *were* broken, you should fix the problem or at least diagnose and document it rather than working around it.)

Show 5 quoted lines
> Instead of having packname on stack and cause stackoverflow because of MAX_PATH ~ 4KB, have it on heap.
> Can have first parameter to pack-write.c:finish_tmp_packfile() as const because packname is not required to be modified.
> 
> I apologise for my two earlier patches not being in proper format. I have finally got it working properly. Will make sure,
> it does not happen again.

Almost. This last set of comments should be moved to directly after the "---" line.

But: please rather stick to *one* microproject and get it perfect, and
leave the others to other students.
Michael
-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/
Faiz Kothari· Feb 27, 2014, 22:35 UTC · re: Michael Haggerty · lore

Re: [PATCH] GSoC2014 Microproject rewrite finish_bulk_checkin()

Hi, Thanks for the remarks. I'll stick to this micro project and follow the guidelines. Yes, the strbuf API is perfectly OK. I was not getting to work it properly, so I used malloc() / free() instead. My bad. I'll resubmit the patch. Thanks.

On Fri, Feb 28, 2014 at 3:47 AM, Michael Haggerty <mhagger@alum.mit.edu> wrote:
Show 73 quoted lines
> On 02/27/2014 08:02 PM, Faiz Kothari wrote:
>> Signed-off-by: Faiz Kothari <faiz.off93@gmail.com>
>> ---
>>  bulk-checkin.c |   12 +++++++-----
>>  1 file changed, 7 insertions(+), 5 deletions(-)
>>
>> diff --git a/bulk-checkin.c b/bulk-checkin.c
>> index 118c625..feeff9f 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;
>>       int i;
>>
>>       if (!state->f)
>> @@ -42,9 +42,11 @@ 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,
>> +
>> +     packname.len = packname.alloc = 64 + strlen(get_object_directory());
>> +     packname.buf = (char *)malloc(packname.len * sizeof(char));
>> +     sprintf(packname.buf, "%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,7 +55,7 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
>>  clear_exit:
>>       free(state->written);
>>       memset(state, 0, sizeof(*state));
>> -
>> +     free(packname.buf);
>>       /* Make objects we just wrote available to ourselves */
>>       reprepare_packed_git();
>>  }
>> -- 1.7.9.5
>>> 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.
>>
>> Adding 64 to strlen(get_object_directory()) to accomodate sha1_to_hex(sha1) and itself.
>> Using the APIs for strbuf is giving me test failures(12/15) during t1050-large.sh
>> So, I used the malloc() and free() instead.
>
> This is not OK.  I promise you, the strbuf API works correctly if it is
> used correctly.  (And if it really *were* broken, you should fix the
> problem or at least diagnose and document it rather than working around it.)
>
>> Instead of having packname on stack and cause stackoverflow because of MAX_PATH ~ 4KB, have it on heap.
>> Can have first parameter to pack-write.c:finish_tmp_packfile() as const because packname is not required to be modified.
>>
>> I apologise for my two earlier patches not being in proper format. I have finally got it working properly. Will make sure,
>> it does not happen again.
>
> Almost.  This last set of comments should be moved to directly after the
> "---" line.
>
> But: please rather stick to *one* microproject and get it perfect, and
> leave the others to other students.
>
> Michael
>
> --
> Michael Haggerty
> mhagger@alum.mit.edu
> http://softwareswirl.blogspot.com/

← back to recent threads