From 6277435a8a89c59f716c111200c072d1454b8ff2 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 11 Apr 2021 17:02:04 -0400 Subject: [PATCH] Silence some Coverity warnings and improve code consistency. Coverity complained about possible overflow in expressions like intresult = tm->tm_sec * 1000000 + fsec; on the grounds that the multiplication would happen in 32-bit arithmetic before widening to the int64 result. I think these are all false positives because of the limited possible range of tm_sec; but nonetheless it seems silly to spell it like that when nearby lines have the identical computation written with a 64-bit constant. ... or more accurately, with an LL constant, which is not project style. Make all of these use INT64CONST(), as we do elsewhere. This is all new code from a2da77cdb, so no need for back-patch. --- src/backend/utils/adt/date.c | 14 +++++++------- src/backend/utils/adt/timestamp.c | 26 +++++++++++++------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 83036e5985e..0bea16cb671 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -2158,7 +2158,7 @@ time_part_common(PG_FUNCTION_ARGS, bool retnumeric) switch (val) { case DTK_MICROSEC: - intresult = tm->tm_sec * 1000000 + fsec; + intresult = tm->tm_sec * INT64CONST(1000000) + fsec; break; case DTK_MILLISEC: @@ -2167,7 +2167,7 @@ time_part_common(PG_FUNCTION_ARGS, bool retnumeric) * tm->tm_sec * 1000 + fsec / 1000 * = (tm->tm_sec * 1'000'000 + fsec) / 1000 */ - PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 3)); + PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3)); else PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0); break; @@ -2178,7 +2178,7 @@ time_part_common(PG_FUNCTION_ARGS, bool retnumeric) * tm->tm_sec + fsec / 1'000'000 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000 */ - PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 6)); + PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6)); else PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0); break; @@ -2940,7 +2940,7 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric) break; case DTK_MICROSEC: - intresult = tm->tm_sec * 1000000 + fsec; + intresult = tm->tm_sec * INT64CONST(1000000) + fsec; break; case DTK_MILLISEC: @@ -2949,7 +2949,7 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric) * tm->tm_sec * 1000 + fsec / 1000 * = (tm->tm_sec * 1'000'000 + fsec) / 1000 */ - PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 3)); + PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3)); else PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0); break; @@ -2960,7 +2960,7 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric) * tm->tm_sec + fsec / 1'000'000 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000 */ - PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 6)); + PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6)); else PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0); break; @@ -2995,7 +2995,7 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric) * time->time / 1'000'000 + time->zone * = (time->time + time->zone * 1'000'000) / 1'000'000 */ - PG_RETURN_NUMERIC(int64_div_fast_to_numeric(time->time + time->zone * 1000000LL, 6)); + PG_RETURN_NUMERIC(int64_div_fast_to_numeric(time->time + time->zone * INT64CONST(1000000), 6)); else PG_RETURN_FLOAT8(time->time / 1000000.0 + time->zone); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index 280ee7f92ba..3a93e92e403 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -4676,7 +4676,7 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric) switch (val) { case DTK_MICROSEC: - intresult = tm->tm_sec * 1000000.0 + fsec; + intresult = tm->tm_sec * INT64CONST(1000000) + fsec; break; case DTK_MILLISEC: @@ -4685,7 +4685,7 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric) * tm->tm_sec * 1000 + fsec / 1000 * = (tm->tm_sec * 1'000'000 + fsec) / 1000 */ - PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 3)); + PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3)); else PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0); break; @@ -4696,7 +4696,7 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric) * tm->tm_sec + fsec / 1'000'000 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000 */ - PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 6)); + PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6)); else PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0); break; @@ -4772,8 +4772,8 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric) case DTK_JULIAN: if (retnumeric) PG_RETURN_NUMERIC(numeric_add_opt_error(int64_to_numeric(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)), - numeric_div_opt_error(int64_to_numeric(((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) * 1000000LL + fsec), - int64_to_numeric(SECS_PER_DAY * 1000000LL), + numeric_div_opt_error(int64_to_numeric(((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) * INT64CONST(1000000) + fsec), + int64_to_numeric(SECS_PER_DAY * INT64CONST(1000000)), NULL), NULL)); else @@ -4962,7 +4962,7 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric) break; case DTK_MICROSEC: - intresult = tm->tm_sec * 1000000 + fsec; + intresult = tm->tm_sec * INT64CONST(1000000) + fsec; break; case DTK_MILLISEC: @@ -4971,7 +4971,7 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric) * tm->tm_sec * 1000 + fsec / 1000 * = (tm->tm_sec * 1'000'000 + fsec) / 1000 */ - PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 3)); + PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3)); else PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0); break; @@ -4982,7 +4982,7 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric) * tm->tm_sec + fsec / 1'000'000 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000 */ - PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 6)); + PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6)); else PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0); break; @@ -5046,8 +5046,8 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric) case DTK_JULIAN: if (retnumeric) PG_RETURN_NUMERIC(numeric_add_opt_error(int64_to_numeric(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)), - numeric_div_opt_error(int64_to_numeric(((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) * 1000000LL + fsec), - int64_to_numeric(SECS_PER_DAY * 1000000LL), + numeric_div_opt_error(int64_to_numeric(((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) * INT64CONST(1000000) + fsec), + int64_to_numeric(SECS_PER_DAY * INT64CONST(1000000)), NULL), NULL)); else @@ -5191,7 +5191,7 @@ interval_part_common(PG_FUNCTION_ARGS, bool retnumeric) switch (val) { case DTK_MICROSEC: - intresult = tm->tm_sec * 1000000 + fsec; + intresult = tm->tm_sec * INT64CONST(1000000) + fsec; break; case DTK_MILLISEC: @@ -5200,7 +5200,7 @@ interval_part_common(PG_FUNCTION_ARGS, bool retnumeric) * tm->tm_sec * 1000 + fsec / 1000 * = (tm->tm_sec * 1'000'000 + fsec) / 1000 */ - PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 3)); + PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3)); else PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0); break; @@ -5211,7 +5211,7 @@ interval_part_common(PG_FUNCTION_ARGS, bool retnumeric) * tm->tm_sec + fsec / 1'000'000 * = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000 */ - PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 6)); + PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6)); else PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0); break; -- 2.39.5