Index: main/menu2.c
===================================================================
--- main/menu2.c	(revision main,2)
+++ main/menu2.c	(revision main,2.1.2)
@@ -5,9 +5,18 @@
 
 
-typedef int menu2_h;
-
+#define _MAX_MENUS 16
 #define _MAX_ITEMS 16
 
+#define _KEY_BITS 4
+#define _IDX_MASK (~(0u) << _KEY_BITS)
+#define _KEY_MASK (~_IDX_MASK)
+
+
+
+typedef unsigned menu2_h;
+
 struct menu2_t {
+	unsigned key;
+
 	char *item_names[_MAX_ITEMS];
 	char *item_descriptions[_MAX_ITEMS];
@@ -18,17 +27,44 @@
 };
 
-static const int _MAX_MENUS = 16;
+static struct menu2_t *_menus[_MAX_MENUS];
 
 
-
-static struct menu2_t *_menus[16];
-
-
-
-static int menu2_init(int i)
+static menu2_h _menu2_init(unsigned i)
 {
 	_menus[i] = calloc(1, sizeof(struct menu2_t));
 	if(!_menus[i]) return 0;
-	return i + 1;
+
+	do {
+		_menus[i]->key = rand() & _KEY_MASK;
+	} while(i == 0 && _menus[i]->key == 0);
+
+	return (i << _KEY_BITS) | _menus[i]->key;
+}
+
+static struct menu2_t **_check_ptr(menu2_h menu)
+{
+	unsigned i, k;
+	struct menu2_t **m;
+
+	if(!menu) return NULL;
+
+	i = menu >> _KEY_BITS;
+	k = menu & _KEY_MASK;
+
+	if(i >= _MAX_MENUS) return NULL;
+
+	m = _menus + i;
+
+	if(!*m) return NULL;
+	if((*m)->key != k) return NULL;
+
+	return m;
+}
+
+static struct menu2_t *_check(menu2_h menu)
+{
+	struct menu2_t **m = _check_ptr(menu);
+	if(!m) return NULL;
+	return *m;
 }
 
@@ -38,12 +74,17 @@
 
 	for(i = 0; i < _MAX_MENUS; i++)
-		if(!_menus[i]) return menu2_init(i);
+		if(!_menus[i]) return _menu2_init(i);
 
 	return 0;
 }
 
+static int _filled(const char *str)
+{
+	return str && (strlen(str) > 0);
+}
+
 int menu2_add_item(menu2_h menu, const char *name, const char *description)
 {
-	struct menu2_t *m = _menus[menu - 1];
+	struct menu2_t *m = _check(menu);
 	int *i;
 
@@ -54,5 +95,6 @@
 
 	m->item_names[*i] = strdup(name);
-	m->item_descriptions[*i] = strdup(description ? description : name);
+	m->item_descriptions[*i] =
+		strdup(_filled(description) ? description : name);
 	m->item_structs[*i] =
 		new_item(m->item_names[*i], m->item_descriptions[*i]);
@@ -65,17 +107,17 @@
 int menu2_destroy(menu2_h menu)
 {
-	struct menu2_t *m = _menus[menu - 1];
+	struct menu2_t **m = _check_ptr(menu);
 	int i;
 
 	if(!m) return 0;
 
-	for(i = 0; i < m->item_count; i++) {
-		free_item(m->item_structs[i]);
-		free(m->item_descriptions[i]);
-		free(m->item_names[i]);
+	for(i = 0; i < (*m)->item_count; i++) {
+		free_item((*m)->item_structs[i]);
+		free((*m)->item_descriptions[i]);
+		free((*m)->item_names[i]);
 	}
 
-	free(m);
-	_menus[menu - 1] = NULL;
+	free(*m);
+	*m = NULL;
 
 	return 1;
@@ -84,5 +126,5 @@
 int menu2_post(menu2_h menu)
 {
-	struct menu2_t *m = _menus[menu - 1];
+	struct menu2_t *m = _check(menu);
 
 	if(!m) return 0;
@@ -97,5 +139,5 @@
 int menu2_drive(menu2_h menu, int c)
 {
-	struct menu2_t *m = _menus[menu - 1];
+	struct menu2_t *m = _check(menu);
 
 	if(!m) return 0;
@@ -109,5 +151,5 @@
 int menu2_unpost(menu2_h menu)
 {
-	struct menu2_t *m = _menus[menu - 1];
+	struct menu2_t *m = _check(menu);
 
 	if(!m) return 0;
