Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/makefs - fix compilation isses



details:   https://anonhg.NetBSD.org/src/rev/db06af3f2233
branches:  trunk
changeset: 778900:db06af3f2233
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Apr 19 17:09:53 2012 +0000

description:
- fix compilation isses
- add some error checking
- avoid alloca
- add missing frees

diffstat:

 usr.sbin/makefs/chfs.c           |   1 +
 usr.sbin/makefs/chfs/chfs_mkfs.c |  68 +++++++++++++++++++++++++--------------
 usr.sbin/makefs/makefs.c         |   6 +-
 usr.sbin/makefs/makefs.h         |   4 +-
 4 files changed, 49 insertions(+), 30 deletions(-)

diffs (200 lines):

diff -r 53b17a2ffe38 -r db06af3f2233 usr.sbin/makefs/chfs.c
--- a/usr.sbin/makefs/chfs.c    Thu Apr 19 16:00:25 2012 +0000
+++ b/usr.sbin/makefs/chfs.c    Thu Apr 19 17:09:53 2012 +0000
@@ -98,6 +98,7 @@
        }
        *val++ = '\0';
 
+/*###101 [cc] error: passing argument 1 of 'set_option' discards qualifiers from pointer target type%%%*/
        retval = set_option(chfs_options, var, val);
        
 leave_chfs_parse_opts:
diff -r 53b17a2ffe38 -r db06af3f2233 usr.sbin/makefs/chfs/chfs_mkfs.c
--- a/usr.sbin/makefs/chfs/chfs_mkfs.c  Thu Apr 19 16:00:25 2012 +0000
+++ b/usr.sbin/makefs/chfs/chfs_mkfs.c  Thu Apr 19 17:09:53 2012 +0000
@@ -109,39 +109,49 @@
 void
 write_eb_header(fsinfo_t *fsopts)
 {
-       chfs_opt_t *chfs_opts;
+       chfs_opt_t *opts;
        struct chfs_eb_hdr ebhdr;
+       char *buf;
+
+       opts = fsopts->fs_specific;
 
-       chfs_opts = fsopts->fs_specific;
+#define MINSIZE MAX(MAX(CHFS_EB_EC_HDR_SIZE, CHFS_EB_HDR_NOR_SIZE), \
+    CHFS_EB_HDR_NAND_SIZE)
+       if (opts->pagesize < MINSIZE)
+               errx(EXIT_FAILURE, "pagesize cannot be less than %zu", MINSIZE);
+       if ((buf = malloc(opts->pagesize)) == NULL)
+               err(EXIT_FAILURE, "Memory allocation failed");
 
-       char buf[chfs_opts->pagesize];
-
-       memset(buf, 0xFF, sizeof(buf));
+       memset(buf, 0xFF, opts->pagesize);
 
        ebhdr.ec_hdr.magic = htole32(CHFS_MAGIC_BITMASK);
        ebhdr.ec_hdr.erase_cnt = htole32(1);
-       ebhdr.ec_hdr.crc_ec = htole32(crc32(0, (uint8_t *)&ebhdr.ec_hdr + 8, 4));
+       ebhdr.ec_hdr.crc_ec = htole32(crc32(0,
+           (uint8_t *)&ebhdr.ec_hdr + 8, 4));
 
-       memcpy(&buf, &ebhdr.ec_hdr, CHFS_EB_EC_HDR_SIZE);
+       memcpy(buf, &ebhdr.ec_hdr, CHFS_EB_EC_HDR_SIZE);
 
-       buf_write(fsopts, &buf, chfs_opts->pagesize);
+       buf_write(fsopts, buf, opts->pagesize);
 
-       memset(buf, 0xFF, chfs_opts->pagesize);
+       memset(buf, 0xFF, opts->pagesize);
 
