Bug #50087 Interval arithmetic for Event_queue_element is not portable.
Submitted: 5 Jan 2010 12:37 Modified: 14 Oct 2010 14:04
Reporter: Tor Didriksen Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: General Severity:S3 (Non-critical)
Version:5.1, 5.5.99 OS:Any
Assigned to: Tor Didriksen CPU Architecture:Any
Triage: Triaged: D3 (Medium)

[5 Jan 2010 12:37] Tor Didriksen
Description:
These tests started to fail in January 2010:
backup.backup_xpfm_compat_restore_lctn0  
backup.backup_xpfm_compat_restore_lctn1

There's now lots of Warnings:
+#	1441	Datetime function: datetime field overflow
due to bogus unsigned/signed arithmetic when computing intervals.
I guess this started to fail simply because we passed newyear.

It turns out that this arithmetic in get_next_time() is not portable:

    long diff_months= (long) (local_now.year - local_start.year)*12 +
                             (local_now.month - local_start.month);

On 64-bit platforms we now get a month interval of 4294967292
in date_add_interval(), which will set an ER_DATETIME_FUNCTION_OVERFLOW
warning.

How to repeat:
Run these tests on a 64bit platform.
backup.backup_xpfm_compat_restore_lctn0  
backup.backup_xpfm_compat_restore_lctn1
(see pushbuild for verification)

