summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorunknown <petr/cps@mysql.com/owlet.local>2006-11-01 16:47:40 +0300
committerunknown <petr/cps@mysql.com/owlet.local>2006-11-01 16:47:40 +0300
commit8a7bc052885494b83fed51d785d9fc4b1cfa9df1 (patch)
treee2642512c7a6f4cbdaa661730aca0413380e045e /sql
parent5d46e2993389f3cffe2a37374ba61334f86f7e5e (diff)
downloadmariadb-git-8a7bc052885494b83fed51d785d9fc4b1cfa9df1.tar.gz
Fix Bug #9191 "TIMESTAMP/from_unixtime() no longer accepts 2^31-1"
(4.1 version, with post-review fixes) The fix for another Bug (6439) limited FROM_UNIXTIME() to TIMESTAMP_MAX_VALUE which is 2145916799 or 2037-12-01 23:59:59 GMT, however unix timestamp in general is not considered to be limited by this value. All dates up to power(2,31)-1 are valid. This patch extends allowed TIMESTAMP range so, that max TIMESTAMP value is power(2,31)-1. It also corrects FROM_UNIXTIME() and UNIX_TIMESTAMP() functions, so that max allowed UNIX_TIMESTAMP() is power(2,31)-1. FROM_UNIXTIME() is fixed accordingly to allow conversion of dates up to 2038-01-19 03:14:07 UTC. The patch also fixes CONVERT_TZ() function to allow extended range of dates. The main problem solved in the patch is possible overflows of variables, used in broken-time representation to time_t conversion (required for UNIX_TIMESTAMP). acinclude.m4: Add new macro to check time_t range configure.in: Call the macro to check time_t range include/my_time.h: Move time-related defines to proper place. Add a function to perform a rough check if a TIMESTAMP value fits into the boundaries. Note: it is defined as "static inline", as otherwise libmysql won't compile (due to the way how gcc handles "inline" directive). mysql-test/r/func_time.result: Update test result mysql-test/r/timezone.result: Update test result mysql-test/r/timezone2.result: Update test result mysql-test/t/func_time.test: Add test for Bug#9191 and update test to be consistent with new TIMESTAMP boundaries mysql-test/t/timezone.test: Update old tests to be consistent with new TIMESTAMP boundaries mysql-test/t/timezone2.test: Update tests for convert_tz to be consistent with new TIMESTAMP boundaries sql/item_timefunc.cc: Fix convert_tz to allow dates from the new (extended) TIMESTAMP range sql/mysql_priv.h: Move time handling defaults to my_time.h sql-common/my_time.c: Because of increased TIMESTAMP_MAX_VALUE overflows in my_system_gmt_sec() became possible. Here we make it safe against the overflows by stepping back from the boundary dates which are likely to trigger them. sql/time.cc: Update TIME_to_timestamp to allow conversion of extended date range sql/tztime.cc: Fix new (4.1) implementation of broken-down time representation to time_t conversion routine to avoid overflows during conversion of boundary dates mysql-test/r/timezone4.result: New BitKeeper file ``mysql-test/r/timezone4.result'' mysql-test/t/timezone4-master.opt: New BitKeeper file ``mysql-test/t/timezone4-master.opt'' mysql-test/t/timezone4.test: New BitKeeper file ``mysql-test/t/timezone4.test''
Diffstat (limited to 'sql')
-rw-r--r--sql/item_timefunc.cc13
-rw-r--r--sql/mysql_priv.h6
-rw-r--r--sql/time.cc9
-rw-r--r--sql/tztime.cc76
4 files changed, 75 insertions, 29 deletions
diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc
index febc92e34f6..68eb2178804 100644
--- a/sql/item_timefunc.cc
+++ b/sql/item_timefunc.cc
@@ -1827,15 +1827,10 @@ bool Item_func_convert_tz::get_date(TIME *ltime,
return 1;
}
- /* Check if we in range where we treat datetime values as non-UTC */
- if (ltime->year < TIMESTAMP_MAX_YEAR && ltime->year > TIMESTAMP_MIN_YEAR ||
- ltime->year==TIMESTAMP_MAX_YEAR && ltime->month==1 && ltime->day==1 ||
- ltime->year==TIMESTAMP_MIN_YEAR && ltime->month==12 && ltime->day==31)
- {
- my_time_tmp= from_tz->TIME_to_gmt_sec(ltime, &not_used);
- if (my_time_tmp >= TIMESTAMP_MIN_VALUE && my_time_tmp <= TIMESTAMP_MAX_VALUE)
- to_tz->gmt_sec_to_TIME(ltime, my_time_tmp);
- }
+ my_time_tmp= from_tz->TIME_to_gmt_sec(ltime, &not_used);
+ /* my_time_tmp is guranteed to be in the allowed range */
+ if (my_time_tmp)
+ to_tz->gmt_sec_to_TIME(ltime, my_time_tmp);
null_value= 0;
return 0;
diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h
index 4a5658c5ccf..c0525198d4a 100644
--- a/sql/mysql_priv.h
+++ b/sql/mysql_priv.h
@@ -142,12 +142,6 @@ MY_LOCALE *my_locale_by_name(const char *name);
/* Characters shown for the command in 'show processlist' */
#define PROCESS_LIST_WIDTH 100
-/* Time handling defaults */
-#define TIMESTAMP_MAX_YEAR 2038
-#define YY_PART_YEAR 70
-#define TIMESTAMP_MIN_YEAR (1900 + YY_PART_YEAR - 1)
-#define TIMESTAMP_MAX_VALUE 2145916799
-#define TIMESTAMP_MIN_VALUE 1
#define PRECISION_FOR_DOUBLE 53
#define PRECISION_FOR_FLOAT 24
diff --git a/sql/time.cc b/sql/time.cc
index ef832ac5a70..0e428e8bc40 100644
--- a/sql/time.cc
+++ b/sql/time.cc
@@ -230,14 +230,11 @@ my_time_t TIME_to_timestamp(THD *thd, const TIME *t, bool *in_dst_time_gap)
*in_dst_time_gap= 0;
- if (t->year < TIMESTAMP_MAX_YEAR && t->year > TIMESTAMP_MIN_YEAR ||
- t->year == TIMESTAMP_MAX_YEAR && t->month == 1 && t->day == 1 ||
- t->year == TIMESTAMP_MIN_YEAR && t->month == 12 && t->day == 31)
+ timestamp= thd->variables.time_zone->TIME_to_gmt_sec(t, in_dst_time_gap);
+ if (timestamp)
{
thd->time_zone_used= 1;
- timestamp= thd->variables.time_zone->TIME_to_gmt_sec(t, in_dst_time_gap);
- if (timestamp >= TIMESTAMP_MIN_VALUE && timestamp <= TIMESTAMP_MAX_VALUE)
- return timestamp;
+ return timestamp;
}
/* If we are here we have range error. */
diff --git a/sql/tztime.cc b/sql/tztime.cc
index b0a32748998..60c5e8efd71 100644
--- a/sql/tztime.cc
+++ b/sql/tztime.cc
@@ -885,9 +885,14 @@ TIME_to_gmt_sec(const TIME *t, const TIME_ZONE_INFO *sp, bool *in_dst_time_gap)
my_time_t local_t;
uint saved_seconds;
uint i;
+ int shift= 0;
DBUG_ENTER("TIME_to_gmt_sec");
+ if (!validate_timestamp_range(t))
+ return 0;
+
+
/* We need this for correct leap seconds handling */
if (t->second < SECS_PER_MIN)
saved_seconds= 0;
@@ -895,11 +900,29 @@ TIME_to_gmt_sec(const TIME *t, const TIME_ZONE_INFO *sp, bool *in_dst_time_gap)
saved_seconds= t->second;
/*
- NOTE If we want to convert full my_time_t range without MySQL
- restrictions we should catch overflow here somehow.
+ NOTE: to convert full my_time_t range we do a shift of the
+ boundary dates here to avoid overflow of my_time_t.
+ We use alike approach in my_system_gmt_sec().
+
+ However in that function we also have to take into account
+ overflow near 0 on some platforms. That's because my_system_gmt_sec
+ uses localtime_r(), which doesn't work with negative values correctly
+ on platforms with unsigned time_t (QNX). Here we don't use localtime()
+ => we negative values of local_t are ok.
*/
- local_t= sec_since_epoch(t->year, t->month, t->day,
+ if ((t->year == TIMESTAMP_MAX_YEAR) && (t->month == 1) && t->day > 4)
+ {
+ /*
+ We will pass (t->day - shift) to sec_since_epoch(), and
+ want this value to be a positive number, so we shift
+ only dates > 4.01.2038 (to avoid owerflow).
+ */
+ shift= 2;
+ }
+
+
+ local_t= sec_since_epoch(t->year, t->month, (t->day - shift),
t->hour, t->minute,
saved_seconds ? 0 : t->second);
@@ -918,6 +941,22 @@ TIME_to_gmt_sec(const TIME *t, const TIME_ZONE_INFO *sp, bool *in_dst_time_gap)
/* binary search for our range */
i= find_time_range(local_t, sp->revts, sp->revcnt);
+ /*
+ As there are no offset switches at the end of TIMESTAMP range,
+ we could simply check for overflow here (and don't need to bother
+ about DST gaps etc)
+ */
+ if (shift)
+ {
+ if (local_t > (TIMESTAMP_MAX_VALUE - shift*86400L +
+ sp->revtis[i].rt_offset - saved_seconds))
+ {
+ DBUG_RETURN(0); /* my_time_t overflow */
+ }
+ else
+ local_t+= shift*86400L;
+ }
+
if (sp->revtis[i].rt_type)
{
/*
@@ -927,10 +966,16 @@ TIME_to_gmt_sec(const TIME *t, const TIME_ZONE_INFO *sp, bool *in_dst_time_gap)
beginning of the gap.
*/
*in_dst_time_gap= 1;
- DBUG_RETURN(sp->revts[i] - sp->revtis[i].rt_offset + saved_seconds);
+ local_t= sp->revts[i] - sp->revtis[i].rt_offset + saved_seconds;
}
else
- DBUG_RETURN(local_t - sp->revtis[i].rt_offset + saved_seconds);
+ local_t= local_t - sp->revtis[i].rt_offset + saved_seconds;
+
+ /* check for TIMESTAMP_MAX_VALUE was already done above */
+ if (local_t < TIMESTAMP_MIN_VALUE)
+ local_t= 0;
+
+ DBUG_RETURN(local_t);
}
@@ -1294,9 +1339,24 @@ Time_zone_offset::Time_zone_offset(long tz_offset_arg):
my_time_t
Time_zone_offset::TIME_to_gmt_sec(const TIME *t, bool *in_dst_time_gap) const
{
- return sec_since_epoch(t->year, t->month, t->day,
- t->hour, t->minute, t->second) -
- offset;
+ my_time_t local_t;
+
+ /*
+ Check timestamp range.we have to do this as calling function relies on
+ us to make all validation checks here.
+ */
+ if (!validate_timestamp_range(t))
+ return 0;
+
+ local_t= sec_since_epoch(t->year, t->month, t->day,
+ t->hour, t->minute, t->second) -
+ offset;
+
+ if (local_t >= TIMESTAMP_MIN_VALUE && local_t <= TIMESTAMP_MAX_VALUE)
+ return local_t;
+
+ /* range error*/
+ return 0;
}