1

While analyzing an Ada codebase with CodePeer, the tool is reporting that "array index check might fail" at a line that does this:

Param(Param'First) := .... 

Param is a passed-in out parameter - and it's of an array type.

My understanding is that Ada carries the array sizes and valid index ranges at runtime (in order to perform range checks)... I can't see how any Ada array can violate its range check by accessing index array'First.

What am I missing?

2 Answers 2

4

You can declare an array with an empty range,

array(1..0) array(10..5) array(1337..-1) 

etc.

In this case, Param'First would be greater than Param'Last, and the index check would fail.

I guess Param is of an unconstrained array type, like String, f.eks?

type String is array(Positive range <>) of Character; 

any object of the type could then be declared as an empty string:

Foo : String(1..0); 

and then passed as parameter to your subprogram

Sign up to request clarification or add additional context in comments.

2 Comments

I understand - but if you don't mind me asking.... why would one do that? I mean, why would Ada even allow such an instantiation to compile?
Functions can return array objects, like Ada.Text_IO.Get_Line. Sometimes, the line is empty (if the user only pressed enter, for example). In that case, the array returned would also need to be empty.
2

You can prevent the problem by giving your unconstrained array type a dynamic predicate, which says that the array can't be empty:

subtype Non_Empty_String is String with Dynamic_Predicate => Non_Empty_String'Length >= 1; 

1 Comment

Alternatively you can check the length of the array before referencing its index values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.