[mw-devel] [Git][arthur/mw][master] 4 commits: Plug some memory leaks

Andrew Price welshbyte at sucs.org
Mon Nov 9 02:09:46 GMT 2015


Andrew Price pushed to branch master at Justin Mitchell / mw


Commits:
bb06aa35 by Andrew Price at 2015-11-09T01:26:09Z
Plug some memory leaks

Spotted by scan-build

- - - - -
1d4b9ef8 by Andrew Price at 2015-11-09T01:46:47Z
Rename is_old to user_exists

- - - - -
15904791 by Andrew Price at 2015-11-09T01:48:46Z
Remove todo.txt

Half of it relates to MUD mode, the other half is out of date.

- - - - -
c6cb079d by Andrew Price at 2015-11-09T02:05:29Z
Remove capitalise.awk

sed does it better.

- - - - -


16 changed files:

- Makefile.common
- − capitalise.awk
- src/client/add.c
- src/client/edit.c
- src/client/js.c
- src/client/main.c
- src/client/mesg.c
- src/client/mod.c
- src/client/newmain.c
- src/client/read.c
- src/client/uri.c
- src/client/user.c
- src/user.c
- src/user.h
- src/webclient/mwpoll.c
- − todo.txt


Changes:

=====================================
Makefile.common
=====================================
--- a/Makefile.common
+++ b/Makefile.common
@@ -57,7 +57,7 @@ endif
 
 # info strings, do not edit.
 DEFS:= -DBUILD_DATE=\"$(shell date +%Y%m%d)\"
-DEFS+= -DBUILD_USER=\"$(shell whoami | awk -f $(DEPTH)capitalise.awk)\"
+DEFS+= -DBUILD_USER=\"$(shell whoami | sed 's/^./\u&/')\"
 DEFS+= -DVER_MAJ=\"$(VERSION_MAJOR)\"
 DEFS+= -DVER_MIN=\"$(VERSION_MINOR)\"
 DEFS+= -DVER_TWK=\"$(VERSION_TWEAK)\"


=====================================
capitalise.awk deleted
=====================================
--- a/capitalise.awk
+++ /dev/null
@@ -1,3 +0,0 @@
-{
-	print(toupper(substr($1, 1, 1)) substr($1, 2))
-}


=====================================
src/client/add.c
=====================================
--- a/src/client/add.c
+++ b/src/client/add.c
@@ -197,7 +197,7 @@ int add_msg(int folnum, struct user *user, int replyto)
 		get_str(new->to,NAMESIZE);
 		strncpy(t,new->to,NAMESIZE);
 		strip_name(t);
