comp.lang.ada
 help / color / mirror / Atom feed
From: isaac1.0@gmail.com
Subject: Re: Compiler checking of String lengths during assignment (Newbie Question)
Date: Thu, 15 Jan 2015 12:44:57 -0800 (PST)
Date: 2015-01-15T12:44:57-08:00	[thread overview]
Message-ID: <93725194-bf90-400f-844f-7d0d49adf030@googlegroups.com> (raw)
In-Reply-To: <77d434cc-00bc-4a2f-b50e-40736abdd2b2@googlegroups.com>

On Friday, January 9, 2015 at 2:50:18 PM UTC-8, isaa...@gmail.com wrote:
> Maybe this is obvious to the experts, but I'm new to Ada.
> 
> I'm using the Libre GNAT GPS 2014. I've a program that looks roughly like this:
> ==============================================
> procedure Main is
>    Head : String (1..5) := "XXX_N";
>    i : Positive;
>    Line : String := "12312312312312312";
>    
> begin
> .
> .
> .
>    i := 2;
>    Head := "123";
>    Head := Line(1 .. 2);
>    Head := Line(i+2 .. i+4);
> .
> .
> .
> end Main;
> ==============================================
> 
> The first 2 cases produces an error during the build, as expected, because Head is known to be length of 5 and I'm assigning something of a different length.
> The 3rd assignment there does NOT produce an error, no warning no nothing even though this clearly violates the same check.
> 
> What does happen is that at runtime the execution is incorrect. It doesn't even terminate in a horrible disaster (which would be preferable). The full program actually just reads a text file (for now) and in this case because the assigned length is incorrect, it just reads a few lines then bails claiming unable to read the text file.
> 
> Aren't Ada compilers supposed to prevent stupid errors like this? Or is there some user stupidity here that I'm not seeing...?
> 
> Thanks,
> Isaac

Thanks for all your responses. There are 2 issues I think.
One is the compiler. Mine says GNAT GPL 2014 (20140331) and gcc is 4.7.4

gcc -c -I- -gnatA E:\Projects\Test\src\main.adb
main.adb:23:27: warning: wrong length for array of subtype of "Standard.String" defined at line 9
main.adb:23:27: warning: "Constraint_Error" will be raised at run time
main.adb:24:27: warning: too few elements for subtype of "Standard.String" defined at line 9
main.adb:24:27: warning: "Constraint_Error" will be raised at run time

So I get the 2 warnings that are expected, but the 3rd one is missed. I'm no compiler expert, but if you say it's difficult for the compiler to sort out arithmetic logic like that, then maybe this is the case. If a newer compiler is not showing the problem, then maybe this has already been solved.

The second issue is user stupidity. In fact, all 3 cases do generate run time error, as expected. The reason I'm not seeing it on the 3rd case is buggy exception handling code. In my sample, there is some error that causes the run-time error on length checking to fall through and show as something else. 


Thank you again for all your help. Back to more fun & games with Ada!


Isaac


  parent reply	other threads:[~2015-01-15 20:44 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-09 22:50 Compiler checking of String lengths during assignment (Newbie Question) isaac1.0
2015-01-09 23:10 ` Simon Wright
2015-01-09 23:20   ` Stefan.Lucks
2015-01-10 12:43     ` Brian Drummond
2015-01-09 23:53 ` Shark8
2015-01-10  0:18 ` Jeffrey Carter
2015-01-10  1:48 ` Adam Beneschan
2015-01-10 10:03 ` Pascal Obry
2015-01-10 10:54 ` Simon Wright
2015-01-15 20:44 ` isaac1.0 [this message]
2015-01-15 21:18   ` Simon Wright
2015-01-15 22:30     ` Jeffrey Carter
2015-01-15 22:32     ` Jeffrey Carter
2015-01-16  7:28       ` Simon Wright
2015-01-16 15:44         ` Jeffrey Carter
replies disabled

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