comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: Bug or feature?
Date: Wed, 14 May 2014 13:15:02 -0700 (PDT)
Date: 2014-05-14T13:15:02-07:00	[thread overview]
Message-ID: <049c868a-e930-4e5d-a96a-611542cd1ce6@googlegroups.com> (raw)
In-Reply-To: <40c7405d-c4c2-4163-a430-01052b769866@googlegroups.com>

On Wednesday, May 14, 2014 12:57:47 PM UTC-7, Adam Beneschan wrote:

> It does appear that GNAT may not always check the range properly when a
> function whose result is Positive tries to return a negative value due to
> overflow.  That would explain why the recursive version doesn't get an
> exception.

Here's an example:

with Ada.Integer_Text_IO;
with Ada.Text_IO;

procedure Test is
   Result : Integer;

   function Add (X1, X2 : Positive) return Positive is
   begin
      return X1 + X2;
   end Add;

   function Multiply (X1, X2 : Positive) return Positive is
   begin
      return X1 * X2;
   end Multiply;

begin
   Result := Add(2_000_000_000, 2_000_000_000);
   Ada.Integer_Text_IO.Put (Item => Result, Width => 1);
   Result := Multiply(6, 2_000_000_000);
   Ada.Integer_Text_IO.Put (Item => Result, Width => 1);
end Test;

Compiled without -gnato.  The result is that a negative number is output after the call to Add, but the call to Multiply raises an exception.  So it looks like when the function result is Positive, a "return" that returns the result of an addition doesn't check to make sure the result is in range (when overflow is possible), but a "return" that returns the result of a multiplication does check.  That would also explain why the original Buffer_Overflow test case works but the recursive Fibonacci routine doesn't.

                                 -- Adam


  reply	other threads:[~2014-05-14 20:15 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-14 19:06 Bug or feature? Laurent
2014-05-14 19:57 ` Adam Beneschan
2014-05-14 20:15   ` Adam Beneschan [this message]
2014-05-14 21:24     ` Laurent
2014-05-14 21:37       ` Adam Beneschan
2014-05-14 22:02         ` Robert A Duff
2014-05-14 22:25           ` Adam Beneschan
2014-05-14 21:42       ` Robert A Duff
2014-05-15  8:51         ` Georg Bauhaus
2014-05-14 21:48       ` Randy Brukardt
2014-05-14 22:35         ` Robert A Duff
2014-05-15  8:23           ` Simon Wright
2014-05-15 18:21             ` Randy Brukardt
2014-05-15  8:58         ` Georg Bauhaus
2014-05-15 18:30           ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox