[mw-devel] MW3 r1307 - trunk/src

welshbyte at sucs.org welshbyte at sucs.org
Fri Nov 30 18:25:58 GMT 2012


Author: welshbyte
Date: 2012-11-30 18:25:58 +0000 (Fri, 30 Nov 2012)
New Revision: 1307

Modified:
   trunk/src/Makefile
   trunk/src/expand.c
   trunk/src/expand.h
   trunk/src/script.c
   trunk/src/script.h
   trunk/src/script_inst.c
   trunk/src/sqlite.c
   trunk/src/talker.c
   trunk/src/vars.c
Log:
Fix some memory leaks, mainly caused by duplicated strdups now that hash_set_p does its own str duping.


Modified: trunk/src/Makefile
===================================================================
--- trunk/src/Makefile	2012-11-29 18:55:24 UTC (rev 1306)
+++ trunk/src/Makefile	2012-11-30 18:25:58 UTC (rev 1307)
@@ -35,7 +35,7 @@
 CFLAGS+= -ggdb -g -D__NO_STRING_INLINE -fstack-protector-all
 
 ### Optimisation - uncomment for release & extra testing
-CFLAGS+=-O3
+CFLAGS+=-O0
 
 CFLAGS += $(DEFS)
 

Modified: trunk/src/expand.c
===================================================================
--- trunk/src/expand.c	2012-11-29 18:55:24 UTC (rev 1306)
+++ trunk/src/expand.c	2012-11-30 18:25:58 UTC (rev 1307)
@@ -55,7 +55,7 @@
 	return NULL;
 }
 
-int eval_var(char *name, var_op_t *op)
+int eval_var(const char *name, var_op_t *op)
 {
     expand_init();
     if (expand_variable(name))

Modified: trunk/src/expand.h
===================================================================
--- trunk/src/expand.h	2012-11-29 18:55:24 UTC (rev 1306)
+++ trunk/src/expand.h	2012-11-30 18:25:58 UTC (rev 1307)
@@ -14,7 +14,7 @@
 
 const char *eval_str(char *arg);
 /*int call_with_var_op(char *varname, var_func_t *func, void *arg);*/
-int eval_var(char *name, var_op_t *op);
+int eval_var(const char *name, var_op_t *op);
 void expand_close(void);
 
 #endif /* EXPAND_H */

Modified: trunk/src/script.c
===================================================================
--- trunk/src/script.c	2012-11-29 18:55:24 UTC (rev 1306)
+++ trunk/src/script.c	2012-11-30 18:25:58 UTC (rev 1307)
@@ -668,8 +668,10 @@
 
 	if (is_js(function_name)) {
 		js_exec(function_name, num, bits);
+		free(function_name);
 		return;
 	}
+	free(function_name);
 	runaway=0;
 	script_terminate=0;
 	script_jump=NULL;
@@ -684,7 +686,7 @@
 	VAR_NEWLIST(&args);
 	ARG_RANGE(&args, 0, num-1);
 	for (i=0; i<num; ++i)
-	    ARG_STR_FORCE(&args, i, strdup(bits[i]));
+	    ARG_STR_FORCE(&args, i, bits[i]);
 
 	switch (ExecScript(bits[0], &args, 1))
 	{
@@ -846,8 +848,8 @@
 
 	VAR_NEWLIST(&args);
 	ARG_RANGE(&args, 0, 3);
-	ARG_STR_FORCE(&args, 0, strdup(event));
-	ARG_STR_FORCE(&args, 1, strdup(text));
+	ARG_STR_FORCE(&args, 0, event);
+	ARG_STR_FORCE(&args, 1, text);
 	if (who!=NULL)
 	{
 	    event_user=strdup(who);
@@ -898,11 +900,11 @@
 
 	VAR_NEWLIST(&args);
 	ARG_RANGE(&args, 0, numargs+1);
-	ARG_STR_FORCE(&args, 0, strdup(event));
+	ARG_STR_FORCE(&args, 0, event);
 
 	for (i = 0; i < numargs; i++)
 	{
-		ARG_STR_FORCE(&args, i + 1, strdup(aargs[i]));
+		ARG_STR_FORCE(&args, i + 1, aargs[i]);
 	}
 
 	if (who!=NULL)

Modified: trunk/src/script.h
===================================================================
--- trunk/src/script.h	2012-11-29 18:55:24 UTC (rev 1306)
+++ trunk/src/script.h	2012-11-30 18:25:58 UTC (rev 1307)
@@ -251,7 +251,7 @@
 char *list_mwsfile(const char *text, int state);
 int expand_script_dir(char **dir);
 
-void var_str_force_2(char *name, char *val);
+void var_str_force_2(const char *name, const char *val);
 char *var_str_val(var_op_t *op);
 void var_key_dup(var_op_t *op);
 

Modified: trunk/src/script_inst.c
===================================================================
--- trunk/src/script_inst.c	2012-11-29 18:55:24 UTC (rev 1306)
+++ trunk/src/script_inst.c	2012-11-30 18:25:58 UTC (rev 1307)
@@ -227,7 +227,7 @@
 		/* print date information into the string in the format "dd/mm/yyyy HH:MM:SS" */
 		snprintf(value, MAXTEXTLENGTH - 1, "%02d/%02d/%04d %02d:%02d:%02d", t->tm_mday, t->tm_mon + 1, t->tm_year + 1900, t->tm_hour, t->tm_min, t->tm_sec);
 	}
-	var_str_force_2(what, strdup(value));
+	var_str_force_2(what, value);
 	free(what);
 }
 
@@ -395,7 +395,7 @@
 		if ((p=strrchr(buff,' '))!=NULL) *p=0;
 	}
 
