public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Fix undetected overflow case in Ada.Real_Time."/"
@ 2015-05-12  8:11 Arnaud Charlet
  0 siblings, 0 replies; only message in thread
From: Arnaud Charlet @ 2015-05-12  8:11 UTC (permalink / raw)
  To: gcc-patches; +Cc: Robert Dewar

[-- Attachment #1: Type: text/plain, Size: 819 bytes --]

Despite the explicit pragma Unsuppress statements, the case of dividing
Time_Span_First by -1 did not raise an exception. Eventually this should
be corrected at the compiler or runtime level, but for now, we add an
explicit check to ensure that this case is caught.

The following test program:

     1. with Ada.Real_Time; use Ada.Real_Time;
     2. procedure ReaTimOv is
     3.    Result : Time_Span;
     4. begin
     5.    Result :=
     6.      Ada.Real_Time."/"
     7.        (Left  => Ada.Real_Time.Time_Span_First,
     8.         Right => -1);
     9. end;

must yield when executed:

raised CONSTRAINT_ERROR : Ada.Real_Time."/": overflow

Tested on x86_64-pc-linux-gnu, committed on trunk

2015-05-12  Robert Dewar  <dewar@adacore.com>

	* a-reatim.adb ("/"): Add explicit check for Time_Span_First / -1.


[-- Attachment #2: difs --]
[-- Type: text/plain, Size: 1459 bytes --]

Index: a-reatim.adb
===================================================================
--- a-reatim.adb	(revision 223033)
+++ a-reatim.adb	(working copy)
@@ -7,7 +7,7 @@
 --                                 B o d y                                  --
 --                                                                          --
 --             Copyright (C) 1991-1994, Florida State University            --
---                     Copyright (C) 1995-2014, AdaCore                     --
+--                     Copyright (C) 1995-2015, AdaCore                     --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -123,6 +123,16 @@
       pragma Unsuppress (Overflow_Check);
       pragma Unsuppress (Division_Check);
    begin
+      --  Even though checks are unsuppressed, we need an explicit check for
+      --  the case of largest negative integer divided by minus one, since
+      --  some library routines we use fail to catch this case. This will be
+      --  fixed at the compiler level in the future, at which point this test
+      --  can be removed.
+
+      if Left = Time_Span_First and then Right = -1 then
+         raise Constraint_Error with "overflow";
+      end if;
+
       return Time_Span (Duration (Left) / Right);
    end "/";
 

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-05-12  8:07 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-12  8:11 [Ada] Fix undetected overflow case in Ada.Real_Time."/" Arnaud Charlet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).