-       if (chfs_opts->mediatype == TYPE_NAND) {
+       if (opts->mediatype == TYPE_NAND) {
                ebhdr.u.nand_hdr.lid = htole32(lebnumber++);
                ebhdr.u.nand_hdr.serial = htole64(++(max_serial));
                ebhdr.u.nand_hdr.crc = htole32(crc32(0,
-                   (uint8_t *)&ebhdr.u.nand_hdr + 4, CHFS_EB_HDR_NAND_SIZE - 4));
-               memcpy(&buf, &ebhdr.u.nand_hdr, CHFS_EB_HDR_NAND_SIZE);
+                   (uint8_t *)&ebhdr.u.nand_hdr + 4,
+                   CHFS_EB_HDR_NAND_SIZE - 4));
+               memcpy(buf, &ebhdr.u.nand_hdr, CHFS_EB_HDR_NAND_SIZE);
        } else {
                ebhdr.u.nor_hdr.lid = htole32(lebnumber++);
-               ebhdr.u.nor_hdr.crc = htole32(crc32(0, (uint8_t *)&ebhdr.u.nor_hdr + 4,
+               ebhdr.u.nor_hdr.crc = htole32(crc32(0,
+                   (uint8_t *)&ebhdr.u.nor_hdr + 4,
                    CHFS_EB_HDR_NOR_SIZE - 4));
-               memcpy(&buf, &ebhdr.u.nor_hdr, CHFS_EB_HDR_NOR_SIZE);
+               memcpy(buf, &ebhdr.u.nor_hdr, CHFS_EB_HDR_NOR_SIZE);
        }
        
-       buf_write(fsopts, &buf, chfs_opts->pagesize);
+       buf_write(fsopts, buf, opts->pagesize);
+       free(buf);
 }
 
 void
@@ -217,26 +227,29 @@
        int fd;
        ssize_t len;
        char *name = node->name;
-       chfs_opt_t *chfs_opts;
+       chfs_opt_t *opts;
        unsigned char *buf;
        uint32_t fileofs = 0;
 
-       chfs_opts = fsopts->fs_specific;
-       buf = malloc(chfs_opts->pagesize);
+       opts = fsopts->fs_specific;
+       buf = malloc(opts->pagesize);
 
-       if (buf == NULL) {
-               err(EXIT_FAILURE, "ERROR memory allocation failed");
-       }
+       if (buf == NULL)
+               goto out;
        
        if (node->type == S_IFREG || node->type == S_IFSOCK) {
                char *longname;
-               asprintf(&longname, "%s/%s", dir, name);
+               if (asprintf(&longname, "%s/%s", dir, name) == 1)
+                       goto out;
 
                fd = open(longname, O_RDONLY, 0444);
+               if (fd == -1)
+                       err(EXIT_FAILURE, "Cannot open `%s'", longname);
 
-               while ((len = read(fd, buf, chfs_opts->pagesize))) {
+               while ((len = read(fd, buf, opts->pagesize))) {
                        if (len < 0) {
                                warn("ERROR while reading %s", longname);
+                               free(longname);
                                free(buf);
                                close(fd);
                                return;
@@ -245,6 +258,7 @@
                        write_data(fsopts, node, buf, len, fileofs);
                        fileofs += len;
                }
+               free(longname);
                close(fd);      
        } else if (node->type == S_IFLNK) {
                len = strlen(node->symlink);
@@ -258,6 +272,9 @@
        }
 
        free(buf);
+       return;
+out:
+       err(EXIT_FAILURE, "Memory allocation failed");
 }
 
 void
@@ -265,8 +282,8 @@
     uint32_t ofs)
 {
        struct chfs_flash_data_node fdata;
+
        memset(&fdata, 0, sizeof(fdata));
-
        if (len == 0) {
                return;
        }
@@ -282,7 +299,8 @@
        fdata.data_length = htole32(len);
        fdata.offset = htole32(ofs);
        fdata.data_crc = htole32(crc32(0, (uint8_t *)buf, len));
-       fdata.node_crc = htole32(crc32(0, (uint8_t *)&fdata, sizeof(fdata) - 4));
+       fdata.node_crc = htole32(crc32(0,
+           (uint8_t *)&fdata, sizeof(fdata) - 4));
 
        buf_write(fsopts, &fdata, sizeof(fdata));
        buf_write(fsopts, buf, len);
diff -r 53b17a2ffe38 -r db06af3f2233 usr.sbin/makefs/makefs.c
--- a/usr.sbin/makefs/makefs.c  Thu Apr 19 16:00:25 2012 +0000
+++ b/usr.sbin/makefs/makefs.c  Thu Apr 19 17:09:53 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makefs.c,v 1.32 2012/04/19 15:36:06 ttoth Exp $        */
+/*     $NetBSD: makefs.c,v 1.33 2012/04/19 17:09:53 christos Exp $     */
 
 /*
  * Copyright (c) 2001-2003 Wasabi Systems, Inc.
@@ -41,7 +41,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(__lint)
-__RCSID("$NetBSD: makefs.c,v 1.32 2012/04/19 15:36:06 ttoth Exp $");
+__RCSID("$NetBSD: makefs.c,v 1.33 2012/04/19 17:09:53 christos Exp $");
 #endif /* !__lint */
 
 #include <assert.h>
@@ -296,7 +296,7 @@
 
 
 int
-set_option(option_t *options, const char *var, const char *val)
+set_option(const option_t *options, const char *var, const char *val)
 {
        int     i;
 
diff -r 53b17a2ffe38 -r db06af3f2233 usr.sbin/makefs/makefs.h
--- a/usr.sbin/makefs/makefs.h  Thu Apr 19 16:00:25 2012 +0000
+++ b/usr.sbin/makefs/makefs.h  Thu Apr 19 17:09:53 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makefs.h,v 1.25 2012/04/19 15:36:06 ttoth Exp $        */
+/*     $NetBSD: makefs.h,v 1.26 2012/04/19 17:09:53 christos Exp $     */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -158,7 +158,7 @@
 void           apply_specfile(const char *, const char *, fsnode *, int);
 void           dump_fsnodes(fsnode *);
 const char *   inode_type(mode_t);
-int            set_option(option_t *, const char *, const char *);
+int            set_option(const option_t *, const char *, const char *);
 fsnode *       walk_dir(const char *, const char *, fsnode *, fsnode *);
 void           free_fsnodes(fsnode *);
 



Home | Main Index | Thread Index | Old Index