Re: GCC 4.0 Ada.Containers Cursor danger.





Dmitriy Anisimkov wrote:
> 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);

You don't need conditional insertion here. Just use the 3-param
Insert:

Container.Insert ("one", 1);
Container.Insert ("two", 2);
Container.Insert ("three", 3);


> 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);

Well of course this is erroneous, since the element designated by
Cursor was deleted in the previous statement!


> ------------------------------------------------------------
> 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.

It certainly does not have to raise a runtime error. It can do
anything it likes, since the behavior is undefined.


> valgrind showing the memory corruption

That's good, since what you did corrupts the memory.


> 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.

This is like saying, Tall buildings shouldn't have windows, since if
there's a window, you might jump out of the window and hurt yourself.

The solution is simple: don't jump out of windows...


> I'm not sure that it is possible to implement runtime error detection
> in such cursor situation.

It's software, we can doing anything we want. So of course you can
detect dangling cursors -- but not without a runtime penalty (in both
time and space).


> I think that ADT is much more on the Ada way then proposed
> Ada.Containers with cursors.

If you don't like cursors, then don't use 'em. Certainly with a map,
you never need to use cursors.

.