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

login
register
mail settings
Submitter Augie Fackler
Date Aug. 21, 2015, 6:36 p.m.
Message ID <837bbc972e00aefa606b.1440182169@augie-macbookair2.roam.corp.google.com>
Download mbox | patch
Permalink /patch/10245/
State Accepted
Headers show

Comments

Augie Fackler - Aug. 21, 2015, 6:36 p.m.
# HG changeset patch
# User Augie Fackler <augie@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;
                       ~ ^~~~~~~
Yuya Nishihara - Aug. 22, 2015, 11:32 a.m.
On Fri, 21 Aug 2015 14:36:09 -0400, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <augie@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.

Patch

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));
 	if (tovisit == NULL) {
 		PyErr_NoMemory();
 		goto bail;