MEDIUM: regex: modify regex_comp() to atomically allocate/free the my_regex struct

Now we atomically allocate the my_regex struct within function
regex_comp() and compile the regex or free both in case of failure. The
pointer to the allocated my_regex struct is returned directly. The
my_regex* argument to regex_comp() is removed.

Function regex_free() was modified so that it systematically frees the
my_regex entry. The function does nothing when called with a NULL as
argument (like free()). It will avoid existing risk of not properly
freeing the initialized area.

Other structures are also updated in order to be compatible (the ones
related to Lua and action rules).
diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c
index 60882f3..3b71ce1 100644
--- a/src/hlua_fcn.c
+++ b/src/hlua_fcn.c
@@ -1537,14 +1537,14 @@
 	return 1;
 }
 
-static struct my_regex *hlua_check_regex(lua_State *L, int ud)
+static struct my_regex **hlua_check_regex(lua_State *L, int ud)
 {
 	return (hlua_checkudata(L, ud, class_regex_ref));
 }
 
 static int hlua_regex_comp(struct lua_State *L)
 {
-	struct my_regex *regex;
+	struct my_regex **regex;
 	const char *str;
 	int cs;
 	char *err;
@@ -1556,7 +1556,7 @@
 	regex = lua_newuserdata(L, sizeof(*regex));
 
 	err = NULL;
-	if (!regex_comp(str, regex, cs, 1, &err)) {
+	if (!(*regex = regex_comp(str, cs, 1, &err))) {
 		lua_pushboolean(L, 0); /* status error */
 		lua_pushstring(L, err); /* Reason */
 		free(err);
@@ -1576,7 +1576,7 @@
 
 static int hlua_regex_exec(struct lua_State *L)
 {
-	struct my_regex *regex;
+	struct my_regex **regex;
 	const char *str;
 	size_t len;
 	struct buffer *tmp;
@@ -1584,6 +1584,11 @@
 	regex = hlua_check_regex(L, 1);
 	str = luaL_checklstring(L, 2, &len);
 
+	if (!*regex) {
+		lua_pushboolean(L, 0);
+		return 1;
+	}
+
 	/* Copy the string because regex_exec2 require a 'char *'
 	 * and not a 'const char *'.
 	 */
@@ -1594,14 +1599,14 @@
 	}
 	memcpy(tmp->area, str, len);
 
-	lua_pushboolean(L, regex_exec2(regex, tmp->area, len));
+	lua_pushboolean(L, regex_exec2(*regex, tmp->area, len));
 
 	return 1;
 }
 
 static int hlua_regex_match(struct lua_State *L)
 {
-	struct my_regex *regex;
+	struct my_regex **regex;
 	const char *str;
 	size_t len;
 	regmatch_t pmatch[20];
@@ -1612,6 +1617,11 @@
 	regex = hlua_check_regex(L, 1);
 	str = luaL_checklstring(L, 2, &len);
 
+	if (!*regex) {
+		lua_pushboolean(L, 0);
+		return 1;
+	}
+
 	/* Copy the string because regex_exec2 require a 'char *'
 	 * and not a 'const char *'.
 	 */
@@ -1622,7 +1632,7 @@
 	}
 	memcpy(tmp->area, str, len);
 
-	ret = regex_exec_match2(regex, tmp->area, len, 20, pmatch, 0);
+	ret = regex_exec_match2(*regex, tmp->area, len, 20, pmatch, 0);
 	lua_pushboolean(L, ret);
 	lua_newtable(L);
 	if (ret) {
@@ -1636,10 +1646,11 @@
 
 static int hlua_regex_free(struct lua_State *L)
 {
-	struct my_regex *regex;
+	struct my_regex **regex;
 
 	regex = hlua_check_regex(L, 1);
-	regex_free(regex);
+	regex_free(*regex);
+	*regex = NULL;
 	return 0;
 }