Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/fgen PR/50914: David Binderman: Fix memory leaks.



details:   https://anonhg.NetBSD.org/src/rev/178c4fb85058
branches:  trunk
changeset: 344001:178c4fb85058
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Mar 08 20:13:44 2016 +0000

description:
PR/50914: David Binderman: Fix memory leaks.
While here, fix error handling too.

diffstat:

 usr.bin/fgen/Makefile |    5 +-
 usr.bin/fgen/fgen.l   |  211 +++++++++++++++++++++++--------------------------
 2 files changed, 102 insertions(+), 114 deletions(-)

diffs (truncated from 635 to 300 lines):

diff -r ed36b86ed95f -r 178c4fb85058 usr.bin/fgen/Makefile
--- a/usr.bin/fgen/Makefile     Tue Mar 08 18:16:11 2016 +0000
+++ b/usr.bin/fgen/Makefile     Tue Mar 08 20:13:44 2016 +0000
@@ -1,8 +1,11 @@
-#      $NetBSD: Makefile,v 1.8 2009/10/29 14:35:25 christos Exp $
+#      $NetBSD: Makefile,v 1.9 2016/03/08 20:13:44 christos Exp $
 
 CPPFLAGS+= -I${.CURDIR}
 PROG=  fgen
 SRCS=  fgen.l
 MAN=   fgen.1
 
+LDADD+=        -lutil
+DPADD+=        ${LIBUTIL}
+
 .include <bsd.prog.mk>