-	var_str_force_2(what, strdup(buff));
+	var_str_force_2(what, buff);
 	free(what);
 }
 
@@ -492,7 +492,7 @@
 		close(ufile);
 	}
 
-	var_str_force_2(what, strdup(rn));
+	var_str_force_2(what, rn);
 	free(what);
 }
 
@@ -523,7 +523,7 @@
 	if (u_god(user->status)) string_add(&buff, "S");
 	string_add(&buff, display_cmflags(user->chatmode & mask));
 
-	var_str_force_2(what, strdup(buff));
+	var_str_force_2(what, buff);
 	free(what);
 	free(buff);
 }
@@ -1031,7 +1031,7 @@
 	if (!isanum(aa, &a, 0) || !isanum(bb, &b, 0))
 	{
 		/* looks like they're not numbers, cant math strings */
-		var_str_force_2(what, strdup("-1"));
+		var_str_force_2(what, "-1");
 		if (script_debug) escprintf("- rand: either '%s' or '%s' were not numbers\n", aa, bb);
 		free(aa);
 		free(bb);
@@ -1044,7 +1044,7 @@
 	/* bad min/max */
 	if (b < a)
 	{
-		var_str_force_2(what, strdup("-1"));
+		var_str_force_2(what, "-1");
 		if (script_debug) printf("- rand operation. second argument (%d) smaller than first (%d)\n", b, a);
 		free(what);
 		return;
@@ -1052,7 +1052,7 @@
 	/* not positive */
 	if ((a < 0) || (b < 0))
 	{
-		var_str_force_2(what, strdup("-1"));
+		var_str_force_2(what, "-1");
 		if (script_debug) printf("- rand operation. arguments (%d, %d) must be positive", a, b);
 		free(what);
 		return;
@@ -1061,7 +1061,7 @@
 	if (script_debug) printf("- rand operation on %d and %d into $%s\n", a, b, what);
 
 	snprintf(buff, 19, "%d", get_rand(a,b));
-	var_str_force_2(what, strdup(buff));
+	var_str_force_2(what, buff);
 	free(what);
 }
 
@@ -1410,7 +1410,7 @@
 		} else
 		{
 			if (script_debug) escprintf("- Creating local variable `%s', initialising to ''\n", name);
-			VAR_STR_CREATE(&var_op, strdup(""));
+			VAR_STR_CREATE(&var_op, "");
 		}
 		free(name);
 	}
@@ -1420,10 +1420,12 @@
 {
 	if (pc->argc >= 1)
 	{
-		VAR_STR_FORCE(local_vars, "?", eval_arg(pc->argv[0], fargc, fargv));
+		char *arg = eval_arg(pc->argv[0], fargc, fargv);
+		VAR_STR_FORCE(local_vars, "?", arg);
+		if (arg) free(arg);
 	} else
 	{
-		VAR_STR_FORCE(local_vars, "?", strdup(""));
+		VAR_STR_FORCE(local_vars, "?", "");
 	}
 	
 	script_terminate=1;
@@ -1510,8 +1512,10 @@
 		for (i=2; i<new_argc; ++i)
 			ARG_STR_FORCE(&locals, i-1, args[i]);
 	}
+	for (i = 0; i < pc->argc; i++)
+		if (args[i]) free(args[i]);
 	free(args); /* contained strings are now in the variable list */
-	VAR_STR_FORCE(&locals, "?", strdup(""));
+	VAR_STR_FORCE(&locals, "?", "");
 	saved_locals = local_vars;
 	if (ExecScript(where, &locals, 0))
 	{
@@ -1526,9 +1530,9 @@
 		VAR_OP_INIT(&op, &locals);
 		VAR_SEARCH(&op, "?");
 		if (VAR_FOUND(&op))
-			var_str_force_2(result_var, strdup(var_str_val(&op)));
+			var_str_force_2(result_var, var_str_val(&op));
 		else
-			var_str_force_2(result_var, strdup(""));
+			var_str_force_2(result_var, "");
 		free(result_var);
 	}
 	VAR_FREELIST(&locals);
@@ -1755,6 +1759,7 @@
 	busy--;
 
 	var_str_force_2(what, value);
+	free(value);
 	free(what);
 	if (prompt!=NULL) free(prompt);
 }
@@ -2150,6 +2155,7 @@
 
 	var_str_force_2(what, value);
 
+	if (value!=NULL) free(value);
 	if (what!=NULL) free(what);
 	if (part!=NULL) free(part);
 	if (text!=NULL) free(text);
@@ -2185,6 +2191,7 @@
 	}
 	
 	var_str_force_2(what, value);
