@@ -810,15 +811,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)rev.nr=total-nr+(start_number-1);/* Make the second and subsequent mails replies to the first */if(thread){-if(nr==(total-2)){-strncpy(ref_message_id,message_id,-sizeof(ref_message_id));-ref_message_id[sizeof(ref_message_id)-1]='\0';-rev.ref_message_id=ref_message_id;+if(rev.message_id){+if(rev.ref_message_id)+free((char*)rev.message_id);+else+rev.ref_message_id=rev.message_id;}-gen_message_id(message_id,sizeof(message_id),-sha1_to_hex(commit->object.sha1));-rev.message_id=message_id;+gen_message_id(&rev,sha1_to_hex(commit->object.sha1));}if(!use_stdout)if(reopen_stdout(commit,rev.nr,keep_subject,
@@ -576,16 +576,19 @@ static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const chao2->flags=flags2;}-staticvoidgen_message_id(char*dest,unsignedintlength,char*base)+staticvoidgen_message_id(structrev_info*info,char*base){constchar*committer=git_committer_info(IDENT_WARN_ON_NO_NAME);constchar*email_start=strrchr(committer,'<');constchar*email_end=strrchr(committer,'>');-if(!email_start||!email_end||email_start>email_end-1)+structstrbufbuf;+if(!email_start||!email_end||email_start>email_end-1)die("Could not extract email from committer identity.");-snprintf(dest,length,"%s.%lu.git.%.*s",base,-(unsignedlong)time(NULL),-(int)(email_end-email_start-1),email_start+1);+strbuf_init(&buf,0);+strbuf_addf(&buf,"%s.%lu.git.%.*s",base,+(unsignedlong)time(NULL),+(int)(email_end-email_start-1),email_start+1);+info->message_id=buf.buf;
I wonder how the rule established by b315c5c (strbuf change: be
sure ->buf is never ever NULL) and at the beginning of strbuf.h
applies here. I think the current implementation of strbuf
happens to allow this, and it is very handy. Perhaps the rule
stated there should be loosened and allow copying the buf away
when you know you have stuff in there (i.e. ->buf != slopbuf).
Pierre, what do you think?
What the patch does itself is much nicer than the original.
@@ -576,16 +576,19 @@ static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const chao2->flags=flags2;}-staticvoidgen_message_id(char*dest,unsignedintlength,char*base)+staticvoidgen_message_id(structrev_info*info,char*base){constchar*committer=git_committer_info(IDENT_WARN_ON_NO_NAME);constchar*email_start=strrchr(committer,'<');constchar*email_end=strrchr(committer,'>');-if(!email_start||!email_end||email_start>email_end-1)+structstrbufbuf;+if(!email_start||!email_end||email_start>email_end-1)die("Could not extract email from committer identity.");-snprintf(dest,length,"%s.%lu.git.%.*s",base,-(unsignedlong)time(NULL),-(int)(email_end-email_start-1),email_start+1);+strbuf_init(&buf,0);+strbuf_addf(&buf,"%s.%lu.git.%.*s",base,+(unsignedlong)time(NULL),+(int)(email_end-email_start-1),email_start+1);+info->message_id=buf.buf;
I wonder how the rule established by b315c5c (strbuf change: be
sure ->buf is never ever NULL) and at the beginning of strbuf.h
applies here. I think the current implementation of strbuf
happens to allow this, and it is very handy. Perhaps the rule
stated there should be loosened and allow copying the buf away
when you know you have stuff in there (i.e. ->buf != slopbuf).
Pierre, what do you think?
What the patch does itself is much nicer than the original.
Why wouldn't you just use strbuf_detach ? I mean replacing:
+ info->message_id = buf.buf;
with:
+ info->message_id = strbuf_detach(&buf, NULL);
isn't really hard to read, and has the nice side effect to prevent
errors that could happen in the future (like reusing buf and screwing
with info->message_id without noticing it). I'd rather stand on the safe
side here, it's more forward-compatible and idiot-proof[0].
[0] Not that I believe git contributors are idiots, but I firmly
believe in defensive programming when it doesn't impact
performances. And I don't believe it would here.
--
·O· Pierre Habouzit
··O madcoder@debian.org
OOO http://www.madism.org
@@ -576,16 +576,19 @@ static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const chao2->flags=flags2;}-staticvoidgen_message_id(char*dest,unsignedintlength,char*base)+staticvoidgen_message_id(structrev_info*info,char*base){constchar*committer=git_committer_info(IDENT_WARN_ON_NO_NAME);constchar*email_start=strrchr(committer,'<');constchar*email_end=strrchr(committer,'>');-if(!email_start||!email_end||email_start>email_end-1)+structstrbufbuf;+if(!email_start||!email_end||email_start>email_end-1)die("Could not extract email from committer identity.");-snprintf(dest,length,"%s.%lu.git.%.*s",base,-(unsignedlong)time(NULL),-(int)(email_end-email_start-1),email_start+1);+strbuf_init(&buf,0);+strbuf_addf(&buf,"%s.%lu.git.%.*s",base,+(unsignedlong)time(NULL),+(int)(email_end-email_start-1),email_start+1);+info->message_id=buf.buf;
I wonder how the rule established by b315c5c (strbuf change: be
sure ->buf is never ever NULL) and at the beginning of strbuf.h
applies here. I think the current implementation of strbuf
happens to allow this, and it is very handy. Perhaps the rule
stated there should be loosened and allow copying the buf away
when you know you have stuff in there (i.e. ->buf != slopbuf).
Pierre, what do you think?
What the patch does itself is much nicer than the original.
Why wouldn't you just use strbuf_detach ? I mean replacing:
+ info->message_id = buf.buf;
with:
+ info->message_id = strbuf_detach(&buf, NULL);
isn't really hard to read, and has the nice side effect to prevent
errors that could happen in the future (like reusing buf and screwing
with info->message_id without noticing it). I'd rather stand on the safe
side here, it's more forward-compatible and idiot-proof[0].
Is it actually right to have buf go out of scope right after
strbuf_detach()? It sort of looks like it would leak memory from buf.buf.
I'm happy to do whatever the API wants there, and I didn't see anything to
leave the struct as if strbuf_release were called, but with the string
extracted for the caller.
-Daniel
*This .sig left intentionally blank*
From: Pierre Habouzit <hidden> Date: 2016-06-15 22:44:11
On Wed, Feb 06, 2008 at 10:10:30PM +0000, Daniel Barkalow wrote:
On Wed, 6 Feb 2008, Pierre Habouzit wrote:
quoted
On Wed, Feb 06, 2008 at 08:31:08PM +0000, Junio C Hamano wrote:
quoted
I wonder how the rule established by b315c5c (strbuf change: be
sure ->buf is never ever NULL) and at the beginning of strbuf.h
applies here. I think the current implementation of strbuf
happens to allow this, and it is very handy. Perhaps the rule
stated there should be loosened and allow copying the buf away
when you know you have stuff in there (i.e. ->buf != slopbuf).
Pierre, what do you think?
What the patch does itself is much nicer than the original.
Why wouldn't you just use strbuf_detach ? I mean replacing:
+ info->message_id = buf.buf;
with:
+ info->message_id = strbuf_detach(&buf, NULL);
isn't really hard to read, and has the nice side effect to prevent
errors that could happen in the future (like reusing buf and screwing
with info->message_id without noticing it). I'd rather stand on the safe
side here, it's more forward-compatible and idiot-proof[0].
Is it actually right to have buf go out of scope right after
strbuf_detach()? It sort of looks like it would leak memory from buf.buf.
I'm happy to do whatever the API wants there, and I didn't see anything to
leave the struct as if strbuf_release were called, but with the string
extracted for the caller.
err no, strbuf_detach gives you a pointer you are supposed to free()
later, and inits the strbuf passed as its argument to be used again,
though if you don't, you leak nothing.
--
·O· Pierre Habouzit
··O madcoder@debian.org
OOO http://www.madism.org
From: Pierre Habouzit <hidden> Date: 2016-06-15 22:44:11
On Wed, Feb 06, 2008 at 10:53:35PM +0000, Pierre Habouzit wrote:
On Wed, Feb 06, 2008 at 10:10:30PM +0000, Daniel Barkalow wrote:
quoted
On Wed, 6 Feb 2008, Pierre Habouzit wrote:
quoted
On Wed, Feb 06, 2008 at 08:31:08PM +0000, Junio C Hamano wrote:
quoted
I wonder how the rule established by b315c5c (strbuf change: be
sure ->buf is never ever NULL) and at the beginning of strbuf.h
applies here. I think the current implementation of strbuf
happens to allow this, and it is very handy. Perhaps the rule
stated there should be loosened and allow copying the buf away
when you know you have stuff in there (i.e. ->buf != slopbuf).
Pierre, what do you think?
What the patch does itself is much nicer than the original.
Why wouldn't you just use strbuf_detach ? I mean replacing:
+ info->message_id = buf.buf;
with:
+ info->message_id = strbuf_detach(&buf, NULL);
isn't really hard to read, and has the nice side effect to prevent
errors that could happen in the future (like reusing buf and screwing
with info->message_id without noticing it). I'd rather stand on the safe
side here, it's more forward-compatible and idiot-proof[0].
Is it actually right to have buf go out of scope right after
strbuf_detach()? It sort of looks like it would leak memory from buf.buf.
I'm happy to do whatever the API wants there, and I didn't see anything to
leave the struct as if strbuf_release were called, but with the string
extracted for the caller.
err no, strbuf_detach gives you a pointer you are supposed to free()
later, and inits the strbuf passed as its argument to be used again,
though if you don't, you leak nothing.
In fact, strbuf_detach is the rough equivalent of doing that:
info->message_id = buf.buf;
buf.buf = NULL;
Except that it sets buf.buf to a magic place so that it's never NULL,
and that it also keeps the internal invariants in place. But after a
strbuf_detach, a strbuf doesn't holds any allocated memory anymore.
You'll see in many places in the code that
`return strbuf_detach(&sb, NULL)` is quite idiomatic, and the function
does exactly what it means "Please detach the memory allocated in that
buffer and give it to me".
--
·O· Pierre Habouzit
··O madcoder@debian.org
OOO http://www.madism.org
From: Daniel Barkalow <hidden> Date: 2016-06-15 22:44:11
On Wed, 6 Feb 2008, Pierre Habouzit wrote:
On Wed, Feb 06, 2008 at 10:53:35PM +0000, Pierre Habouzit wrote:
quoted
On Wed, Feb 06, 2008 at 10:10:30PM +0000, Daniel Barkalow wrote:
quoted
On Wed, 6 Feb 2008, Pierre Habouzit wrote:
quoted
On Wed, Feb 06, 2008 at 08:31:08PM +0000, Junio C Hamano wrote:
quoted
I wonder how the rule established by b315c5c (strbuf change: be
sure ->buf is never ever NULL) and at the beginning of strbuf.h
applies here. I think the current implementation of strbuf
happens to allow this, and it is very handy. Perhaps the rule
stated there should be loosened and allow copying the buf away
when you know you have stuff in there (i.e. ->buf != slopbuf).
Pierre, what do you think?
What the patch does itself is much nicer than the original.
Why wouldn't you just use strbuf_detach ? I mean replacing:
+ info->message_id = buf.buf;
with:
+ info->message_id = strbuf_detach(&buf, NULL);
isn't really hard to read, and has the nice side effect to prevent
errors that could happen in the future (like reusing buf and screwing
with info->message_id without noticing it). I'd rather stand on the safe
side here, it's more forward-compatible and idiot-proof[0].
Is it actually right to have buf go out of scope right after
strbuf_detach()? It sort of looks like it would leak memory from buf.buf.
I'm happy to do whatever the API wants there, and I didn't see anything to
leave the struct as if strbuf_release were called, but with the string
extracted for the caller.
err no, strbuf_detach gives you a pointer you are supposed to free()
later, and inits the strbuf passed as its argument to be used again,
though if you don't, you leak nothing.
In fact, strbuf_detach is the rough equivalent of doing that:
info->message_id = buf.buf;
buf.buf = NULL;
Except that it sets buf.buf to a magic place so that it's never NULL,
and that it also keeps the internal invariants in place. But after a
strbuf_detach, a strbuf doesn't holds any allocated memory anymore.
You'll see in many places in the code that
`return strbuf_detach(&sb, NULL)` is quite idiomatic, and the function
does exactly what it means "Please detach the memory allocated in that
buffer and give it to me".
Ah, good. That's what I'll use, then.
-Daniel
*This .sig left intentionally blank*