Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WhntO-0005lj-UZ for pgsql-bugs@arkaria.postgresql.org; Tue, 06 May 2014 22:31:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WhntO-0008Nf-DP for pgsql-bugs@arkaria.postgresql.org; Tue, 06 May 2014 22:31:18 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WhntN-0008MX-ER for pgsql-bugs@postgresql.org; Tue, 06 May 2014 22:31:17 +0000 Received: from mail-pa0-f43.google.com ([209.85.220.43]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WhntK-0001mW-Qb for pgsql-bugs@postgresql.org; Tue, 06 May 2014 22:31:16 +0000 Received: by mail-pa0-f43.google.com with SMTP id hz1so139570pad.2 for ; Tue, 06 May 2014 15:31:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:subject:from:to:date:content-type :mime-version; bh=cEAdHfHdaT5itHt6VwN/IdEM5KGv33XUkQ8sbih9Q6s=; b=TQPv1gHyo97mT1inrQWTWUfRzaPdRzrkwvd1x9zbk2depIlxrg7KPrmZ/dsnjDUPZ1 ktFCACvw7T3+zRwInGFOQy6/ejCjNUBhhbuwexN1NhjzK0wTlFFq+z7/ow6Fin+qskiI naCtKo0MNruj1OVlEPDRL+kIGDUBeJi3asiSufCleAEqYM/jKAQSrdtVL4F0GYGh0IlJ BPmLcJn2gTGDslFyKyvjO7NNUQ4QLABmNt8kz7ToLmhuR6HItIXaTNqjMZ/XhWjbRdHs 0enVpO9Sw1j6u18RyEQWKGG0H/jqlIRFw0RUlJ3FHWS6nEBQz+1RFmksNZRhZYz8sWiK e3jw== X-Gm-Message-State: ALoCoQlL+feSuHolIxPpMzi7JIGqgWAPNg9y4tI4vq9C8E9NAkeSd+1Z5ll1lsMif5X545TMwbm/ X-Received: by 10.67.4.169 with SMTP id cf9mr11205336pad.45.1399415473521; Tue, 06 May 2014 15:31:13 -0700 (PDT) Received: from ?IPv6:::1? ([2600:3c01::f03c:91ff:fe93:2ae4]) by mx.google.com with ESMTPSA id gg3sm2563479pbc.34.2014.05.06.15.31.12 for (version=SSLv3 cipher=RC4-SHA bits=128/128); Tue, 06 May 2014 15:31:12 -0700 (PDT) Message-ID: <1399415471.27807.49.camel@sussancws0025> Subject: can insert timestamp value that can't be read From: Jeff Davis To: pgsql-bugs@postgresql.org Date: Tue, 06 May 2014 15:31:11 -0700 Content-Type: multipart/mixed; boundary="=-RgOF8Ai3z4ttuBtQYRdX" X-Mailer: Evolution 3.2.3-0ubuntu6 Mime-Version: 1.0 X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --=-RgOF8Ai3z4ttuBtQYRdX Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit Example: => create table r(t timestamp); CREATE TABLE => insert into r values( timestamp '294277-01-09 00:00:00' + interval '36 hour' ); INSERT 0 1 => select * from r; ERROR: timestamp out of range (A similar case with different values exists when compiled with --disable-integer-datetimes.) It looks like timestamp[tz]_pl_interval() is not doing proper validation in all paths. Patch attached. I looked for other areas that might be affected, but none jumped out. The patch does not introduce any tests because I couldn't think of a good way to test both integer and float timestamps properly. Regards, Jeff Davis --=-RgOF8Ai3z4ttuBtQYRdX Content-Disposition: attachment; filename="timestamp_validation.patch" Content-Type: text/x-patch; name="timestamp_validation.patch"; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit *** a/src/backend/utils/adt/timestamp.c --- b/src/backend/utils/adt/timestamp.c *************** *** 2923,2934 **** timestamp_pl_interval(PG_FUNCTION_ARGS) result = timestamp; else { if (span->month != 0) { - struct pg_tm tt, - *tm = &tt; - fsec_t fsec; - if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0) ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), --- 2923,2934 ---- result = timestamp; else { + struct pg_tm tt, + *tm = &tt; + fsec_t fsec; + if (span->month != 0) { if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0) ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), *************** *** 2958,2966 **** timestamp_pl_interval(PG_FUNCTION_ARGS) if (span->day != 0) { - struct pg_tm tt, - *tm = &tt; - fsec_t fsec; int julian; if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0) --- 2958,2963 ---- *************** *** 2978,2984 **** timestamp_pl_interval(PG_FUNCTION_ARGS) errmsg("timestamp out of range"))); } ! timestamp += span->time; result = timestamp; } --- 2975,2997 ---- errmsg("timestamp out of range"))); } ! if (span->time != 0) ! { ! /* add time directly to timestamp */ ! timestamp += span->time; ! ! /* convert back and forth only for consistent error checking */ ! if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0) ! ereport(ERROR, ! (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), ! errmsg("timestamp out of range"))); ! ! if (tm2timestamp(tm, fsec, NULL, ×tamp) != 0) ! ereport(ERROR, ! (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), ! errmsg("timestamp out of range"))); ! } ! result = timestamp; } *************** *** 3023,3034 **** timestamptz_pl_interval(PG_FUNCTION_ARGS) result = timestamp; else { if (span->month != 0) { - struct pg_tm tt, - *tm = &tt; - fsec_t fsec; - if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0) ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), --- 3036,3047 ---- result = timestamp; else { + struct pg_tm tt, + *tm = &tt; + fsec_t fsec; + if (span->month != 0) { if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0) ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), *************** *** 3060,3068 **** timestamptz_pl_interval(PG_FUNCTION_ARGS) if (span->day != 0) { - struct pg_tm tt, - *tm = &tt; - fsec_t fsec; int julian; if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0) --- 3073,3078 ---- *************** *** 3082,3088 **** timestamptz_pl_interval(PG_FUNCTION_ARGS) errmsg("timestamp out of range"))); } ! timestamp += span->time; result = timestamp; } --- 3092,3114 ---- errmsg("timestamp out of range"))); } ! if (span->time != 0) ! { ! /* add time directly to timestamp */ ! timestamp += span->time; ! ! /* convert back and forth only for consistent error checking */ ! if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0) ! ereport(ERROR, ! (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), ! errmsg("timestamp out of range"))); ! ! if (tm2timestamp(tm, fsec, NULL, ×tamp) != 0) ! ereport(ERROR, ! (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), ! errmsg("timestamp out of range"))); ! } ! result = timestamp; } --=-RgOF8Ai3z4ttuBtQYRdX Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --=-RgOF8Ai3z4ttuBtQYRdX--