comp.lang.ada
 help / color / mirror / Atom feed
* SPARK - strange range check with array assignment
@ 2015-08-18 20:06 Maciej Sobczak
  2015-08-18 22:07 ` Georg Bauhaus
  2015-08-19  0:11 ` Anh Vo
  0 siblings, 2 replies; 4+ messages in thread
From: Maciej Sobczak @ 2015-08-18 20:06 UTC (permalink / raw)


Consider (extracted from a bigger problem):

-- p.ads:
pragma SPARK_Mode;

package P is

   procedure Foo (S : in out String)
      with Pre => S'Length > 3;

end P;

-- p.adb:
pragma SPARK_Mode;

package body P is

   procedure Foo (S : in out String)
   is
   begin
      S (S'First .. S'First + 2) := "abc";  -- HERE
   end Foo;

end P;


Foo is intended to overwrite the beginning of the given string.
GNATProve (2014) says:

p.adb:8:37: warning: range check might fail

Curiously, the assertion added just before the failing line:

      pragma Assert (S'First + 2 in S'Range);

passes without any warning.

What value or expression above is subject to a range check that might fail?

-- 
Maciej Sobczak * http://www.inspirel.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: SPARK - strange range check with array assignment
  2015-08-18 20:06 SPARK - strange range check with array assignment Maciej Sobczak
@ 2015-08-18 22:07 ` Georg Bauhaus
  2015-08-19  0:11 ` Anh Vo
  1 sibling, 0 replies; 4+ messages in thread
From: Georg Bauhaus @ 2015-08-18 22:07 UTC (permalink / raw)


On 18.08.15 22:06, Maciej Sobczak wrote:
> Consider (extracted from a bigger problem):
>
> -- p.ads:
> pragma SPARK_Mode;
>
> package P is
>
>     procedure Foo (S : in out String)
>        with Pre => S'Length > 3;
>
> end P;
>
> -- p.adb:
> pragma SPARK_Mode;
>
> package body P is
>
>     procedure Foo (S : in out String)
>     is
>     begin
>        S (S'First .. S'First + 2) := "abc";  -- HERE
>     end Foo;
>
> end P;
>
>
> Foo is intended to overwrite the beginning of the given string.
> GNATProve (2014) says:
>
> p.adb:8:37: warning: range check might fail
>
> Curiously, the assertion added just before the failing line:
>
>        pragma Assert (S'First + 2 in S'Range);
>
> passes without any warning.
>
> What value or expression above is subject to a range check that might fail?

Maybe there are hints at some historic reasons in two quotes from [1] about
the original SPARK:

"SPARK does not have array slices". (p.103)
"It is important to realize that since all constraints have to be static
  it follows that all arrays have static bounds. Thus we cannot declare
  an array in SPARK whose bounds are not known until the program executes.
  This may be felt rather irksome but is necessary if the space occupied
  by the program is to be predictably bounded." (p.96)

[1] Barnes, John (1997). High Integrity Ada. The SPARK Approach.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: SPARK - strange range check with array assignment
  2015-08-18 20:06 SPARK - strange range check with array assignment Maciej Sobczak
  2015-08-18 22:07 ` Georg Bauhaus
@ 2015-08-19  0:11 ` Anh Vo
  2015-08-19 10:09   ` Maciej Sobczak
  1 sibling, 1 reply; 4+ messages in thread
From: Anh Vo @ 2015-08-19  0:11 UTC (permalink / raw)


On Tuesday, August 18, 2015 at 1:06:47 PM UTC-7, Maciej Sobczak wrote:
> Consider (extracted from a bigger problem):
> 
> -- p.ads:
> pragma SPARK_Mode;
> 
> package P is
> 
>    procedure Foo (S : in out String)
>       with Pre => S'Length > 3;
> 
> end P;
> 
> -- p.adb:
> pragma SPARK_Mode;
> 
> package body P is
> 
>    procedure Foo (S : in out String)
>    is
>    begin
>       S (S'First .. S'First + 2) := "abc";  -- HERE
>    end Foo;
> 
> end P;
> 
> 
> Foo is intended to overwrite the beginning of the given string.
> GNATProve (2014) says:
> 
> p.adb:8:37: warning: range check might fail
> 
> Curiously, the assertion added just before the failing line:
> 
>       pragma Assert (S'First + 2 in S'Range);
> 
> passes without any warning.
> 
> What value or expression above is subject to a range check that might fail?
 
it went through gnatprove (2015) happily. I tested it on Windows.

Anh Vo

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: SPARK - strange range check with array assignment
  2015-08-19  0:11 ` Anh Vo
@ 2015-08-19 10:09   ` Maciej Sobczak
  0 siblings, 0 replies; 4+ messages in thread
From: Maciej Sobczak @ 2015-08-19 10:09 UTC (permalink / raw)



> it went through gnatprove (2015) happily. I tested it on Windows.

I have just installed SPARK 2015 on Windows and I confirm that the test passes OK. Time to upgrade. :-)

Regards,

-- 
Maciej Sobczak * http://www.inspirel.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-08-19 10:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-18 20:06 SPARK - strange range check with array assignment Maciej Sobczak
2015-08-18 22:07 ` Georg Bauhaus
2015-08-19  0:11 ` Anh Vo
2015-08-19 10:09   ` Maciej Sobczak

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