From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.182.29.3 with SMTP id f3mr9526083obh.16.1421354698633; Thu, 15 Jan 2015 12:44:58 -0800 (PST) X-Received: by 10.50.67.107 with SMTP id m11mr160096igt.15.1421354698438; Thu, 15 Jan 2015 12:44:58 -0800 (PST) Path: border1.nntp.dca1.giganews.com!nntp.giganews.com!h15no6000856igd.0!news-out.google.com!db6ni86igc.0!nntp.google.com!h15no3864725igd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 15 Jan 2015 12:44:57 -0800 (PST) In-Reply-To: <77d434cc-00bc-4a2f-b50e-40736abdd2b2@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=207.102.98.187; posting-account=QZXBrgoAAADhgHRaMWbEzK_0LI2JYo5W NNTP-Posting-Host: 207.102.98.187 References: <77d434cc-00bc-4a2f-b50e-40736abdd2b2@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <93725194-bf90-400f-844f-7d0d49adf030@googlegroups.com> Subject: Re: Compiler checking of String lengths during assignment (Newbie Question) From: isaac1.0@gmail.com Injection-Date: Thu, 15 Jan 2015 20:44:58 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: number.nntp.giganews.com comp.lang.ada:191887 Date: 2015-01-15T12:44:57-08:00 List-Id: 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. >=20 > I'm using the Libre GNAT GPS 2014. I've a program that looks roughly like= this: > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > procedure Main is > Head : String (1..5) :=3D "XXX_N"; > i : Positive; > Line : String :=3D "12312312312312312"; > =20 > begin > . > . > . > i :=3D 2; > Head :=3D "123"; > Head :=3D Line(1 .. 2); > Head :=3D Line(i+2 .. i+4); > . > . > . > end Main; > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > The first 2 cases produces an error during the build, as expected, becaus= e Head is known to be length of 5 and I'm assigning something of a differen= t length. > The 3rd assignment there does NOT produce an error, no warning no nothing= even though this clearly violates the same check. >=20 > 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 fu= ll program actually just reads a text file (for now) and in this case becau= se the assigned length is incorrect, it just reads a few lines then bails c= laiming unable to read the text file. >=20 > Aren't Ada compilers supposed to prevent stupid errors like this? Or is t= here some user stupidity here that I'm not seeing...? >=20 > 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.Str= ing" 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 n= o compiler expert, but if you say it's difficult for the compiler to sort o= ut arithmetic logic like that, then maybe this is the case. If a newer comp= iler 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 ti= me error, as expected. The reason I'm not seeing it on the 3rd case is bugg= y exception handling code. In my sample, there is some error that causes th= e run-time error on length checking to fall through and show as something e= lse.=20 Thank you again for all your help. Back to more fun & games with Ada! Isaac