Patchwork manifest.c: ensure realloc_if_full() returns 1 or 0

login
register
mail settings
Submitter Matt Harbison
Date March 14, 2015, 3:38 a.m.
Message ID <39339e3fd795da2afc10.1426304311@Envy>
Download mbox | patch
Permalink /patch/8089/
State Accepted
Headers show

Comments

Matt Harbison - March 14, 2015, 3:38 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1426301440 14400
#      Fri Mar 13 22:50:40 2015 -0400
# Node ID 39339e3fd795da2afc10c211cced742cf900fa50
# Parent  0bd08ed74fa0b4043ec4f57a5eec5d63f547fae7
manifest.c: ensure realloc_if_full() returns 1 or 0

This fixes an MSVC 2008 warning that I don't see with gcc 4.6.3-2:

    warning C4047: 'return' :
            'bool' differs in levels of indirection from 'line *'

More importantly, the truncation from pointer to 'unsigned char' would have
returned 0 if self->lines pointed to an address divisible by 0xFF, which causes
find_lines() to return MANIFEST_OOM.  I was able to cause this to happen in a
trivial program with the gcc compiler.
Augie Fackler - March 14, 2015, 3:50 a.m.
On Fri, Mar 13, 2015 at 11:38:31PM -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1426301440 14400
> #      Fri Mar 13 22:50:40 2015 -0400
> # Node ID 39339e3fd795da2afc10c211cced742cf900fa50
> # Parent  0bd08ed74fa0b4043ec4f57a5eec5d63f547fae7
> manifest.c: ensure realloc_if_full() returns 1 or 0

queued, thanks

>
> This fixes an MSVC 2008 warning that I don't see with gcc 4.6.3-2:
>
>     warning C4047: 'return' :
>             'bool' differs in levels of indirection from 'line *'
>
> More importantly, the truncation from pointer to 'unsigned char' would have
> returned 0 if self->lines pointed to an address divisible by 0xFF, which causes
> find_lines() to return MANIFEST_OOM.  I was able to cause this to happen in a
> trivial program with the gcc compiler.
>
> diff --git a/mercurial/manifest.c b/mercurial/manifest.c
> --- a/mercurial/manifest.c
> +++ b/mercurial/manifest.c
> @@ -104,7 +104,7 @@
>               self->maxlines *= 2;
>               self->lines = realloc(self->lines, self->maxlines * sizeof(line));
>       }
> -	return self->lines;
> +	return !!self->lines;
>  }
>
>  /*
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/manifest.c b/mercurial/manifest.c
--- a/mercurial/manifest.c
+++ b/mercurial/manifest.c
@@ -104,7 +104,7 @@ 
 		self->maxlines *= 2;
 		self->lines = realloc(self->lines, self->maxlines * sizeof(line));
 	}
-	return self->lines;
+	return !!self->lines;
 }
 
 /*