Silence some Coverity warnings and improve code consistency.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 11 Apr 2021 21:02:04 +0000 (17:02 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 11 Apr 2021 21:02:04 +0000 (17:02 -0400)
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
src/backend/utils/adt/timestamp.c

index 83036e5985e504ed2a57ab586448953eb94ca207..0bea16cb671ac90461627ed11ef7441c56bd32a9 100644 (file)
@@ -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);
    }
index 280ee7f92bab83ea750cc5df0bbf8b326f7030de..3a93e92e4036dc0afe0fcf81ca7251749b599166 100644 (file)
@@ -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;