Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bZDCb-0004l4-5W for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Aug 2016 08:24:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bZDCa-0006ir-Nj for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Aug 2016 08:24:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bZDCa-0006if-75 for pgsql-hackers@postgresql.org; Mon, 15 Aug 2016 08:24:56 +0000 Received: from newmail.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bZDCV-0005f3-9a for pgsql-hackers@postgresql.org; Mon, 15 Aug 2016 08:24:55 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 1BA8B21C2D3A; Mon, 15 Aug 2016 11:24:50 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru Received: from mail.postgrespro.ru ([127.0.0.1]) by localhost (mail.postgrespro.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 16VgNigs01gC; Mon, 15 Aug 2016 11:24:50 +0300 (MSK) Received: from [192.168.27.153] (unknown [192.168.27.1]) by mail.postgrespro.ru (Postfix) with ESMTPSA id E211D21C2D38; Mon, 15 Aug 2016 11:24:49 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1471249489; bh=ouYjtQWLJ7S6z5O97Rse/RhM7lB8wbhLSbaAzzuTNwA=; h=Subject:To:References:Cc:From:Date:In-Reply-To; b=ozYuHMGwZIsTV5LFX/FwF5GNFys49smIr9s5KmOKNc1zq7XIo0Ij2YtxTuohlphWX 3xyZtGt6p5Fa1mQ2y9Xi1KMF7LPuKBLFqsnbPotHxto35p7iFtXMoWIHRT+MtBcmEO 4xFNSvpq+wwxscB73QnBrwB4VE97bHz5jMvqKvFs= Subject: Re: to_date_valid() To: Andreas 'ads' Scherbaum , "Joshua D. Drake" , Peter Eisentraut , Albe Laurenz , 'Andreas Karlsson *EXTERN*' , Pavel Stehule References: <57786490.9010201@wars-nicht.de> <5778EA94.1020706@wars-nicht.de> <2bc6bcef-cada-1ac7-f9a2-2f6128ca4c3c@proxel.se> <579823DF.3080903@commandprompt.com> Cc: Jaime Casanova , Euler Taveira , Pg Hackers From: Artur Zakirov Message-ID: <95738e12-6ed6-daf5-9dcf-6336072e6b15@postgrespro.ru> Date: Mon, 15 Aug 2016 11:24:07 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------99A19D8DC668D85726148FF0" X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------99A19D8DC668D85726148FF0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 14.08.2016 01:52, Andreas 'ads' Scherbaum wrote: > > Attached is a patch to "do the right thing". The verification is in > "to_date()" now, the extra function is removed. Regression tests are > updated - two or three of them returned a wrong date before, and still > passed. They fail now. Documentation is also updated. > > > Regards, > Is it right and "true" way to validate date by extra transforming and comparison? Maybe validate date by using ValidateDate(). Attached sample patch. -- Artur Zakirov Postgres Professional: http://www.postgrespro.com Russian Postgres Company --------------99A19D8DC668D85726148FF0 Content-Type: text/x-patch; name="to-date-valid.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="to-date-valid.patch" diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index bbd97dc..c0048c9 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -3563,7 +3563,9 @@ do_to_timestamp(text *date_txt, text *fmt, { FormatNode *format; TmFromChar tmfc; - int fmt_len; + int fmt_len, + fmask = 0; /* Bit mask for ValidateDate() */ + char *date_str = NULL; ZERO_tmfc(&tmfc); ZERO_tm(tm); @@ -3574,7 +3576,6 @@ do_to_timestamp(text *date_txt, text *fmt, if (fmt_len) { char *fmt_str; - char *date_str; bool incache; fmt_str = text_to_cstring(fmt); @@ -3630,7 +3631,6 @@ do_to_timestamp(text *date_txt, text *fmt, DCH_from_char(format, date_str, &tmfc); - pfree(date_str); pfree(fmt_str); if (!incache) pfree(format); @@ -3706,6 +3706,7 @@ do_to_timestamp(text *date_txt, text *fmt, if (tmfc.bc && tm->tm_year > 0) tm->tm_year = -(tm->tm_year - 1); } + fmask |= DTK_M(YEAR); } else if (tmfc.cc) /* use first year of century */ { @@ -3717,10 +3718,14 @@ do_to_timestamp(text *date_txt, text *fmt, else /* +1 because year == 599 is 600 BC */ tm->tm_year = tmfc.cc * 100 + 1; + fmask |= DTK_M(YEAR); } if (tmfc.j) + { j2date(tmfc.j, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); + fmask |= DTK_DATE_M; + } if (tmfc.ww) { @@ -3734,6 +3739,7 @@ do_to_timestamp(text *date_txt, text *fmt, isoweekdate2date(tmfc.ww, tmfc.d, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); else isoweek2date(tmfc.ww, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); + fmask |= DTK_DATE_M; } else tmfc.ddd = (tmfc.ww - 1) * 7 + 1; @@ -3744,11 +3750,17 @@ do_to_timestamp(text *date_txt, text *fmt, if (tmfc.d) tm->tm_wday = tmfc.d - 1; /* convert to native numbering */ if (tmfc.dd) + { tm->tm_mday = tmfc.dd; + fmask |= DTK_M(DAY); + } if (tmfc.ddd) tm->tm_yday = tmfc.ddd; if (tmfc.mm) + { tm->tm_mon = tmfc.mm; + fmask |= DTK_M(MONTH); + } if (tmfc.ddd && (tm->tm_mon <= 1 || tm->tm_mday <= 1)) { @@ -3771,6 +3783,7 @@ do_to_timestamp(text *date_txt, text *fmt, j0 = isoweek2j(tm->tm_year, 1) - 1; j2date(j0 + tmfc.ddd, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); + fmask |= DTK_DATE_M; } else { @@ -3793,9 +3806,24 @@ do_to_timestamp(text *date_txt, text *fmt, if (tm->tm_mday <= 1) tm->tm_mday = tmfc.ddd - y[i - 1]; + + fmask |= DTK_M(MONTH) | DTK_M(DAY); } } + /* Validate date with bit mask received above */ + if (fmask != 0 && date_str) + { + if (ValidateDate(fmask, false, false, false, tm) != 0) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW), + errmsg("date/time field value out of range: \"%s\"", + date_str))); + } + + if (date_str) + pfree(date_str); + #ifdef HAVE_INT64_TIMESTAMP if (tmfc.ms) *fsec += tmfc.ms * 1000; --------------99A19D8DC668D85726148FF0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --------------99A19D8DC668D85726148FF0--