public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Sven Eden" <sven.eden@gmx.de>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/ufed:master commit in: /
Date: Sat, 19 Jan 2013 21:43:36 +0000 (UTC)	[thread overview]
Message-ID: <1358631871.4c71fca78a4ff4afc8d8380c6221acecf58f123a.yamakuzure@gentoo> (raw)

commit:     4c71fca78a4ff4afc8d8380c6221acecf58f123a
Author:     Sven Eden <sven.eden <AT> gmx <DOT> de>
AuthorDate: Sat Jan 19 21:44:31 2013 +0000
Commit:     Sven Eden <sven.eden <AT> gmx <DOT> de>
CommitDate: Sat Jan 19 21:44:31 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/ufed.git;a=commit;h=4c71fca7

Fixed several memory leaks. According to valgrind, ufed is now leak free.

---
 ufed-curses-checklist.c |  107 +++++++++++++++++++++++++++--------------------
 1 files changed, 61 insertions(+), 46 deletions(-)

diff --git a/ufed-curses-checklist.c b/ufed-curses-checklist.c
index a2e3c88..89d8a4a 100644
--- a/ufed-curses-checklist.c
+++ b/ufed-curses-checklist.c
@@ -16,7 +16,7 @@ struct flag {
 	struct item item;
 	char *name;
 	char on;
-	char *state;
+	char state[5];
 	bool *isInstalled;
 	char **pkgs;
 	char **descr;
@@ -29,6 +29,7 @@ static int descriptionleft;
 static char *fayt;
 static struct item **faytsave;
 static size_t maxDescWidth = 0;
+static char *lineBuf = NULL;
 
 #define mkKey(x) x, sizeof(x)-1
 static const struct key keys[] = {
@@ -52,35 +53,37 @@ int firstNormalY = -1; //!< y of first not masked flag
 
 /* static functions */
 static char *getline(FILE *fp) {
-	size_t size;
-	char *result;
-
-	size = LINE_MAX;
-	result = malloc(size);
-	if(result == NULL)
-		ERROR_EXIT(-1, "Can not allocate %lu bytes\n", sizeof(char) * size);
-	if(fgets(result, size, fp) == NULL)
+	static size_t size = LINE_MAX;
+
+	if (NULL == lineBuf) {
+		lineBuf = malloc(size);
+		if (NULL == lineBuf)
+			ERROR_EXIT(-1, "Can not allocate %lu bytes for line buffer\n", sizeof(char) * size);
+	}
+
+	if(fgets(lineBuf, size, fp) == NULL)
 		return NULL;
-	{
-		char *p = strchr(result, '\n');
-		if(p!=NULL) {
-			*p++ = '\0';
-			p = realloc(result, p-result);
-			return p ? p : result;
+	else {
+		char *p = strchr(lineBuf, '\n');
+		if(p != NULL) {
+			*p = '\0';
+			return lineBuf;
 		}
 	}
 	for(;;) {
-		result = realloc(result, size+size/2);
-		if(result == NULL)
-			ERROR_EXIT(-1, "Can not reallocate %lu bytes\n", (size_t)(sizeof(char) * size * 1.5));
-		if(fgets(result+size, size/2, fp)==NULL)
+		char *newLine = realloc(lineBuf, size + size / 2);
+		if(newLine == NULL)
+			ERROR_EXIT(-1, "Can not reallocate %lu bytes for line buffer\n",
+				(size_t)(sizeof(char) * size * 1.5));
+		lineBuf = newLine;
+		newLine = NULL;
+		if(fgets(lineBuf + size, size / 2, fp) == NULL)
 			return NULL;
-		{
-			char *p = strchr(result+size, '\n');
-			if(p!=NULL) {
-				*p++ = '\0';
-				p = realloc(result, p-result);
-				return p ? p : result;
+		else {
+			char *p = strchr(lineBuf + size, '\n');
+			if(p != NULL) {
+				*p = '\0';
+				return lineBuf;
 			}
 		}
 		size += size/2;
@@ -90,37 +93,42 @@ static char *getline(FILE *fp) {
 
 static void read_flags(void) {
 	FILE *input = fdopen(3, "r");
-	char *line;
-	int y=0;
+	int   y     = 0;
+	char *line  = NULL;
 
-	if(input==NULL)
+	if(input == NULL)
 		ERROR_EXIT(-1, "fdopen failed with error %d\n", errno);
 	atexit(&free_flags);
+
 	for(;;) {
 		struct {
 			int start, end;
 		} name, on, state, pkgs, desc;
-		int ndescr;
-		struct flag *flag;
-		char descState;
+		int ndescr = 0;
+		struct flag *flag = NULL;
+		char descState = 0;
 
 		line = getline(input);
-		if(line==NULL)
+		if(NULL == line)
 			break;
 		if(sscanf(line, "%n%*s%n %n%*s%n %n(%*[ +-])%n %d",
 				&name.start,  &name.end,
 				&on.start,    &on.end,
 				&state.start, &state.end,
-				&ndescr)!=1)
+				&ndescr) != 1)
 			ERROR_EXIT(-1, "flag sscanf failed on line\n\"%s\"\n", line);
 
 		/* Allocate memory for the struct and the arrays */
 		// struct
-		if (NULL == (flag = (struct flag*)calloc(1, sizeof(struct flag))))
+		if (NULL == (flag = (struct flag*)malloc(sizeof(struct flag))))
 			ERROR_EXIT(-1, "Can not allocate %lu bytes for flag\n", sizeof(struct flag));
 		// isInstalled
 		if (NULL == (flag->isInstalled = (bool*)calloc(ndescr, sizeof(bool))))
 			ERROR_EXIT(-1, "Can not allocate %lu bytes for isInstalled array\n", ndescr * sizeof(bool));
+		// name
+		if (NULL == (flag->name = (char*)calloc(name.end - name.start + 1, sizeof(char))))
+			ERROR_EXIT(-1, "Can not allocate %lu bytes for flag name\n",
+				(name.end - name.start + 1) * sizeof(char));
 		// pkgs
 		if (NULL == (flag->pkgs = (char**)calloc(ndescr, sizeof(char*))))
 			ERROR_EXIT(-1, "Can not allocate %lu bytes for pkg array\n", ndescr * sizeof(char*));
@@ -131,10 +139,9 @@ static void read_flags(void) {
 		/* note position and name of the flag */
 		flag->item.top = y;
 
-		line[name.end] = '\0';
-		if(name.end-name.start+11 > minwidth)
-			minwidth = name.end-name.start+11;
-		flag->name = &line[name.start];
+		if(name.end - name.start + 11 > minwidth)
+			minwidth = name.end - name.start + 11;
+		strncpy(flag->name, &line[name.start], name.end - name.start);
 
 		/* check and save current flag setting from configuration */
 		line[on.end] = '\0';
@@ -148,10 +155,9 @@ static void read_flags(void) {
 			ERROR_EXIT(-1, "flag->on can not be determined with \"%s\"\n", &line[on.start]);
 
 		/* check and set flag state */
-		line[state.end] = '\0';
-		if(state.end-state.start != 4)
-			ERROR_EXIT(-1, "state.end - state.start is %d (must be 4)\n", state.end - state.start);
-		flag->state = &line[state.start];
+		if(state.end - state.start != 4)
+			ERROR_EXIT(-1, "state length is %d (must be 4)\n", state.end - state.start);
+		strncpy(flag->state, &line[state.start], 4);
 
 		/* check and set flag item height */
 		flag->item.height = ndescr;
@@ -162,9 +168,10 @@ static void read_flags(void) {
 		for (int i = 0; i < ndescr; ++i) {
 			pkgs.start = pkgs.end = -1;
 			desc.start = desc.end = -1;
+
 			line = getline(input);
-			if(line == NULL)
-				break;
+			if (!line) break;
+
 			/* There are two possible layouts:
 			 * a: "g [description]" for global flag descriptions and
 			 * b: "x (pkg(s)) [description]" for local flag descriptions
@@ -217,8 +224,6 @@ static void read_flags(void) {
 			size_t fullWidth = 1 + strlen(flag->descr[i]) + (flag->pkgs[i] ? strlen(flag->pkgs[i] + 3) : 0);
 			if (fullWidth > maxDescWidth)
 				maxDescWidth = fullWidth;
-
-			free(line);
 		} // loop through description lines
 
 		/* record first not masked y if not done, yet */
@@ -248,6 +253,8 @@ static void read_flags(void) {
 
 static void free_flags(void) {
 	struct flag *flag = flags;
+
+	// Clear all flags
 	if(flag != NULL) {
 		flag->item.prev->next = NULL;
 		do {
@@ -256,6 +263,7 @@ static void free_flags(void) {
 				if (flag->pkgs[i])  free(flag->pkgs[i]);
 				if (flag->descr[i]) free(flag->descr[i]);
 			}
+			if (flag->name)        free(flag->name);
 			if (flag->isInstalled) free(flag->isInstalled);
 			if (flag->pkgs)        free(flag->pkgs);
 			if (flag->descr)       free(flag->descr);
@@ -264,6 +272,10 @@ static void free_flags(void) {
 		} while(flag != NULL);
 		flags = NULL;
 	}
+
+	// Clear line buffer
+	if (lineBuf)
+		free(lineBuf);
 }
 
 
@@ -555,5 +567,8 @@ int main(void) {
 		fclose(output);
 	}
 
+	if (fayt)     free(fayt);
+	if (faytsave) free(faytsave);
+
 	return result;
 }


             reply	other threads:[~2013-01-19 21:43 UTC|newest]

Thread overview: 238+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-19 21:43 Sven Eden [this message]
  -- strict thread matches above, loose matches on Subject: below --
2020-11-07 14:25 [gentoo-commits] proj/ufed:master commit in: / Sven Eden
2020-05-02  8:38 Ulrich Müller
2019-09-27  6:42 Sven Eden
2019-09-27  6:39 Sven Eden
2019-09-24 17:57 Sven Eden
2019-09-24 17:56 Sven Eden
2019-04-07 15:17 David Seifert
2019-04-07 13:56 David Seifert
2019-04-07 13:19 David Seifert
2019-04-07 13:19 David Seifert
2019-04-07 13:19 David Seifert
2019-04-07 13:19 David Seifert
2019-04-07 13:19 David Seifert
2015-02-12 15:47 Sven Eden
2015-02-11  9:03 Sven Eden
2014-11-10  9:59 Sven Eden
2014-10-28 11:43 Sven Eden
2014-02-26 10:26 Sven Eden
2014-02-26 10:26 Sven Eden
2014-02-26 10:26 Sven Eden
2014-02-26 10:26 Sven Eden
2014-02-26 10:26 Sven Eden
2014-02-26 10:26 Sven Eden
2014-02-25  8:18 Sven Eden
2014-02-25  8:18 Sven Eden
2014-02-25  8:18 Sven Eden
2014-02-25  8:18 Sven Eden
2013-11-25 21:43 Sven Eden
2013-11-25 21:43 Sven Eden
2013-11-25 21:43 Sven Eden
2013-11-25 21:43 Sven Eden
2013-11-25 21:43 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-20  8:30 Sven Eden
2013-09-11  7:09 Sven Eden
2013-09-11  6:31 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-11  6:04 Sven Eden
2013-09-10 12:37 Sven Eden
2013-09-10  6:36 Sven Eden
2013-09-10  6:36 Sven Eden
2013-09-10  6:36 Sven Eden
2013-09-10  6:36 Sven Eden
2013-09-10  6:36 Sven Eden
2013-09-10  6:36 Sven Eden
2013-09-10  6:36 Sven Eden
2013-07-22  9:34 Sven Eden
2013-07-22  6:09 Sven Eden
2013-07-22  6:09 Sven Eden
2013-04-09  7:22 Sven Eden
2013-04-09  7:22 Sven Eden
2013-04-09  7:22 Sven Eden
2013-04-08  7:18 Sven Eden
2013-04-03 13:39 Sven Eden
2013-03-05 16:53 Sven Eden
2013-03-05 16:49 Sven Eden
2013-03-05 16:49 Sven Eden
2013-03-05 16:49 Sven Eden
2013-03-05 16:49 Sven Eden
2013-03-05 16:49 Sven Eden
2013-02-21 10:02 Sven Eden
2013-02-19 15:16 Sven Eden
2013-02-19 13:34 Sven Eden
2013-02-18  7:22 Sven Eden
2013-02-15  8:36 Sven Eden
2013-02-15  8:36 Sven Eden
2013-02-15  8:36 Sven Eden
2013-02-14  8:35 Sven Eden
2013-02-14  8:35 Sven Eden
2013-02-14  8:35 Sven Eden
2013-02-13  9:23 Sven Eden
2013-02-13  9:23 Sven Eden
2013-02-13  9:23 Sven Eden
2013-02-13  9:23 Sven Eden
2013-02-13  9:23 Sven Eden
2013-02-12 10:51 Sven Eden
2013-02-12 10:51 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-12  9:01 Sven Eden
2013-02-06  9:09 Sven Eden
2013-02-06  9:09 Sven Eden
2013-02-05 18:06 Paul Varner
2013-02-05 13:53 Sven Eden
2013-02-05 13:53 Sven Eden
2013-02-05 11:24 Sven Eden
2013-02-03 14:32 Sven Eden
2013-02-03 14:32 Sven Eden
2013-02-03 14:32 Sven Eden
2013-02-03 14:32 Sven Eden
2013-02-03 14:32 Sven Eden
2013-02-02 20:49 Sven Eden
2013-02-02 10:11 Sven Eden
2013-02-02  9:47 Sven Eden
2013-02-02  9:47 Sven Eden
2013-02-02  9:47 Sven Eden
2013-02-01 21:12 Sven Eden
2013-02-01 21:12 Sven Eden
2013-02-01 21:12 Sven Eden
2013-02-01 16:04 Sven Eden
2013-02-01 15:55 Sven Eden
2013-02-01 15:26 Sven Eden
2013-02-01 14:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-02-01 10:49 Sven Eden
2013-01-24 10:15 Sven Eden
2013-01-24 10:15 Sven Eden
2013-01-24 10:15 Sven Eden
2013-01-24 10:15 Sven Eden
2013-01-24 10:15 Sven Eden
2013-01-23 14:44 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-23 12:05 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-19 21:43 Sven Eden
2013-01-16 13:43 Sven Eden
2013-01-16 12:56 Sven Eden
2013-01-16 12:56 Sven Eden
2013-01-16 12:56 Sven Eden
2013-01-16 12:56 Sven Eden
2013-01-16 12:56 Sven Eden
2013-01-16 12:56 Sven Eden
2013-01-08 11:02 Sven Eden
2013-01-02  8:47 Sven Eden
2013-01-02  8:01 Sven Eden
2013-01-02  8:01 Sven Eden
2012-11-20 17:31 Paul Varner
2012-11-20 17:25 Paul Varner
2012-10-23 16:13 Paul Varner
2012-10-23 16:13 Paul Varner
2012-10-23 16:13 Paul Varner
2012-10-23 16:01 Paul Varner
2012-10-22 20:42 Paul Varner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1358631871.4c71fca78a4ff4afc8d8380c6221acecf58f123a.yamakuzure@gentoo \
    --to=sven.eden@gmx.de \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox