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=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f2690a5e963b61b6,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!g44g2000cwa.googlegroups.com!not-for-mail From: "Dmitriy Anisimkov" Newsgroups: comp.lang.ada Subject: GCC 4.0 Ada.Containers Cursor danger. Date: 4 Jul 2005 04:01:31 -0700 Organization: http://groups.google.com Message-ID: <1120474891.635131.216700@g44g2000cwa.googlegroups.com> NNTP-Posting-Host: 80.89.131.48 Mime-Version: 1.0 Content-Type: text/plain; charset="koi8-r" X-Trace: posting.google.com 1120474897 25682 127.0.0.1 (4 Jul 2005 11:01:37 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 4 Jul 2005 11:01:37 +0000 (UTC) User-Agent: G2/0.2 Complaints-To: groups-abuse@google.com Injection-Info: g44g2000cwa.googlegroups.com; posting-host=80.89.131.48; posting-account=t4CEmgwAAAA8dL2naG2k3iz_rN__dZy3 Xref: g2news1.google.com comp.lang.ada:11846 Date: 2005-07-04T04:01:31-07:00 List-Id: IMHO the cursors in the Ada.Containers implemented in GCC 4.0 dangerouse like pointers in C/C++. Look at the code below. ---------------------------------------------- with Ada.Containers.Indefinite_Hashed_Maps; with Ada.Strings.Hash; package HTab is new Ada.Containers.Indefinite_Hashed_Maps (String, Integer, Ada.Strings.Hash, "=", "="); ----------------------------------------------------------------- with Ada.Text_IO; with HTab; procedure AC1 is use Ada.Text_IO; package Table renames HTab; Cursor : Table.Cursor; Cursor2 : Table.Cursor; Container : Table.Map; Success : Boolean; begin Table.Insert (Container, "one", 11111, Cursor, Success); pragma Assert (Success); Table.Insert (Container, "two", 22222, Cursor, Success); pragma Assert (Success); Table.Insert (Container, "three", 33333, Cursor, Success); pragma Assert (Success); Table.Insert (Container, "two", 2222, Cursor, Success); pragma Assert (not Success); -- Delete element "two" independently. Cursor2 := Table.Find (Container, "two"); Table.Delete (Container, Cursor2); -- The erroreneous line below do nothing and do not raise any exception. Table.Replace_Element (Cursor, -22222); Cursor := Table.First (Container); -- Print all lines, and see that we do not have a key "two". while Table.Has_Element (Cursor) loop Put_Line (Table.Key (Cursor) & ' ' & Integer'Image (Table.Element (Cursor))); Table.Next (Cursor); end loop; end AC1; ------------------------------------------------------------ The code above have to raise at least runtime error at Table.Replace_Element (Cursor, -22222); but it do nothing and do not raise any exception. valgrind showing the memory corruption --------------------------- ==24602== Invalid read of size 4 ==24602== at 0x804F1EC: htab__replace_element (a-cihama.adb:630) ==24602== by 0x805024D: _ada_ac1 (ac1.adb:31) ==24602== by 0x8049C2F: main (b~ac1.adb:157) ==24602== Address 0x1BA4EAF8 is 8 bytes inside a block of size 16 free'd ==24602== at 0x1B903B0D: free (vg_replace_malloc.c:152) ==24602== by 0x805D257: __gnat_free (s-memory.adb:113) ==24602== by 0x804D8CD: htab(float, long double,...)(...) (a-cihama.adb:338) ==24602== by 0x804CAE5: htab__delete__2 (a-cihama.adb:205) ==24602== by 0x805021E: _ada_ac1 (ac1.adb:27) ==24602== by 0x8049C2F: main (b~ac1.adb:157) ==24602== ==24602== Invalid write of size 4 ==24602== at 0x804F223: htab__replace_element (a-cihama.adb:632) ==24602== by 0x805024D: _ada_ac1 (ac1.adb:31) ==24602== by 0x8049C2F: main (b~ac1.adb:157) ==24602== Address 0x1BA4EAF8 is 8 bytes inside a block of size 16 free'd ==24602== at 0x1B903B0D: free (vg_replace_malloc.c:152) ==24602== by 0x805D257: __gnat_free (s-memory.adb:113) ==24602== by 0x804D8CD: htab(float, long double,...)(...) (a-cihama.adb:338) ==24602== by 0x804CAE5: htab__delete__2 (a-cihama.adb:205) ==24602== by 0x805021E: _ada_ac1 (ac1.adb:27) ==24602== by 0x8049C2F: main (b~ac1.adb:157) --------------------------- I am using "ADT Components" from http://lgl.epfl.ch/ada/components/index.html too. I'm writting in ADT but have to use code with AI302. I did not found so much errors with ADT as with AI302 and with Ada.Containers cursors. ADT do not have a cursors at all. All get/put operations from container is just per key. All iterations via the containers are with simple generic procedures. I'm not sure that it is possible to implement runtime error detection in such cursor situation. I think that ADT is much more on the Ada way then proposed Ada.Containers with cursors.