# [PATCH/RFH] pp_header(): work around possible memory corruption

4 messages from 2007-06-15 to 2007-06-23. Participants: Johannes Schindelin, Junio C Hamano.
Thread: https://gitlist.dev/t/8610

## Johannes Schindelin, 2007-06-15 12:19

Subject: [PATCH/RFH] pp_header(): work around possible memory corruption
Message-ID: <Pine.LNX.4.64.0706151318210.4059@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0706151318210.4059%40racer.site

```

add_user_info() possibly adds way more than just the commit header line. 
In fact, it sometimes needs so much more space that there is a buffer 
overrun, leading to an ugly crash. For example, the date is printed in its 
own line, and usually takes up more space than the equivalent Unix epoch.

So, for good measure, add 80 characters (a full line) to the allocated 
space, in addition to the header line length.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

---

	I have no idea if 80 is a good value, and if other places
	need an equivalent fix up, too.

	But I needed this patch in a hurry...

 commit.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/commit.c b/commit.c
index f778bf4..03436b1 100644
--- a/commit.c
+++ b/commit.c
@@ -997,7 +997,7 @@ static void pp_header(enum cmit_fmt fmt,
 			len = linelen;
 			if (fmt == CMIT_FMT_EMAIL)
 				len = bound_rfc2047(linelen, encoding);
-			ALLOC_GROW(*buf_p, *ofs_p + len, *space_p);
+			ALLOC_GROW(*buf_p, *ofs_p + len + 80, *space_p);
 			dst = *buf_p + *ofs_p;
 			*ofs_p += add_user_info("Author", fmt, dst,
 						line + 7, dmode, encoding);
@@ -1008,7 +1008,7 @@ static void pp_header(enum cmit_fmt fmt,
 			len = linelen;
 			if (fmt == CMIT_FMT_EMAIL)
 				len = bound_rfc2047(linelen, encoding);
-			ALLOC_GROW(*buf_p, *ofs_p + len, *space_p);
+			ALLOC_GROW(*buf_p, *ofs_p + len + 80, *space_p);
 			dst = *buf_p + *ofs_p;
 			*ofs_p += add_user_info("Commit", fmt, dst,
 						line + 10, dmode, encoding);

```

## Junio C Hamano, 2007-06-16 05:31

Subject: Re: [PATCH/RFH] pp_header(): work around possible memory corruption
Message-ID: <7vps3w4g9x.fsf@assigned-by-dhcp.pobox.com>
URL: https://gitlist.dev/e/7vps3w4g9x.fsf%40assigned-by-dhcp.pobox.com
In-Reply-To: <Pine.LNX.4.64.0706151318210.4059@racer.site>

```
At least the older humongous pretty_print_commit() got separated
into manageable chunks, and I was happy.  I was just too lazy
when refactoring the code and stopped there.

The right fix is to propagate the "realloc as needed" callchain
into add_user_info(), instead of having "this should be enough"
there.  These two you touched are the only two callsite of that
static function.

```

## Johannes Schindelin, 2007-06-19 00:19

Subject: Re: [PATCH/RFH] pp_header(): work around possible memory corruption
Message-ID: <Pine.LNX.4.64.0706190118230.4059@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0706190118230.4059%40racer.site
In-Reply-To: <7vps3w4g9x.fsf@assigned-by-dhcp.pobox.com>

```
Hi,

On Fri, 15 Jun 2007, Junio C Hamano wrote:

> At least the older humongous pretty_print_commit() got separated into 
> manageable chunks, and I was happy.  I was just too lazy when 
> refactoring the code and stopped there.

That's perfectly okay. This is why you parked it in 'next', I guess.

> The right fix is to propagate the "realloc as needed" callchain into 
> add_user_info(), instead of having "this should be enough" there.  
> These two you touched are the only two callsite of that static function.

Right. As I said, I was in a hurry, and could not research it properly. 
Besides, now that you gave me the proper pointer, I can take care about it 
tomorrow, unless somebody else is faster.

Ciao,
Dscho

```

## Johannes Schindelin, 2007-06-23 23:32

Subject: Re: [PATCH/RFH] pp_header(): work around possible memory corruption
Message-ID: <Pine.LNX.4.64.0706240031220.4059@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0706240031220.4059%40racer.site
In-Reply-To: <7vps3w4g9x.fsf@assigned-by-dhcp.pobox.com>

```
Hi,

On Fri, 15 Jun 2007, Junio C Hamano wrote:

> At least the older humongous pretty_print_commit() got separated into 
> manageable chunks, and I was happy.  I was just too lazy when 
> refactoring the code and stopped there.

Understandable.

> The right fix is to propagate the "realloc as needed" callchain into 
> add_user_info(), instead of having "this should be enough" there.  
> These two you touched are the only two callsite of that static function.

I had a go at this, but unfortunately I do not understand enough of what 
is going on there. For example, add_rfc2047() sometimes quotes some text. 
I have no idea what a conservative estimate of the growth is, so I cannot 
continue there.

Sorry,
Dscho

```
