[PATCH 1 of 4 warning-cleanup] parsers.c: avoid integer truncations

Yuya Nishihara yuya at tcha.org
Sat Aug 22 11:32:33 UTC 2015


On Fri, 21 Aug 2015 14:36:09 -0400, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <augie at google.com>
> # Date 1440181433 14400
> #      Fri Aug 21 14:23:53 2015 -0400
> # Node ID 837bbc972e00aefa606bed43ee8232275b8f5e52
> # Parent  607868eccaa7a351f604f3d5b62734b9670270df
> parsers.c: avoid integer truncations
> 
> Caught with `make local CFLAGS='-Wshorten-64-to-32' CC=clang`:
> 
> mercurial/parsers.c:1191:28: warning: implicit conversion loses integer
>       precision: 'long' to 'int' [-Wshorten-64-to-32]
>                         tovisit[lentovisit++] = revnum;
>                                               ~ ^~~~~~
> mercurial/parsers.c:1230:12: warning: implicit conversion loses integer
>       precision: 'long' to 'int' [-Wshorten-64-to-32]
>                 minidx = minroot;
>                        ~ ^~~~~~~
> 
> diff --git a/mercurial/parsers.c b/mercurial/parsers.c
> --- a/mercurial/parsers.c
> +++ b/mercurial/parsers.c
> @@ -1127,13 +1127,13 @@ static PyObject *reachableroots2(indexOb
>  	Py_ssize_t i;
>  	Py_ssize_t l;
>  	int r;
> -	int minidx;
> +	long minidx;
>  	int parents[2];
>  
>  	/* Internal data structure:
>  	 * tovisit: array of length len+1 (all revs + nullrev), filled upto lentovisit
>  	 * revstates: array of length len+1 (all revs + nullrev) */
> -	int *tovisit = NULL;
> +	long *tovisit = NULL;
>  	long lentovisit = 0;
>  	enum { RS_SEEN = 1, RS_ROOT = 2, RS_REACHABLE = 4 };
>  	char *revstates = NULL;
> @@ -1153,7 +1153,7 @@ static PyObject *reachableroots2(indexOb
>  		goto bail;
>  
>  	/* Initialize internal datastructures */
> -	tovisit = (int *)malloc((len + 1) * sizeof(int));
> +	tovisit = (long *)malloc((len + 1) * sizeof(long));

tovisit is an array of len(index) size. I don't think it's worth wasting space
only to silence compiler warning. We know valid revnum doesn't exceed INT_MAX.



More information about the Mercurial-devel mailing list