-		if (is_private(fol,user) && !is_old(t, &usr))
+		if (is_private(fol,user) && !user_exists(t, &usr))
 		{
 			printf(_("Message must be addressed to an existing user.\n"));
 			free(fol);
@@ -217,7 +217,7 @@ int add_msg(int folnum, struct user *user, int replyto)
 		{
 			printf(_("Send to: "));
 			get_str(new->to,SUBJECTSIZE);
-			if (is_private(fol,user) && !is_old(new->to, &usr))
+			if (is_private(fol,user) && !user_exists(new->to, &usr))
 			{
 				printf(_("Message must be addressed to an existing user.\n"));
 				free(fol);


=====================================
src/client/edit.c
=====================================
--- a/src/client/edit.c
+++ b/src/client/edit.c
@@ -15,6 +15,8 @@
 #include <time.h>
 #include <stdbool.h>
 #include <sys/stat.h>
+
+#include <util.h>
 #include "talker_privs.h"
 #include "special.h"
 #include "incoming.h"
@@ -145,7 +147,7 @@ void edit_user(const char *args, const char *name)
 	struct user u;
 	struct person *usr = &u.record;
 
-	if (!is_old(name, &u))
+	if (!user_exists(name, &u))
 	{
 		printf(_("Username %s not found.\n"),name);
 		return;
@@ -411,7 +413,7 @@ void edit_user(const char *args, const char *name)
 			char answer[10];
 			struct user uu;
 
-			if (is_old(username, &uu))
+			if (user_exists(username, &uu))
 			{
 				/* it exists, so is it the right person */
 				if (u.posn != uu.posn)
@@ -774,7 +776,7 @@ void mesg_edit(const char *args, struct folder *folder, int msgno, struct user *
 {
 	int fd, text;
 	struct Header head;
-	char *buff;
+	_autofree char *buff = NULL;
 
 	fd = open_folder_index(folder, FOL_LIVE, O_RDWR, 0);
 	if (fd < 0)
@@ -936,7 +938,6 @@ void mesg_edit(const char *args, struct folder *folder, int msgno, struct user *
 	Unlock_File(fd);
 	close(fd);
 	close(text);
-	free(buff);
 }
 
 void time_on(long u)


=====================================
src/client/js.c
=====================================
--- a/src/client/js.c
+++ b/src/client/js.c
@@ -288,8 +288,8 @@ static JSBool js_input(JSContext *cx, unsigned int argc, jsval *vp)
 	size_t ucs2_length, prompt_length, line_length;
 	const jschar *ucs2_string;
 	int conv_error;
-
-	char *prompt=NULL, *line;
+	_autofree char *prompt = NULL;
+	_autofree char *line = NULL;
 
 	if(argc > 0) {
 		if(JSVAL_IS_STRING(argv[0])) {
@@ -303,8 +303,6 @@ static JSBool js_input(JSContext *cx, unsigned int argc, jsval *vp)
 				conv_error=convert_string_charset((char *)ucs2_string, "UCS-2", ucs2_length*sizeof(jschar), prompt, "LOCAL//TRANSLIT", prompt_length, NULL, NULL, NULL, NULL, NULL);
 				if(conv_error<0) {
 					fprintf(stderr, "js_input: convert_string_charset failed with error: %d\n", conv_error);
-					free(prompt);
-					prompt=NULL;
 				}
 			}
 		} else {
@@ -322,8 +320,6 @@ static JSBool js_input(JSContext *cx, unsigned int argc, jsval *vp)
 	js_start_timeout();
 	busy--;
 
-	free(prompt);
-
 	line_length = sizeof(jschar) * (strlen(line) + 1);
 
 	// likewise as this has come straight from a readline we need a convert from local charset


=====================================
src/client/main.c
=====================================
--- a/src/client/main.c
+++ b/src/client/main.c
@@ -468,7 +468,7 @@ int main(int argc, char **argv)
 		b=(char *)getmylogin();
 		if (b!=NULL)
 		{
-			if (is_old(b, user))
+			if (user_exists(b, user))
 				list_new_items(user,true);
 		}else
 			printf(_("Username not permitted.\n"));
@@ -480,7 +480,7 @@ int main(int argc, char **argv)
 		if (b!=NULL)
 		{
 			/* try and load user - if ok, view since info */
-			if (is_old(b, user))
+			if (user_exists(b, user))
 			{
 				/* view since */
 				list_users_since(user->record.lastlogout);
@@ -536,7 +536,7 @@ int main(int argc, char **argv)
 	{
 		const char *name = argv[msguser_num];
 
-		if (!is_old(name, user))
+		if (!user_exists(name, user))
 		{
 			fprintf(stderr,_("%s: User %s not found.\n"), argv[0], name);
 			exit(-1);
@@ -559,7 +559,7 @@ int main(int argc, char **argv)
 			fprintf(stderr,_("%s: Folder %s not found.\n"), argv[0], foldname);
 			exit(-1);
 		}
-		if (!is_old(folduser, user))
+		if (!user_exists(folduser, user))
 		{
 			fprintf(stderr,_("%s: User %s not found.\n"), argv[0], folduser);
 			exit(-1);


=====================================
src/client/mesg.c
=====================================
--- a/src/client/mesg.c
+++ b/src/client/mesg.c
@@ -5,6 +5,8 @@
 #include <unistd.h>
 #include <string.h>
 #include <stdarg.h>
+
+#include <util.h>
 #include "talker_privs.h"
 #include "special.h"
 #include "ipc.h"
@@ -19,16 +21,18 @@ extern struct user * const user;
 void send_mesg(char *from, const char *to, char *text, int wiz)
 {
 	char buff[MAXTEXTLENGTH];
-	struct user *usr;
+	_autofree struct user *usr = calloc(1, sizeof(*usr));
 
-	usr = user_get(to);
-	if (! usr) {
+	if (usr == NULL) {
+		perror("calloc");
+		return;
+	}
+	if (!user_exists(to, usr)) {
 		printf(_("User does not exist.\n"));
 		return;
 	}
 	if (! u_reg(usr)) {
 		printf(_("%s is not registered.\n"),to);
-		free(usr);
 		return;
 	}
 	if (u_mesg(usr)) {
@@ -36,12 +40,9 @@ void send_mesg(char *from, const char *to, char *text, int wiz)
 			printf(_("%s has msg off, writing anyway.\n"),to);
 		} else {
 			printf(_("%s has turned messages off.\n"),to);
-			free(usr);
 			return;
 		}
 	}
-	free(usr);
-
 	snprintf(buff, MAXTEXTLENGTH, "*** %s: %s", from, text);
 	if (ipc_send_to_username(to, IPC_WIZ, buff) <= 0) {
 		printf(_("User not logged on.\n"));


=====================================
src/client/mod.c
=====================================
--- a/src/client/mod.c
+++ b/src/client/mod.c
@@ -118,6 +118,7 @@ void moderate(void)
 			if ((foo=read(tmptext,text,head.size))<head.size)
 			{
 				printf(_("only %d of %d bytes read.\n"),foo,head.size);
+				free(text);
 				return;
 			}
 			display_article(&head,tmptext);


=====================================
src/client/newmain.c
=====================================
--- a/src/client/newmain.c
+++ b/src/client/newmain.c
@@ -575,8 +575,6 @@ void c_topten(CommandList *cm, int argc, const char **argv, char *args)
 {
 	struct listing *head=NULL;
 	struct Header hdr;
-	struct listing *lnew;
-	struct listing *ptr;
 	struct folder folder;
 	int file;
 	int count;
@@ -594,7 +592,7 @@ void c_topten(CommandList *cm, int argc, const char **argv, char *args)
 
 	if ((file = open_folder_index(&folder, FOL_LIVE, O_RDONLY, 0)) < 0)
 		return;
-	lnew=(struct listing *)malloc(sizeof(*lnew));
+
 	while(read(file,&hdr,sizeof(hdr))>0)
 	{
 		struct listing *listing = head;
@@ -608,12 +606,12 @@ void c_topten(CommandList *cm, int argc, const char **argv, char *args)
 		}
 		else
 		{
+			struct listing *lnew = calloc(1, sizeof(*lnew));
 			strcpy(lnew->name,hdr.from);
 			lnew->count=1;
 			lnew->size=hdr.size;
 			lnew->next=head;
 			head=lnew;
-			lnew=(struct listing *)malloc(sizeof(*lnew));
 		}
 	}
 	head=Sort(head);
@@ -621,12 +619,13 @@ void c_topten(CommandList *cm, int argc, const char **argv, char *args)
 	printf("Top posters in folder %s\n", folder.name);
 	printf("In order of Total size of text posted.\n");
 
-	ptr=head;
 	count=2;
-	while (ptr!=NULL)
+	while (head != NULL)
 	{
-		printf("Name: %*s   %3d Msgs.   %5d bytes (%2.1fK)\n",NAMESIZE,ptr->name,ptr->count,ptr->size,ptr->size/1024.0);
-		ptr=ptr->next;
+		struct listing *l;
+
+		printf("Name: %*s   %3d Msgs.   %5d bytes (%2.1fK)\n", NAMESIZE, head->name,
+		       head->count, head->size, head->size/1024.0);
 		count++;
 		if (count>=screen_height-2)
 		{
@@ -636,6 +635,9 @@ void c_topten(CommandList *cm, int argc, const char **argv, char *args)
 			if (*buff=='q' || *buff=='Q') exit(0);
 			count=0;
 		}
+		l = head;
+		head = head->next;
+		free(l);
 	}
 }
 


=====================================
src/client/read.c
=====================================
--- a/src/client/read.c
+++ b/src/client/read.c
@@ -74,6 +74,7 @@ void display_article(struct Header *tmp, int datafile)
 	{
 		printf("Failed to read entire data entry");
 		perror("read data");
+		free(buff);
 		return;
 	}
 	if (!remote)


=====================================
src/client/uri.c
=====================================
--- a/src/client/uri.c
+++ b/src/client/uri.c
@@ -5,6 +5,7 @@
 #include <readline/readline.h>
 
 #include <sqlite.h>
+#include <util.h>
 #include "uri.h"
 #include "sqlite.h"
 #include "talker.h"
@@ -232,9 +233,13 @@ void uri_list(int argc, const char **argv, int wiz)
 {
 	char *username = NULL; // default is to display everyone's uris
 	int number = 10; // default is to display 10 uris
-	struct user *list_user = NULL;
+	_autofree struct user *list_user = calloc(1, sizeof(*list_user));
 	char c;
 
+	if (list_user == NULL) {
+		perror("calloc");
+		return;
+	}
 	if(argc == 1) // just '.uri list'
 	{
 		username = user->record.name;
@@ -266,7 +271,7 @@ void uri_list(int argc, const char **argv, int wiz)
 		}
 
 	}
-	else if( (list_user = user_get(argv[1])) != NULL) // is a real user
+	else if (user_exists(argv[1], list_user))
 	{
 		username = list_user->record.name;
 		if(argc==3)
@@ -275,21 +280,18 @@ void uri_list(int argc, const char **argv, int wiz)
 			{
 				if(number < 1)
 				{
-					free(list_user);
 					printf(".uri list %s - can't list <1 uris\n", argv[1]);
 					return;
 				}
 			}
 			else
 			{
-				free(list_user);
 				printf(".uri list %s - last parameter was not a positive integer\n", argv[1]);
 				return;
 			}
 		}
 		if(argc>3)
 		{
-			free(list_user);
 			printf(".uri list %s - too many parameters\n", argv[1]);
 			return;
 		}
@@ -323,10 +325,6 @@ void uri_list(int argc, const char **argv, int wiz)
 	}
 
 	uri_list_display(number, username);
-
-	if(list_user) {
-		free(list_user);
-	}
 }
 
 // checks an id supplied to uri_delete/nsfw etc. is valid or creates an id if the user specified 'last'


=====================================
src/client/user.c
=====================================
--- a/src/client/user.c
+++ b/src/client/user.c
@@ -301,7 +301,7 @@ void login_ok(struct user *usr, int *autochat)
 	int okay;
 	do{
 		get_login(name, *autochat);
-		if(is_old(name, usr))
+		if(user_exists(name, usr))
 			okay=old_usr(usr);
 		else
 			okay=new_usr(name, usr);


=====================================
src/user.c
=====================================
--- a/src/user.c
+++ b/src/user.c
@@ -132,23 +132,9 @@ static int user_find_name(const char *name, struct user *user, int *found)
 	return 0;
 }
 
-int is_old(const char *name, struct user *usr)
+int user_exists(const char *name, struct user *usr)
 {
         int found;
 
 	return !user_find_name(name, usr, &found) && found;
 }
-
-/*
- * Get the user DB record for a named user
- * Returns a malloc()ed record, or NULL on error.
- */
-struct user *user_get(const char *name)
-{
-	struct user *usr;
-
-	usr = calloc(1, sizeof(*usr));
-	if (usr != NULL && is_old(name, usr))
-		return usr;
-	return NULL;
-}


=====================================
src/user.h
=====================================
--- a/src/user.h
+++ b/src/user.h
@@ -85,8 +85,7 @@ extern int fetch_first_user(int fd, struct user *user);
 extern int fetch_next_user(int fd, struct user *user);
 extern void update_user(struct user *user);
 extern int update_user_fd(int fd, struct user *user);
-extern int is_old(const char *name, struct user *user);
-extern struct user* user_get(const char *name);
+extern int user_exists(const char *name, struct user *user);
 
 #define for_each_user(userp, fd, status) \
 	for((status) = fetch_first_user((fd), (userp)); \


=====================================
src/webclient/mwpoll.c
=====================================
--- a/src/webclient/mwpoll.c
+++ b/src/webclient/mwpoll.c
@@ -125,10 +125,8 @@ int main(int argc, char ** argv)
 		if ((p=strchr(rawpw,'\n'))!=NULL) *p=0;
 	}
 
-	/* fetch the user record */
-	if (!is_old(username, user)) {
+	if (!user_exists(username, user)) {
 		if (autocreate) {
-			/* user doesnt exist, create them */
 			create_user(user, username, rawpw);
 		} else {
 			printf("User '%s' not found.\n", username);


=====================================
todo.txt deleted
=====================================
--- a/todo.txt
+++ /dev/null
@@ -1,131 +0,0 @@
-=================
-== BUGS/NEEDED ==
-=================
-
-
-  Rooms
-  ------
-
-
-  Code
-  -----
-
-
-  Help
-  -----
-
-
-
-===========
-== IDEAS ==
-===========
-
-
-  Rooms
-  ------
-
-   - If anyone wishes to start, there are some links for new areas around:
-
-     * 614:  Tunnel down to the sea. needs a nice half-way room with a stream
-             and quartz roof - for reference material see the Ulgo caves in the
-             Belgariad by David Eddings :)
-
-     * 21:   Path around the south of the manor, heads over downlands to the
-             sea cliffs - plan to do a nice walk here, beaches, a path down,
-             and some form of link to the above tunnel
-
-     * 1026: Hidden path behind waterfall would be nice. perhaps linking up
-             to the smugglers tunnel, and catacombs below cmc's house
-
-     * 410:  Catacombs below cmc's house
-
-     * 442:  Continue the lane northwards
-
-     * 37:   The Gatehouse/Lodge
-
-     * 470:  The hole
-
-     * 422:  Continue the lane southwards
-
-     * 20:   The wall, lawn etc.. all need doing
-
-     * 6000: Perhaps more on the milliways universe?
-
-   - There needs to be some catacombs system designed - would be nice to have
-     everything turn you around so you have to use left, right, forwards and
-     backwards, and depending on the direction you just came from these mean
-     different things - AKA the Discworld MUD UU Library.
-
-   - The beaches, cliffs, and downlands all need designing, with maybe a
-     lighthouse, boathouse, and even the ability to sail elsewhere.
-
-
-
-  Code
-  -----
-
-   - Add 'option lists' for extra tab-completion styles in scripts:
-     Allow to be added using 'binds' in .mwrc
-     Eg:
-	option-list bw black white
-		defines keyword 'bw' to contain 'black' and 'white'
-	option-list uname [usernames] dom
-		defines keyword 'uname' to be the list 'usernames' + 'dom'
-
-   - Add 'useralias' to check for alternative usernames
-     * add command 'useralias <username> <new alias>'
-       - this will not allow you to create an alias of a username
-         that doesnt exist
-       - this will not allow you to create an alias that is the
-         same as a user that already exists
-       - any useralias checks that fail will remove themselves from
-         the useralias list (ie, user has been deleted)
-         add command 'removeuseralias <alias>'
-       - this will not allow you to unalias an alias that does not
-         exist
-     * add alias listings to the end of 'part_who' (main.c)
-     * add alias listings to the end of 'part_who_talk' (main.c)
-     * add alias check to line 672 of script_inst.c in user ignore test
-       (ie, if cant find user, check for user alias first, and try again)
-     * add alias check to line 1544 of script_inst.c in 'ison' user check
-       (ie, if is_old fails, check for alias and is_old that too)
-     * add alias check to line 813 of talker.c, in 't_ignore' user check
-       (ie, if is_old fails, check for alias and is_old that too)
-     * add alias check for unignore, and qunignore in talker.c and
-       script_inst.c respectively (see previous lines for ignores)
-     * add alias check to line 1699 of talker.c (talk_sendto) in
-       username stricmp. (+lines 1705, 1708)
-     * same for talk_single, sendipc and sendrpc in talker.c
-     * same for 'get_pid' and 'get_user_pid' in who.c
-     * arg.. and in all the other places where usernames are used
-
-     *****************************************************************
-     *** COME UP WITH A FUNCTION THAT WILL CHANGE TEXT TO AN ALIAS ***
-     *** eg, 'CheckUserName(char *szInput, char *szOutput)'        ***
-     *** will return the aliased name, or if not found, the same   ***
-     *** name in output. therefore, any username checks just need  ***
-     *** to run this at the beginning!!! :)                        ***
-     *****************************************************************
-
-   - Text output preprocessing - need to run a script from any send routine
-     (such as sayto, talk etc...)
-     * 'outputevent' script command to bind an output event function
-     * any outputevent function can use 'SAY' etc.. to send text out
-     * use 'OUTPUT OFF' to suppress normal sending of text
-     * will only be able to work on normal talker output - not RAW, SAYTO etc..
-     * MUST BE ABLE TO STOP CONSTANT REDIRECTION WITH SCRIPTS. ie, if a script
-       uses a SAY, then that SAY must *not* be handled by the output script
-       or infinite loops will occur
-
-   - Get '!user timeout <user>' to automatically set the special 'T' flag
-     too? This needs discussion, because currently this allows for
-     current-process timeout changes, without it being persistent.
-
-   - Need to add 'time events' to MUD. eg, "Someone walks past whistling",
-     "You hear some church bells ring XXX o'clock", and "It begins to snow".
-     These would obviously have to be room/distance dependant.
-
-
-  Help
-  -----
-



View it on GitLab: https://projects.sucs.org/arthur/mw/compare/a4e857931e30b695efe0ed2fa85934c19d6d9e8d...c6cb079d56dad87eb146e74fe81e1d160747176b
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20151109/9e4e73a7/attachment-0001.html>


More information about the mw-devel mailing list