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.66.234.38 with SMTP id ub6mr9631807pac.145.1475083662791; Wed, 28 Sep 2016 10:27:42 -0700 (PDT) X-Received: by 10.157.8.10 with SMTP id 10mr2617334oty.16.1475083662745; Wed, 28 Sep 2016 10:27:42 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.fcku.it!aioe.org!news.glorb.com!x192no120781itb.0!news-out.google.com!203ni621itk.0!nntp.google.com!l13no118545itl.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 28 Sep 2016 10:27:42 -0700 (PDT) In-Reply-To: <374b0d79-541d-44d2-886e-dd41f8815914@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=149.32.224.37; posting-account=Qh2kiQoAAADpCLlhT_KTYoGO8dU3n4I6 NNTP-Posting-Host: 149.32.224.37 References: <374b0d79-541d-44d2-886e-dd41f8815914@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <8b8998a2-f29b-4664-a6e0-2271933a3c9a@googlegroups.com> Subject: Re: Limited type in generic package causes double free or corruption From: Anh Vo Injection-Date: Wed, 28 Sep 2016 17:27:42 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:31923 Date: 2016-09-28T10:27:42-07:00 List-Id: On Wednesday, September 28, 2016 at 6:26:02 AM UTC-7, onox wrote: > I have a generic package (A) with a limited type (Tree). The generic pack= age is used as a generic formal parameter in another generic package (B). T= his second generic package has a primitive operation (Get_Tree) that return= s a component of a tagged record that is of that limited type. Calling that= primitive operation results in a "double free or corruption (fasttop)" mes= sage when the program terminates. >=20 > Shouldn't the compiler (GNAT GPL 2015) check that the type in the generic= formal parameter (package) is limited and forbid the copying operation? If= I remove the "limited" keyword from the type, then the program terminates = without any errors. >=20 > Compile via gnatmake c.adb >=20 > I put the example in a separate task, otherwise the program could just ha= ng with 100 % CPU instead of displaying the error and terminating. >=20 > onox >=20 > -- File a.ads > private with Ada.Containers.Vectors; >=20 > generic > Foo : Positive; > package A is >=20 > -- Remove "limited" keyword to avoid double-free or corruption > type Tree is tagged limited private; >=20 > function Create_Tree return Tree; >=20 > private >=20 > package Level_Vectors is new Ada.Containers.Vectors (Positive, Positiv= e); >=20 > -- Remove "limited" keyword to avoid double-free or corruption > type Tree is tagged limited record > Levels : Level_Vectors.Vector; > end record; >=20 > end A; > -- End of file a.ads >=20 > -- File a.adb > package body A is >=20 > function Create_Tree return Tree is > begin > return Object : Tree do > Object.Levels.Append (10); > end return; > end Create_Tree; >=20 > end A; > -- End of file a.adb >=20 > -- File b.ads > with A; >=20 > generic > with package Trees is new A (<>); > package B is >=20 > type Thing is tagged limited private; >=20 > function Load_Thing return Thing; >=20 > function Get_Tree (Object : Thing) return Trees.Tree; >=20 > private >=20 > type Thing is tagged limited record > My_Tree : Trees.Tree; > end record; >=20 > end B; > -- End of file b.ads >=20 > -- File b.adb > package body B is >=20 > function Load_Thing return Thing is > begin > return Object : Thing :=3D (My_Tree =3D> Trees.Create_Tree) do > null; > end return; > end Load_Thing; >=20 > -- If Trees.Tree (A.Tree) is limited, shouldn't this function be disa= llowed? > function Get_Tree (Object : Thing) return Trees.Tree is > (Object.My_Tree); >=20 > end B; > -- End of file b.adb >=20 > -- File c.adb > with Ada.Text_IO; > with A; > with B; >=20 > procedure C is >=20 > task Worker; >=20 > task body Worker is > package AA is new A (1); > use AA; > package Things is new B (AA); >=20 > M : Things.Thing :=3D Things.Load_Thing; > T : Tree :=3D M.Get_Tree; > begin > Ada.Text_IO.Put_Line ("Worker thread"); > end Worker; >=20 > begin > Ada.Text_IO.Put_Line ("Environment thread"); > end C; > -- End of file c.adb Your codes work fine under GNAT GPL 2016. Of course, you need to pass compi= ler flag -gnat2012 during compilation. Anh Vo