+	free(value);
 	free(what);
 }
 
@@ -2223,6 +2230,7 @@
 	}
 	
 	var_str_force_2(what, value);
+	free(value);
 	free(what);
 }
 
@@ -2260,14 +2268,14 @@
 		if (script_debug) printf(" - WHOAMI: Your username is '%s'\n", value);
 	}
 
-	var_str_force_2(what, strdup(value));
+	var_str_force_2(what, value);
 	free(value);
 	free(what);
 }
 	
 void scr_version( struct code *pc, int fargc, char **fargv )
 {
-	char *what, *value;
+	char *what;
 	const char *version_str = "R "VER_MAJ" "VER_MIN;
 
 	if (pc->argc < 1) {
@@ -2276,8 +2284,7 @@
 	}
 	what=eval_arg(pc->argv[0], fargc, fargv);
 	if (script_debug) printf(" - VERSION: Version string is '%s'", version_str);
-	value=strdup(version_str);
-	var_str_force_2(what, value);
+	var_str_force_2(what, version_str);
 	free(what);
 }
 
@@ -2509,6 +2516,7 @@
 	}
 	var_str_force_2(what, text);
 	if (srch) free(srch);
+	free(text);
 	free(what);
 } 
 
@@ -2532,6 +2540,6 @@
 	if (talker_logontype & LOGONMASK_SUMMONED) value[pos++] = 'S';
 	value[pos] = 0;
 
-	var_str_force_2(what, strdup(value));
+	var_str_force_2(what, value);
 	free(what);
 }

Modified: trunk/src/sqlite.c
===================================================================
--- trunk/src/sqlite.c	2012-11-29 18:55:24 UTC (rev 1306)
+++ trunk/src/sqlite.c	2012-11-30 18:25:58 UTC (rev 1307)
@@ -87,6 +87,8 @@
 	}
 
 	new = malloc(sizeof(*new));
+	if (new == NULL)
+		return NULL;
 	new->rows = -1;
 	new->cols = -1;
 	new->colNames = NULL;
@@ -114,6 +116,7 @@
 		}
 		sqlite3_free(error);
 		db_close(db);
+		free(new);
 		return NULL;
 	}
 
@@ -254,21 +257,20 @@
 {
 	struct db_result *old;
 	char *value;
-	char *out;
+	char *out = NULL;
 
 	char *query = sqlite3_mprintf("SELECT * from %Q WHERE user=%Q AND opt=%Q", table, user, opt);
 	old  = db_query(USERSQL, query, 1);
 	sqlite3_free(query);
 
 	if (old == NULL) return NULL;
-	if (db_numrows(old) < 1) return NULL;
+	if (db_numrows(old) < 1) goto out_dbfree;
 
 	if ((value = db_item(old, 0, "arg"))==NULL)
-	{
-		db_free(old);
-		return NULL;
-	}
+		goto out_dbfree;
+
 	out = strdup(value);
+out_dbfree:
 	db_free(old);
 	return out;
 }
@@ -319,21 +321,20 @@
 {
 	struct db_result *old;
 	char *value;
-	char *out;
+	char *out = NULL;
 
 	char *query = sqlite3_mprintf("SELECT * from "USERDB_ROOMS" WHERE room=%d AND opt=%Q", room, opt);
 	old  = db_query(USERSQL, query, 1);
 	sqlite3_free(query);
 
 	if (old == NULL) return NULL;
-	if (db_numrows(old) < 1) return NULL;
+	if (db_numrows(old) < 1) goto out_dbfree;
 
 	if ((value = db_item(old, 0, "arg"))==NULL)
-	{
-		db_free(old);
-		return NULL;
-	}
+		goto out_dbfree;
+
 	out = strdup(value);
+out_dbfree:
 	db_free(old);
 	return out;
 }

Modified: trunk/src/talker.c
===================================================================
--- trunk/src/talker.c	2012-11-29 18:55:24 UTC (rev 1306)
+++ trunk/src/talker.c	2012-11-30 18:25:58 UTC (rev 1307)
@@ -1489,12 +1489,15 @@
 	broadcast_onoffcode(1, 0, NULL, NULL);
 
 	char *topic = db_room_get(user->room, "topic");
-	if (topic != NULL && *topic != 0) {
+	if (topic == NULL)
+		return;
+
+	if (*topic != 0) {
 		AUTOFREE_BUFFER buff=NULL;
 		asprintf(&buff, "Topic: %s\n", topic);
 		display_message(buff, 0, 1);
 	}
-	if (topic != NULL) free(topic);
+	free(topic);
 }
 
 /**** actual talker utilities *****/

Modified: trunk/src/vars.c
===================================================================
--- trunk/src/vars.c	2012-11-29 18:55:24 UTC (rev 1306)
+++ trunk/src/vars.c	2012-11-30 18:25:58 UTC (rev 1307)
@@ -40,7 +40,7 @@
         return VAR_INT_VAL(&op);
 }
 
-void var_str_force_2(char *name, char *val)
+void var_str_force_2(const char *name, const char *val)
 {
     var_op_t op;
 




More information about the mw-devel mailing list