diff -r ed36b86ed95f -r 178c4fb85058 usr.bin/fgen/fgen.l
--- a/usr.bin/fgen/fgen.l       Tue Mar 08 18:16:11 2016 +0000
+++ b/usr.bin/fgen/fgen.l       Tue Mar 08 20:13:44 2016 +0000
@@ -1,5 +1,5 @@
 %{
-/*     $NetBSD: fgen.l,v 1.36 2013/10/18 20:47:06 christos Exp $       */
+/*     $NetBSD: fgen.l,v 1.37 2016/03/08 20:13:44 christos Exp $       */
 /* FLEX input for FORTH input file scanner */
 /*  
  * Copyright (c) 1998 Eduardo Horvath.
@@ -42,7 +42,7 @@
 #endif
 
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: fgen.l,v 1.36 2013/10/18 20:47:06 christos Exp $");
+__RCSID("$NetBSD: fgen.l,v 1.37 2016/03/08 20:13:44 christos Exp $");
 #endif
 
 %}
@@ -66,6 +66,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <util.h>
 
 #include "fgen.h"
 static TOKEN ltoken;
@@ -101,7 +102,7 @@
 
 static int debug = 0;
 #define ASSERT if (debug) assert
-#define STATE(y, x)    do { if (debug) printf( "%lx State %s: token `%s'\n", outpos, x, y); } while (0)
+#define STATE(y, x)    do { if (debug) printf("%lx State %s: token `%s'\n", outpos, x, y); } while (0)
 static int mark_fload = 0;
 
 %}
@@ -310,7 +311,7 @@
 static int aadd(struct macro *, struct macro *);
 static struct macro *alookup(struct macro *, const char *);
 static void initdic(void);
-__dead static void usage(const char *);
+__dead static void usage(void);
 static void tokenize(YY_BUFFER_STATE);
 static int emit(const char *);
 static int spit(long);
@@ -787,10 +788,8 @@
        if (debug > 1)
                printf("push %lx\n", (long)val);
        parse_stack[parse_stack_ptr++] = val;
-       if (parse_stack_ptr >= PSTKSIZ) {
-               (void)printf( "Parse stack overflow\n");
-               exit(1);
-       }
+       if (parse_stack_ptr >= PSTKSIZ)
+               errx(EXIT_FAILURE, "Parse stack overflow");
 }
 
 static Cell
@@ -831,7 +830,7 @@
                        return fadd(dict->l, new);
                else {
                        if (debug > 5)
-                               (void)printf( "fadd: new FCode `%s' is %lx\n", 
+                               printf("fadd: new FCode `%s' is %lx\n", 
                                              new->name, new->num);
                        new->l = new->r = NULL;
                        dict->l = new;
@@ -841,7 +840,7 @@
                        return fadd(dict->r, new);
                else {
                        if (debug > 5)
-                               (void)printf( "fadd: new FCode `%s' is %lx\n", 
+                               printf("fadd: new FCode `%s' is %lx\n", 
                                              new->name, new->num);
                        new->l = new->r = NULL;
                        dict->r = new;
@@ -862,7 +861,7 @@
        res = strcmp(dict->name, str);
        ASSERT(dict->type == FCODE);
        if (debug > 5)
-               (void)printf( "flookup: `%s' and `%s' %s match\n", 
+               printf("flookup: `%s' and `%s' %s match\n", 
                              str, dict->name, res?"don't":"do");
        if (!res) return (dict);
        if (res < 0)
@@ -895,7 +894,7 @@
                        new->l = new->r = NULL;
                        dict->l = new;
                        if (debug > 5)
-                               (void)printf( "aadd: new alias `%s' to `%s'\n", 
+                               printf("aadd: new alias `%s' to `%s'\n", 
                                              new->name, new->equiv);
                }
        } else {
@@ -905,7 +904,7 @@
                        new->l = new->r = NULL;
                        dict->r = new;
                        if (debug > 5)
-                               (void)printf( "aadd: new alias `%s' to `%s'\n", 
+                               printf("aadd: new alias `%s' to `%s'\n", 
                                              new->name, new->equiv);
                }
        }
@@ -948,8 +947,8 @@
 
        while ((++code)->name) {
                if(!fadd(dictionary, code)) {
-                       printf("init: duplicate dictionary entry %s\n", 
-                              code->name);
+                       warnx("%s: duplicate dictionary entry `%s'", __func__,
+                           code->name);
                }
        }
 
@@ -959,8 +958,8 @@
        alias->type = MACRO;
        while ((++alias)->name) {
                if(!aadd(aliases, alias)) {
-                       printf("init: duplicate macro entry %s\n", 
-                              alias->name);
+                       warnx("%s: duplicate macro entry `%s'", __func__,
+                           alias->name);
                }
        }
 
@@ -987,10 +986,11 @@
 }
 
 static void
-usage(const char *me)
+usage(void)
 {
-       (void)fprintf(stderr, "%s: [-d level] [-o outfile] <infile>\n", me);
-       exit(1);
+       (void)fprintf(stderr, "%s: [-d level] [-o outfile] <infile>\n",
+       getprogname());
+       exit(EXIT_FAILURE);
 }
 
 int
@@ -1004,7 +1004,6 @@
        int i;
 
        outf = 1; /* stdout */
-       myname = argv[0];
 
        while ((ch = getopt(argc, argv, "d:o:")) != -1)
                switch(ch) {
@@ -1016,13 +1015,13 @@
                        outfile = optarg;
                        break;
                default:
-                       usage(myname);
+                       usage();
                }
        argc -= optind;
        argv += optind;
        
        if (argc != 1)
-               usage(myname);
+               usage();
        
        infile = argv[0];
 
@@ -1031,8 +1030,8 @@
         */
        initdic();
        outbufsiz = BUFCLICK;
-       outbuf = malloc(outbufsiz);
-       fheader = (struct fcode_header *)outbuf;
+       fheader = emalloc(outbufsiz);
+       outbuf = (void *)fheader;
        outpos = 0;
        emit(hdrtype);
        outpos = sizeof(*fheader);
@@ -1041,9 +1040,9 @@
         * Do it.
         */
        if ((inf = fopen(infile, "r")) == NULL)
-               (void)err(1, "can not open %s for reading", infile);
+               err(EXIT_FAILURE, "Cannot open `%s'", infile);
 
-       inbuf = yy_create_buffer( inf, YY_BUF_SIZE );
+       inbuf = yy_create_buffer(inf, YY_BUF_SIZE);
        yy_switch_to_buffer(inbuf);
        tokenize(inbuf);
        yy_delete_buffer(inbuf);
@@ -1059,15 +1058,16 @@
        fheader->checksum = htons(fheader->checksum);
 
        if ((outf = open(outfile, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
-               err(1, "can out open %s for writing", outfile);
+               err(EXIT_FAILURE, "Cannot open `%s'", outfile);
 
        if (write(outf, outbuf, outpos) != outpos) {
+               int serrno = errno;
                close(outf);
                unlink(outfile);
-               err(1, "write error");
+               errc(EXIT_FAILURE, serrno, "write error");
        }
        close(outf);
-       return (0);
+       return EXIT_SUCCESS;
 };
 
 /*
@@ -1134,14 +1134,14 @@
                case TOK_STRING_LIT: 
                        STATE(token->text, "TOK_STRING_LIT:");
                {
-                       int len;
+                       size_t len;
                        char *p = token->text;
                        
                        ++p;                    /* Skip the quote */
                        len = strlen(++p);      /* Skip the 1st space */
 
 #define ERR_TOOLONG    \
-       token_err(yylineno, infile, yytext, "string length %d too long", len)
+       token_err(yylineno, infile, yytext, "string length %zu too long", len)
 
                        if (len > 255)
                                ERR_TOOLONG;
@@ -1157,7 +1157,7 @@
                case TOK_PSTRING: 
                        STATE(token->text, "TOK_PSTRING:");
                {
-                       int len;
+                       size_t len;
                        char *p = token->text;
 
                        if (*p++ == '.') p++; /* Skip over delimiter */
@@ -1179,7 +1179,7 @@
                case TOK_ABORT_S:
                        STATE(token->text, "TOK_PSTRING:");
                {
-                       int len;
+                       size_t len;
                        Cell value = -2;
                        char *p = token->text;
 
@@ -1235,19 +1235,19 @@
                                    "EOF in colon definition");
                        
                        /* Add new code to dictionary */
-                       fcode = malloc(sizeof(*fcode));
+                       fcode = emalloc(sizeof(*fcode));
                        fcode->num = nextfcode++;
-                       fcode->name = strdup(token->text);
+                       fcode->name = estrdup(token->text);
                        if (!fadd(dictionary, fcode)) {
                                /* Duplicate definition.  Free the memory. */
                                if (debug)
-                                       (void)printf("%s: duplicate FCode\n",
+                                       printf("%s: duplicate FCode\n",
                                                token->text);
                                free(__UNCONST(fcode->name));
                                free(fcode);
                        }
                        if (debug)
-                               (void)printf("Adding %s to dictionary\n", token->text);         
+                               printf("Adding %s to dictionary\n", token->text);               
                        if (state == 0)
                                emit("new-token");
                        else {
@@ -1290,21 +1290,23 @@
 
                        token = yylex();
                        if (token == NULL) {
-                               (void)printf( "EOF in alias definition\n");
+                               warnx("EOF in alias definition");
                                return;
                        }
                        if (token->type != TOK_OTHER) {
-                               (void)printf( "ENDCOMMENT aliasing weird token type %d\n",
-                                             token->type);
+                               warnx("ENDCOMMENT aliasing weird token type %d",
+                                   token->type);
                        }
-                       alias = malloc(sizeof(*alias));
-                       alias->name = strdup(token->text);
+                       alias = emalloc(sizeof(*alias));
+                       alias->name = estrdup(token->text);



Home | Main Index | Thread Index | Old Index