[fixed] Bug in core::map() - the root should never be red

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

[fixed] Bug in core::map() - the root should never be red

Post by rogerborg »

Removing the root of an irr::core::map's red-black tree can lead to a crash if the new root is red. The root node should always be black.

Code: Select all

Index: include/irrMap.h
===================================================================
--- include/irrMap.h	(revision 1444)
+++ include/irrMap.h	(working copy)
@@ -830,7 +830,10 @@
 	{
 		Root = newRoot;
 		if (Root != 0)
+		{
 			Root->setParent(0);
+			Root->setBlack(); // The root must always be black
+		}
 	}
 
 	//! Insert a node into the tree without using any fancy balancing logic.

Test case that demonstrates the crash (when unpatched) and better behaviour after patching:

Code: Select all

#include <irrlicht.h>

#pragma comment(lib, "Irrlicht.lib")

int main()
{
    irr::core::map<int, int> myMap;

    (void)myMap.insert(2, 0);
    (void)myMap.insert(3, 0);
    myMap.remove(2); // removes the root
    (void)myMap.insert(0, 0); // crashes here

    // The following exercises the code after the patch
    myMap.remove(3); // remove the new root
    (void)myMap.insert(2, 0);
    (void)myMap.insert(4, 0);
    (void)myMap.insert(1, 0);
    (void)myMap.insert(3, 0);
    myMap.remove(2); // removes the root again
    (void)myMap.insert(0, 0);

    return 0;
}
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

If only someone would provide a simpler, less buggy implementation of a map :cry:
Anyway, thanks for the patch.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Reading the current implementation scared me. :shock: I had to get a special hug from the missus afterwards.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
full.metal.coder
Posts: 68
Joined: Sat May 10, 2008 11:30 am
Contact:

Post by full.metal.coder »

if anyone would provide a decent implementation of a hash table... :wink:

Qt implementations of both just rock but are licensed under GPL so Irrlicht can not reuse them directly... :( there's also google dense_hash_map which is supposed to be good but has some limitations.

A temporary solution is to use two arrays, which is bug free and allows two-ways indexing but gives O(n) lookup instead of O(log n) for a classic map and O(1) for a hash table...
Post Reply