Suggested fix:
Never perform subtraction on unsigned values!
(or even better: don't use unsigned)
[6 Jan 2010 12:59] Tor Didriksen
See also http://bugs.mysql.com/bug.php?id=50077&thanks=1&notify=131
[6 Jan 2010 13:04] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/96119

2958 Tor Didriksen	2010-01-06
      Bug #50087  	Interval arithmetic for Event_queue_element is not portable.
      
      Subtraction of two unsigned months yielded a (very large) positive value.
      Conversion of this to a signed value was not necessarily well defined.
      
      Solution: do the subtraction on signed values.
     @ mysql-test/r/events_scheduling.result
        Add test case.
     @ mysql-test/t/events_scheduling.test
        Add test case.
     @ sql/event_data_objects.cc
        Convert month to signed before doing the subtraction.
[6 Jan 2010 14:01] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/96126

2958 Tor Didriksen	2010-01-06
      Bug #50087  	Interval arithmetic for Event_queue_element is not portable.
      
      Subtraction of two unsigned months yielded a (very large) positive value.
      Conversion of this to a signed value was not necessarily well defined.
      
      Solution: do the subtraction on signed values.
     @ mysql-test/r/events_scheduling.result
        Add test case.
     @ mysql-test/t/events_scheduling.test
        Add test case.
     @ sql/event_data_objects.cc
        Convert month to signed before doing the subtraction.
[6 Jan 2010 14:06] Guilhem Bichot
simple bugfix, no second review needed
[6 Jan 2010 14:47] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/96132

3811 Tor Didriksen	2010-01-06 [merge]
      Merge of
        Bug #50087    Interval arithmetic for Event_queue_element is not portable.
        
        Subtraction of two unsigned months yielded a (very large) positive value.
        Conversion of this to a signed value was not necessarily well defined.
        
        Solution: do the subtraction on signed values.
     @ mysql-test/r/events_scheduling.result
            Add test case.
     @ mysql-test/suite/backup/t/disabled.def
        Re-enable tests, since underlying bug is fixed.
     @ mysql-test/t/events_scheduling.test
            Add test case.
     @ sql/event_data_objects.cc
            Convert to signed before doing the subtraction.
[6 Jan 2010 14:50] Tor Didriksen
Pushed to
bzr+ssh://bk-internal.mysql.com/bzrroot/server/mysql-next-mr-bugfixing

Merged and pushed to
bzr+ssh://bk-internal.mysql.com/bzrroot/server/mysql-6.0-codebase-bugfixing
[12 Jan 2010 16:25] Bugs System
Pushed into 6.0.14-alpha (revid:alik@sun.com-20100112162328-2sblcul1kl08bbib) (version source revid:guilhem@mysql.com-20100108092756-k0zzf4kvx9b7bh38) (merge vers: 6.0.14-alpha) (pib:15)
[12 Jan 2010 16:25] Bugs System
Pushed into mysql-next-mr (revid:alik@sun.com-20100112162142-nrjoxn6h2nrnh2o0) (version source revid:guilhem@mysql.com-20100108092756-k0zzf4kvx9b7bh38) (pib:15)
[13 Jan 2010 18:04] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/96809

3062 Ingo Struewing	2010-01-13
      WL#5101 - MySQL Backup back port
      Merged revid:tor.didriksen@sun.com-20100106140051-3j2iuag63eltsr2e
        Bug #50087    Interval arithmetic for Event_queue_element is not portable.
        
        Subtraction of two unsigned months yielded a (very large) positive value.
        Conversion of this to a signed value was not necessarily well defined.
        
        Solution: do the subtraction on signed values.
     @ mysql-test/r/events_scheduling.result
        WL#5101 - MySQL Backup back port
            Add test case.
     @ mysql-test/t/events_scheduling.test
        WL#5101 - MySQL Backup back port
            Add test case.
     @ sql/event_data_objects.cc
        WL#5101 - MySQL Backup back port
            Convert month to signed before doing the subtraction.
[6 Mar 2010 10:52] Bugs System
Pushed into 5.5.3-m3 (revid:alik@sun.com-20100306103849-hha31z2enhh7jwt3) (version source revid:guilhem@mysql.com-20100108092756-k0zzf4kvx9b7bh38) (merge vers: 6.0.14-alpha) (pib:16)
[14 Mar 2010 6:31] Sveta Smirnova
Bug exists in 5.1 as well.
[14 Mar 2010 6:32] Sveta Smirnova
Test case demonstrating problem in 5.1:

delimiter //;

CREATE EVENT `subscription_addpartition_ev2` ON SCHEDULE EVERY 1 MONTH STARTS '2009-06-09 13:36:27' ON COMPLETION NOT PRESERVE ENABLE DO
BEGIN
    select 1;
END//

show warnings //

Repeatable only on 64-bit box
[19 May 2010 9:29] Tor Didriksen
Backported and pushed to mysql-5.1-bugteam
Merged (unchanged) and pushed to mysql-pe
[28 May 2010 5:59] Bugs System
Pushed into mysql-next-mr (revid:alik@sun.com-20100524190136-egaq7e8zgkwb9aqi) (version source revid:alik@sun.com-20100524190136-egaq7e8zgkwb9aqi) (pib:16)
[28 May 2010 6:28] Bugs System
Pushed into 6.0.14-alpha (revid:alik@sun.com-20100524190941-nuudpx60if25wsvx) (version source revid:alik@sun.com-20100524190409-5w4l7mje1wk1c90l) (merge vers: 6.0.14-alpha) (pib:16)
[28 May 2010 6:56] Bugs System
Pushed into 5.5.5-m3 (revid:alik@sun.com-20100524185725-c8k5q7v60i5nix3t) (version source revid:alexey.kopytov@sun.com-20100523204118-0tl3goawu658rxh6) (merge vers: 5.5.5-m3) (pib:16)
[2 Jun 2010 8:49] Bugs System
Pushed into 5.1.48 (revid:georgi.kodinov@oracle.com-20100602084411-2yu607bslbmgufl3) (version source revid:tor.didriksen@sun.com-20100519091859-rwwjn5kjrgmoypic) (merge vers: 5.1.47) (pib:16)
[8 Jun 2010 18:30] Paul Dubois
Noted in 5.1.48, 5.5.5, 6.0.14 changelogs.

Calculation of intervals for Event Scheduler events was not portable.
[14 Oct 2010 8:31] Bugs System
Pushed into mysql-5.1-telco-7.0 5.1.51-ndb-7.0.20 (revid:martin.skold@mysql.com-20101014082627-jrmy9xbfbtrebw3c) (version source revid:vasil.dimov@oracle.com-20100513074652-0cvlhgkesgbb2bfh) (merge vers: 5.5.5-m3) (pib:21)
[14 Oct 2010 8:46] Bugs System
Pushed into mysql-5.1-telco-6.3 5.1.51-ndb-6.3.39 (revid:martin.skold@mysql.com-20101014083757-5qo48b86d69zjvzj) (version source revid:vasil.dimov@oracle.com-20100513074652-0cvlhgkesgbb2bfh) (merge vers: 5.5.5-m3) (pib:21)
[14 Oct 2010 9:01] Bugs System
Pushed into mysql-5.1-telco-6.2 5.1.51-ndb-6.2.19 (revid:martin.skold@mysql.com-20101014084420-y54ecj85j5we27oa) (version source revid:vasil.dimov@oracle.com-20100513074652-0cvlhgkesgbb2bfh) (merge vers: 5.5.5-m3) (pib:21)
[14 Oct 2010 14:04] Jon Stephens
Already documented in the 5.1.48 changelog; no additional changelog entries required. Set back to Closed state.