sql: current_time/current_timestamp/localtime/localtimestamp truncate the INT8 precision to 32 bits, so current_time(4294967297) is accepted as current_time(1)

Aperta Adatta ai principianti
#176,245 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go

Direzione di ricerca

Leggi gli overload di precisione in pkg/sql/sem/builtins/builtins.go alle righe indicate (10680, 10694, 10707, 10773 e 10787). Verifica che l’argomento DInt rientri nell’intervallo 0..6 prima di convertirlo in int32, preservando il valore originale nell’errore di intervallo. Aggiungi o esegui test SQL mirati per tutte e quattro le funzioni, inclusi valori oltre l’intervallo a 32 bit; il lavoro è completato quando ogni argomento fuori intervallo restituisce SQLSTATE 22003 con la precisione fornita nell’errore.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

C-bug O-community X-blathers-untriaged

Describe the problem

The precision overloads of current_time, current_timestamp, localtime and localtimestamp take an INT8
argument, convert it to int32 and only then check that it is between 0 and 6:

prec := int32(tree.MustBeDInt(args[0]))
if prec < 0 || prec > 6 {
    return nil, pgerror.Newf(pgcode.NumericValueOutOfRange, "precision %d out of range", prec)
}

(pkg/sql/sem/builtins/builtins.go#L10680-L10683,
the same lines at
#L10694,
#L10707,
#L10773 and
#L10787.)

So the precision is taken modulo 2^32: 4294967297 (2^32 + 1) is accepted and used as precision 1, while 7 is
rejected. When the narrowed value is out of range, the error names the narrowed value, not the argument:
current_time(4294967303) fails with precision 7 out of range. The argument can also come from an INT8
column, not only from a literal.

The TIME(p)/TIMESTAMP(p) type syntax is not affected: '12:34:56'::TIME(4294967297) is rejected with
numeric constant out of int32 range.

To Reproduce

  1. Start a node: cockroach start-single-node --insecure.
  2. Run in cockroach sql --insecure:
SELECT current_time(7);
SELECT current_timestamp(4294967297) AS p_2p32_plus_1, current_timestamp(1) AS p_1, current_timestamp(6) AS p_6;
SELECT current_time(4294967297) = current_time(1)           AS time_2p32_plus_1_is_1,
       current_timestamp(4294967296) = current_timestamp(0) AS timestamptz_2p32_is_0,
       localtime(4294967299) = localtime(3)                 AS localtime_2p32_plus_3_is_3,
       localtimestamp(-4294967290) = localtimestamp(6)      AS localts_6_minus_2p32_is_6;
SELECT current_time(4294967303);
SELECT current_timestamp(9223372036854775807);
SELECT current_timestamp(p) = current_timestamp(0) AS from_int8_column FROM (VALUES (4294967296:::INT8)) AS v(p);

Output on the master edge build (the clock values differ from run to run; the rest does not):

ERROR: precision 7 out of range
SQLSTATE: 22003

       p_2p32_plus_1       |           p_1            |              p_6
---------------------------+--------------------------+--------------------------------
  2026-11-16 04:17:20.4+00 | 2026-11-16 04:17:20.4+00 | 2026-11-16 04:17:20.390942+00
(1 row)

  time_2p32_plus_1_is_1 | timestamptz_2p32_is_0 | localtime_2p32_plus_3_is_3 | localts_6_minus_2p32_is_6
------------------------+-----------------------+----------------------------+----------------------------
            t           |           t           |             t              |             t
(1 row)

ERROR: precision 7 out of range
SQLSTATE: 22003

ERROR: precision -1 out of range
SQLSTATE: 22003

  from_int8_column
--------------------
         t
(1 row)

Expected behavior

ERROR: precision 4294967297 out of range (SQLSTATE 22003) for every argument outside 0..6, as for 7 and -1, with
the argument as given in the message. The documentation says "CockroachDB supports precision levels from 0
(seconds) to 6 (microseconds) for TIME/TIMETZ values" (https://www.cockroachlabs.com/docs/dev/time#precision),
and the same for TIMESTAMP/TIMESTAMPTZ (https://www.cockroachlabs.com/docs/dev/timestamp#precision).
In PostgreSQL 18.6 the precision of these functions must be an integer constant, and a value beyond 32 bits is
rejected by the parser:

postgres=# SELECT current_time(4294967297);
ERROR:  syntax error at or near "4294967297"

A possible fix is to check the DInt before the conversion in all five overloads (or in one shared helper):

p := tree.MustBeDInt(args[0])
if p < 0 || p > 6 {
    return nil, pgerror.Newf(pgcode.NumericValueOutOfRange, "precision %d out of range", p)
}
prec := int32(p)

Additional data / screenshots

No schema is needed; all statements are constant expressions. The same INT8-to-int32 pattern was reported for
the geospatial builtins in #176134 and for round(float, int) in #176135; those fixes are in other files and
would not cover these overloads.

Environment:

  • CockroachDB version: CCL v27.1.0-alpha.00000000-dev-a68d28214f60288e146c3731a0d82d45c29d0477 (master edge build, 2026/11/15 07:08:17, go1.27.4); also v26.4.0
  • Server OS: Linux x86-64 (Ubuntu 22.04), cockroach start-single-node --insecure
  • Client app: cockroach sql

Additional context

Impact: low. An out-of-range precision computed by an application is silently accepted and gives a value rounded
to an unrelated precision, and when it is rejected the error reports a different number than the one passed.

Jira issue: CRDB-72232

Lingua principale
Go
Stelle
32.6k
Fork
4.2k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cockroachdb/cockroach

Tutte le issue di cockroachdb/cockroach

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.