Re: [PATCH] Fix expressions always false

2019-12-19 Thread Ranier Vilela
>1) long is 64 bits on Unix-like platforms
>2) checking a long against LONG_MIN/LONG_MAX is _definitely_ pointless
>3) it's being cast to an int for the from_char_set_int() call below
>Please take your time to read the whole context of the code you're
>changing, and consider other platforms than just Windows.
Thank you for point me, about this.

regards,
Ranier Vilela

Em qui., 19 de dez. de 2019 às 20:58, Dagfinn Ilmari Mannsåker <
ilm...@ilmari.org> escreveu:

> Ranier Vilela  writes:
>
> > More about expressions always false.
> > 2. src/backend/utils/adt/formatting.c
> > result is declared long. Comparison with int limits is always false.
> > 3. src/backend/utils/adt/jsonfuncs.c
> > lindex is declared long. . Comparison with int limits is always false.
>
> 1) long is 64 bits on Unix-like platforms
> 2) checking a long against LONG_MIN/LONG_MAX is _definitely_ pointless
> 3) it's being cast to an int for the from_char_set_int() call below
>
> Please take your time to read the whole context of the code you're
> changing, and consider other platforms than just Windows.
>
> - ilmari
> --
> "A disappointingly low fraction of the human race is,
>  at any given time, on fire." - Stig Sandbeck Mathisen
>
diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c
index dbed597816..f0ad9f23e5 100644
--- a/src/backend/executor/execExprInterp.c
+++ b/src/backend/executor/execExprInterp.c
@@ -2736,7 +2736,7 @@ ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op)
 /* Get sub-array details from first member */
 elem_ndims = this_ndims;
 ndims = elem_ndims + 1;
-if (ndims <= 0 || ndims > MAXDIM)
+if (ndims > MAXDIM)
 	ereport(ERROR,
 			(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 			 errmsg("number of array dimensions (%d) exceeds " \
diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c
index a6dd8b75aa..bb1e8522dd 100644
--- a/src/backend/utils/adt/network.c
+++ b/src/backend/utils/adt/network.c
@@ -280,8 +280,6 @@ network_send(inet *addr, bool is_cidr)
 	pq_sendbyte(, ip_bits(addr));
 	pq_sendbyte(, is_cidr);
 	nb = ip_addrsize(addr);
-	if (nb < 0)
-		nb = 0;
 	pq_sendbyte(, nb);
 	addrptr = (char *) ip_addr(addr);
 	for (i = 0; i < nb; i++)


Re: [PATCH] Fix expressions always false

2019-12-19 Thread Dagfinn Ilmari Mannsåker
Ranier Vilela  writes:

> More about expressions always false.
> 2. src/backend/utils/adt/formatting.c
> result is declared long. Comparison with int limits is always false.
> 3. src/backend/utils/adt/jsonfuncs.c
> lindex is declared long. . Comparison with int limits is always false.

1) long is 64 bits on Unix-like platforms
2) checking a long against LONG_MIN/LONG_MAX is _definitely_ pointless
3) it's being cast to an int for the from_char_set_int() call below

Please take your time to read the whole context of the code you're
changing, and consider other platforms than just Windows.

- ilmari
-- 
"A disappointingly low fraction of the human race is,
 at any given time, on fire." - Stig Sandbeck Mathisen




[PATCH] Fix expressions always false

2019-12-19 Thread Ranier Vilela
More about expressions always false.
1. /src/backend/executor/execExprInterp.c
ndims <= 0 neve be negative, because ndims aways is added up +1
2. src/backend/utils/adt/formatting.c
result is declared long. Comparison with int limits is always false.
3. src/backend/utils/adt/jsonfuncs.c
lindex is declared long. . Comparison with int limits is always false.
4. src/backend/utils/adt/network.c
ip_addrsize is macro and awlays return 4 or 16

regards,
Ranier Vilela
diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c
index dbed597816..f0ad9f23e5 100644
--- a/src/backend/executor/execExprInterp.c
+++ b/src/backend/executor/execExprInterp.c
@@ -2736,7 +2736,7 @@ ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op)
 /* Get sub-array details from first member */
 elem_ndims = this_ndims;
 ndims = elem_ndims + 1;
-if (ndims <= 0 || ndims > MAXDIM)
+if (ndims > MAXDIM)
 	ereport(ERROR,
 			(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 			 errmsg("number of array dimensions (%d) exceeds " \
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index 8fcbc2267f..832b3b2ed2 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -2418,13 +2418,13 @@ from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node,
 	 copy, node->key->name),
 			  errdetail("Value must be an integer.";
 
-	if (errno == ERANGE || result < INT_MIN || result > INT_MAX)
+	if (errno == ERANGE || result < LONG_MIN || result > LONG_MAX)
 		RETURN_ERROR(ereport(ERROR,
 			 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
 			  errmsg("value for \"%s\" in source string is out of range",
 	 node->key->name),
-			  errdetail("Value must be in the range %d to %d.",
-		INT_MIN, INT_MAX;
+			  errdetail("Value must be in the range %ld to %ld.",
+		LONG_MIN, LONG_MAX;
 
 	if (dest != NULL)
 	{
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index 1b0fb2afae..ec988fb6b9 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -1406,7 +1406,7 @@ get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
 			errno = 0;
 			lindex = strtol(indextext, , 10);
 			if (endptr == indextext || *endptr != '\0' || errno != 0 ||
-lindex > INT_MAX || lindex < INT_MIN)
+lindex > LONG_MAX || lindex < LONG_MIN)
 PG_RETURN_NULL();
 
 			if (lindex >= 0)
@@ -4786,11 +4786,11 @@ setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls,
 
 		errno = 0;
 		lindex = strtol(c, , 10);
-		if (errno != 0 || badp == c || *badp != '\0' || lindex > INT_MAX ||
-			lindex < INT_MIN)
+		if (errno != 0 || badp == c || *badp != '\0' || lindex > LONG_MAX ||
+			lindex < LONG_MIN)
 			ereport(ERROR,
 	(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
-	 errmsg("path element at position %d is not an integer: \"%s\"",
+	 errmsg("path element at position %d is not an long integer: \"%s\"",
 			level + 1, c)));
 		idx = lindex;
 	}
diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c
index a6dd8b75aa..bb1e8522dd 100644
--- a/src/backend/utils/adt/network.c
+++ b/src/backend/utils/adt/network.c
@@ -280,8 +280,6 @@ network_send(inet *addr, bool is_cidr)
 	pq_sendbyte(, ip_bits(addr));
 	pq_sendbyte(, is_cidr);
 	nb = ip_addrsize(addr);
-	if (nb < 0)
-		nb = 0;
 	pq_sendbyte(, nb);
 	addrptr = (char *) ip_addr(addr);
 	for (i = 0; i < nb; i++)