[PATCH misc-utils/rev] Various fixes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Revisions of rev(1) produces the following fixes:
    * Change indentation to 8 characters, for better reading the code.

    * Add some memory allocation error handling.

    * Fix memory leaks. In cases when Ctrl-C is used to exit the program,
      'p' cannot be freed, so made it a global var, to share between main()
      and sig_handler(). Signal handing is necessary to fix some leaks, so
      added a very basic, non invasive, mechanism.

Signed-off-by: Davidlohr Bueso <dave@xxxxxxx>
---
 text-utils/rev.c |  190 +++++++++++++++++++++++++++++++-----------------------
 1 files changed, 109 insertions(+), 81 deletions(-)

diff --git a/text-utils/rev.c b/text-utils/rev.c
index 3ae38cf..4ea5e09 100644
--- a/text-utils/rev.c
+++ b/text-utils/rev.c
@@ -41,7 +41,10 @@
  * 	added Native Language Support
  * 1999-09-19 Bruno Haible <haible@xxxxxxxxxxxxxx>
  * 	modified to work correctly in multi-byte locales
- *
+ * July 2010 - Davidlohr Bueso <dave@xxxxxxx>
+ *      Fixed memory leaks (including Linux signal handling)
+ *      Added some memory allocation error handling  
+ *      Changed tab indentation to 8 chars for better reading the code
  */
 
 #include <stdarg.h>
@@ -51,91 +54,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <signal.h>
 #include <err.h>
 
 #include "nls.h"
 #include "widechar.h"
 
-void usage(void);
-
-int
-main(int argc, char *argv[])
-{
-  register char *filename;
-  register wchar_t *t;
-  size_t buflen = 512;
-  wchar_t *p = malloc(buflen*sizeof(wchar_t));
-  size_t len;
-  FILE *fp;
-  int ch, rval;
-
-  setlocale(LC_ALL, "");
-  bindtextdomain(PACKAGE, LOCALEDIR);
-  textdomain(PACKAGE);
-
-  while ((ch = getopt(argc, argv, "")) != -1)
-    switch(ch) {
-    case '?':
-    default:
-      usage();
-    }
-
-  argc -= optind;
-  argv += optind;
-
-  fp = stdin;
-  filename = "stdin";
-  rval = 0;
-  do {
-    if (*argv) {
-      if ((fp = fopen(*argv, "r")) == NULL) {
-	warn("cannot open %s", *argv );
-	rval = 1;
-	++argv;
-	continue;
-      }
-      filename = *argv++;
-    }
-
-    while (fgetws(p, buflen, fp)) {
-
-      len = wcslen(p);
-
-      /* This is my hack from setpwnam.c -janl */
-      while (p[len-1] != '\n' && !feof(fp)) {
-	/* Extend input buffer if it failed getting the whole line */
-
-	/* So now we double the buffer size */
-	buflen *= 2;
-
-	p = realloc(p, buflen*sizeof(wchar_t));
-	if (p == NULL)
-	  err(1, _("unable to allocate bufferspace"));
-
-	/* And fill the rest of the buffer */
-	if (fgetws(&p[len], buflen/2, fp) == NULL) break;
-
-	len = wcslen(p);
-
-	/* That was a lot of work for nothing.  Gimme perl! */
-      }
-
-      t = p + len - 1 - (*(p+len-1)=='\r' || *(p+len-1)=='\n');
-      for ( ; t >= p; --t)
-	if (*t != 0)
-	  putwchar(*t);
-      putwchar('\n');
-    }
-    fflush(fp);
-    if (ferror(fp)) {
-      warn("%s", filename);
-      rval = 1;
-    }
-    if (fclose(fp))
-      rval = 1;
-  } while(*argv);
-  exit(rval);
-}
+/* if not global, we cannot free it when SIGINTs occur */
+wchar_t *p;
 
 void
 usage(void)
@@ -143,3 +69,105 @@ usage(void)
 	(void)fprintf(stderr, _("usage: rev [file ...]\n"));
 	exit(1);
 }
+
+static void 
+sig_handler(int signo)
+{
+	switch(signo) {
+	case SIGINT: /* very used Ctrl-C when no file is passed as argv */
+	case SIGTERM:
+		if(p)
+			free(p);
+	}
+
+	exit (EXIT_SUCCESS);
+}
+
+int
+main(int argc, char *argv[])
+{
+	register char *filename;
+	register wchar_t *t;
+	size_t len, buflen = 512;
+	FILE *fp;
+	int ch, rval;
+
+	setlocale(LC_ALL, "");
+	bindtextdomain(PACKAGE, LOCALEDIR);
+	textdomain(PACKAGE);
+	
+	signal(SIGINT, sig_handler);
+	signal(SIGTERM, sig_handler);
+	
+	/* if no mem left, no bother continuing */
+	p = malloc(buflen*sizeof(wchar_t));
+	if (p == NULL)
+		err(1, _("unable to allocate bufferspace"));
+
+	while ((ch = getopt(argc, argv, "")) != -1)
+		switch(ch) {
+		case '?':
+		default:
+			free(p);
+		usage(); 
+		}
+
+	argc -= optind;
+	argv += optind;
+
+	fp = stdin;
+	filename = "stdin";
+	rval = 0;
+	do {
+		if (*argv) {
+			if ((fp = fopen(*argv, "r")) == NULL) {
+				warn("cannot open %s", *argv );
+				rval = 1;
+				++argv;
+				continue;
+			}
+			filename = *argv++;
+		}
+
+		while (fgetws(p, buflen, fp)) {
+
+			len = wcslen(p);
+
+			/* This is my hack from setpwnam.c -janl */
+			while (p[len-1] != '\n' && !feof(fp)) {
+				/* Extend input buffer if it failed getting the whole line */
+
+				/* So now we double the buffer size */
+				buflen *= 2;
+
+				p = realloc(p, buflen*sizeof(wchar_t));
+				if (p == NULL)
+					err(1, _("unable to allocate bufferspace"));
+
+				/* And fill the rest of the buffer */
+				if (fgetws(&p[len], buflen/2, fp) == NULL) break;
+
+				len = wcslen(p);
+
+				/* That was a lot of work for nothing.  Gimme perl! */
+			}
+
+			t = p + len - 1 - (*(p+len-1)=='\r' || *(p+len-1)=='\n');
+			for ( ; t >= p; --t)
+				if (*t != 0)
+					putwchar(*t);
+			putwchar('\n');
+		}
+		fflush(fp);
+		if (ferror(fp)) {
+			warn("%s", filename);
+			rval = 1;
+		}
+		if (fclose(fp))
+			rval = 1;
+	} while(*argv);
+
+	free(p);
+
+	exit(rval);
+}
-- 
1.7.0.4

--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux