Skip to content

Commit 6277435

Browse files
committed
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 a2da77c, so no need for back-patch.
1 parent d7cff12 commit 6277435

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/backend/utils/adt/date.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -2158,7 +2158,7 @@ time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
21582158
switch (val)
21592159
{
21602160
case DTK_MICROSEC:
2161-
intresult = tm->tm_sec * 1000000 + fsec;
2161+
intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
21622162
break;
21632163

21642164
case DTK_MILLISEC:
@@ -2167,7 +2167,7 @@ time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
21672167
* tm->tm_sec * 1000 + fsec / 1000
21682168
* = (tm->tm_sec * 1'000'000 + fsec) / 1000
21692169
*/
2170-
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 3));
2170+
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
21712171
else
21722172
PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
21732173
break;
@@ -2178,7 +2178,7 @@ time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
21782178
* tm->tm_sec + fsec / 1'000'000
21792179
* = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
21802180
*/
2181-
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 6));
2181+
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
21822182
else
21832183
PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
21842184
break;
@@ -2940,7 +2940,7 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
29402940
break;
29412941

29422942
case DTK_MICROSEC:
2943-
intresult = tm->tm_sec * 1000000 + fsec;
2943+
intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
29442944
break;
29452945

29462946
case DTK_MILLISEC:
@@ -2949,7 +2949,7 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
29492949
* tm->tm_sec * 1000 + fsec / 1000
29502950
* = (tm->tm_sec * 1'000'000 + fsec) / 1000
29512951
*/
2952-
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 3));
2952+
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
29532953
else
29542954
PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
29552955
break;
@@ -2960,7 +2960,7 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
29602960
* tm->tm_sec + fsec / 1'000'000
29612961
* = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
29622962
*/
2963-
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 6));
2963+
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
29642964
else
29652965
PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
29662966
break;
@@ -2995,7 +2995,7 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
29952995
* time->time / 1'000'000 + time->zone
29962996
* = (time->time + time->zone * 1'000'000) / 1'000'000
29972997
*/
2998-
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(time->time + time->zone * 1000000LL, 6));
2998+
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(time->time + time->zone * INT64CONST(1000000), 6));
29992999
else
30003000
PG_RETURN_FLOAT8(time->time / 1000000.0 + time->zone);
30013001
}

src/backend/utils/adt/timestamp.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -4676,7 +4676,7 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
46764676
switch (val)
46774677
{
46784678
case DTK_MICROSEC:
4679-
intresult = tm->tm_sec * 1000000.0 + fsec;
4679+
intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
46804680
break;
46814681

46824682
case DTK_MILLISEC:
@@ -4685,7 +4685,7 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
46854685
* tm->tm_sec * 1000 + fsec / 1000
46864686
* = (tm->tm_sec * 1'000'000 + fsec) / 1000
46874687
*/
4688-
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 3));
4688+
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
46894689
else
46904690
PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
46914691
break;
@@ -4696,7 +4696,7 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
46964696
* tm->tm_sec + fsec / 1'000'000
46974697
* = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
46984698
*/
4699-
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 6));
4699+
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
47004700
else
47014701
PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
47024702
break;
@@ -4772,8 +4772,8 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
47724772
case DTK_JULIAN:
47734773
if (retnumeric)
47744774
PG_RETURN_NUMERIC(numeric_add_opt_error(int64_to_numeric(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)),
4775-
numeric_div_opt_error(int64_to_numeric(((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) * 1000000LL + fsec),
4776-
int64_to_numeric(SECS_PER_DAY * 1000000LL),
4775+
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),
4776+
int64_to_numeric(SECS_PER_DAY * INT64CONST(1000000)),
47774777
NULL),
47784778
NULL));
47794779
else
@@ -4962,7 +4962,7 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
49624962
break;
49634963

49644964
case DTK_MICROSEC:
4965-
intresult = tm->tm_sec * 1000000 + fsec;
4965+
intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
49664966
break;
49674967

49684968
case DTK_MILLISEC:
@@ -4971,7 +4971,7 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
49714971
* tm->tm_sec * 1000 + fsec / 1000
49724972
* = (tm->tm_sec * 1'000'000 + fsec) / 1000
49734973
*/
4974-
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 3));
4974+
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
49754975
else
49764976
PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
49774977
break;
@@ -4982,7 +4982,7 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
49824982
* tm->tm_sec + fsec / 1'000'000
49834983
* = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
49844984
*/
4985-
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 6));
4985+
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
49864986
else
49874987
PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
49884988
break;
@@ -5046,8 +5046,8 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
50465046
case DTK_JULIAN:
50475047
if (retnumeric)
50485048
PG_RETURN_NUMERIC(numeric_add_opt_error(int64_to_numeric(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)),
5049-
numeric_div_opt_error(int64_to_numeric(((((tm->tm_hour * MINS_PER_HOUR) + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) * 1000000LL + fsec),
5050-
int64_to_numeric(SECS_PER_DAY * 1000000LL),
5049+
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),
5050+
int64_to_numeric(SECS_PER_DAY * INT64CONST(1000000)),
50515051
NULL),
50525052
NULL));
50535053
else
@@ -5191,7 +5191,7 @@ interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
51915191
switch (val)
51925192
{
51935193
case DTK_MICROSEC:
5194-
intresult = tm->tm_sec * 1000000 + fsec;
5194+
intresult = tm->tm_sec * INT64CONST(1000000) + fsec;
51955195
break;
51965196

51975197
case DTK_MILLISEC:
@@ -5200,7 +5200,7 @@ interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
52005200
* tm->tm_sec * 1000 + fsec / 1000
52015201
* = (tm->tm_sec * 1'000'000 + fsec) / 1000
52025202
*/
5203-
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 3));
5203+
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 3));
52045204
else
52055205
PG_RETURN_FLOAT8(tm->tm_sec * 1000.0 + fsec / 1000.0);
52065206
break;
@@ -5211,7 +5211,7 @@ interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
52115211
* tm->tm_sec + fsec / 1'000'000
52125212
* = (tm->tm_sec * 1'000'000 + fsec) / 1'000'000
52135213
*/
5214-
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * 1000000LL + fsec, 6));
5214+
PG_RETURN_NUMERIC(int64_div_fast_to_numeric(tm->tm_sec * INT64CONST(1000000) + fsec, 6));
52155215
else
52165216
PG_RETURN_FLOAT8(tm->tm_sec + fsec / 1000000.0);
52175217
break;

0 commit comments

Comments
 (0)