Source-Changes-HG archive

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

[src/trunk]: src/sbin/gpt merge command line parsers and check all memory all...



details:   https://anonhg.NetBSD.org/src/rev/4d8f4bc7fb38
branches:  trunk
changeset: 341987:4d8f4bc7fb38
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Dec 01 23:29:07 2015 +0000

description:
merge command line parsers and check all memory allocations.

diffstat:

 sbin/gpt/add.c      |   20 ++------
 sbin/gpt/biosboot.c |   25 ++-------
 sbin/gpt/gpt.c      |  129 +++++++++++++++++++++++++++++++--------------------
 sbin/gpt/gpt.h      |    3 +
 sbin/gpt/gpt_uuid.c |   36 +++++++++----
 sbin/gpt/gpt_uuid.h |    3 +-
 sbin/gpt/label.c    |   45 +++++++++++------
 sbin/gpt/show.c     |    8 +--
 8 files changed, 149 insertions(+), 120 deletions(-)

diffs (truncated from 528 to 300 lines):

diff -r 21bb6480ce16 -r 4d8f4bc7fb38 sbin/gpt/add.c
--- a/sbin/gpt/add.c    Tue Dec 01 22:49:25 2015 +0000
+++ b/sbin/gpt/add.c    Tue Dec 01 23:29:07 2015 +0000
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: add.c,v 1.33 2015/12/01 19:25:24 christos Exp $");
+__RCSID("$NetBSD: add.c,v 1.34 2015/12/01 23:29:07 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -152,28 +152,19 @@
 cmd_add(gpt_t gpt, int argc, char *argv[])
 {
        int ch;
-       int64_t human_num;
 
        while ((ch = getopt(argc, argv, GPT_AIS "bl:t:")) != -1) {
                switch(ch) {
                case 'b':
-                       if (block > 0)
-                               return usage();
-                       if (dehumanize_number(optarg, &human_num) < 0)
-                               return usage();
-                       block = human_num;
-                       if (block < 1)
+                       if (gpt_human_get(&block) == -1)
                                return usage();
                        break;
                case 'l':
-                       if (name != NULL)
+                       if (gpt_name_get(gpt, &name) == -1)
                                return usage();
-                       name = (uint8_t *)strdup(optarg);
                        break;
                case 't':
-                       if (!gpt_uuid_is_nil(type))
-                               return usage();
-                       if (gpt_uuid_parse(optarg, type) != 0)
+                       if (gpt_uuid_get(gpt, &type) == -1)
                                return usage();
                        break;
                default:
@@ -188,9 +179,8 @@
                return usage();
 
        /* Create NetBSD FFS partitions by default. */
-       if (gpt_uuid_is_nil(type)) {
+       if (gpt_uuid_is_nil(type))
                gpt_uuid_create(GPT_TYPE_NETBSD_FFS, type, NULL, 0);
-       }
 
        if (optind != argc)
                return usage();
diff -r 21bb6480ce16 -r 4d8f4bc7fb38 sbin/gpt/biosboot.c
--- a/sbin/gpt/biosboot.c       Tue Dec 01 22:49:25 2015 +0000
+++ b/sbin/gpt/biosboot.c       Tue Dec 01 23:29:07 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: biosboot.c,v 1.17 2015/12/01 16:32:19 christos Exp $ */
+/*     $NetBSD: biosboot.c,v 1.18 2015/12/01 23:29:07 christos Exp $ */
 
 /*
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$NetBSD: biosboot.c,v 1.17 2015/12/01 16:32:19 christos Exp $");
+__RCSID("$NetBSD: biosboot.c,v 1.18 2015/12/01 23:29:07 christos Exp $");
 #endif
 
 #include <sys/stat.h>
@@ -252,34 +252,23 @@
 #ifdef DIOCGWEDGEINFO
        struct dkwedge_info dkw;
 #endif
-       char *dev, *p;
+       char *dev;
        int ch;
        gpt_t ngpt = gpt;
 
        while ((ch = getopt(argc, argv, "c:i:L:")) != -1) {
                switch(ch) {
                case 'c':
-                       if (bootpath != NULL)
-                               usage();
-                       if ((bootpath = strdup(optarg)) == NULL) {
-                               gpt_warn(gpt, "strdup failed");
-                               return -1;
-                       }
+                       if (gpt_name_get(gpt, &bootpath) == -1)
+                               return usage();
                        break;
                case 'i':
-                       if (entry > 0)
-                               usage();
-                       entry = strtoul(optarg, &p, 10);
-                       if (*p != 0 || entry < 1)
+                       if (gpt_entry_get(&entry) == -1)
                                return usage();
                        break;
                case 'L':
-                       if (label != NULL)
+                       if (gpt_name_get(gpt, &label) == -1)
                                return usage();
-                       if ((label = (uint8_t *)strdup(optarg)) == NULL) {
-                               gpt_warn(gpt, "strdup failed");
-                               return -1;
-                       }
                        break;
                default:
                        return usage();
diff -r 21bb6480ce16 -r 4d8f4bc7fb38 sbin/gpt/gpt.c
--- a/sbin/gpt/gpt.c    Tue Dec 01 22:49:25 2015 +0000
+++ b/sbin/gpt/gpt.c    Tue Dec 01 23:29:07 2015 +0000
@@ -35,7 +35,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: gpt.c,v 1.51 2015/12/01 19:25:24 christos Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.52 2015/12/01 23:29:07 christos Exp $");
 #endif
 
 #include <sys/param.h>
@@ -850,7 +850,8 @@
        hdr->hdr_lba_alt = htole64(last);
        hdr->hdr_lba_start = htole64(gpt->tbl->map_start + blocks);
        hdr->hdr_lba_end = htole64(last - blocks - 1LL);
-       gpt_uuid_generate(hdr->hdr_guid);
+       if (gpt_uuid_generate(gpt, hdr->hdr_guid) == -1)
+               return -1;
        hdr->hdr_lba_table = htole64(gpt->tbl->map_start);
        hdr->hdr_entries = htole32((blocks * gpt->secsz) /
            sizeof(struct gpt_ent));
@@ -860,7 +861,8 @@
 
        ent = gpt->tbl->map_data;
        for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
-               gpt_uuid_generate(ent[i].ent_guid);
+               if (gpt_uuid_generate(gpt, ent[i].ent_guid) == -1)
+                       return -1;
        }
 
        /*
@@ -895,12 +897,50 @@
        return last;
 }
 
+static int
+gpt_size_get(gpt_t gpt, off_t *size)
+{
+       off_t sectors;
+       int64_t human_num;
+       char *p;
+
+       if (*size > 0)
+               return -1;
+       sectors = strtoll(optarg, &p, 10);
+       if (sectors < 1)
+               return -1;
+       if (*p == '\0' || ((*p == 's' || *p == 'S') && p[1] == '\0')) {
+               *size = sectors * gpt->secsz;
+               return 0;
+       }
+       if ((*p == 'b' || *p == 'B') && p[1] == '\0') {
+               *size = sectors;
+               return 0;
+       }
+       if (dehumanize_number(optarg, &human_num) < 0)
+               return -1;
+       *size = human_num;
+       return 0;
+}
+
+int
+gpt_human_get(off_t *human)
+{
+       int64_t human_num;
+
+       if (*human > 0)
+               return -1;
+       if (dehumanize_number(optarg, &human_num) < 0)
+               return -1;
+       *human = human_num;
+       if (*human < 1)
+               return -1;
+       return 0;
+}
+
 int
 gpt_add_find(gpt_t gpt, struct gpt_find *find, int ch) 
 {
-       int64_t human_num;
-       char *p;
-
        switch (ch) {
        case 'a':
                if (find->all > 0)
@@ -908,31 +948,19 @@
                find->all = 1;
                break;
        case 'b':
-               if (find->block > 0)
-                       return -1;
-               if (dehumanize_number(optarg, &human_num) < 0)
-                       return -1;
-               find->block = human_num;
-               if (find->block < 1)
+               if (gpt_human_get(&find->block) == -1)
                        return -1;
                break;
        case 'i':
-               if (find->entry > 0)
-                       return -1;
-               find->entry = strtoul(optarg, &p, 10);
-               if (*p != 0 || find->entry < 1)
+               if (gpt_entry_get(&find->entry) == -1)
                        return -1;
                break;
        case 'L':
-               if (find->label != NULL)
+               if (gpt_name_get(gpt, &find->label) == -1)
                        return -1;
-               find->label = (uint8_t *)strdup(optarg);
                break;
        case 's':
-               if (find->size > 0)
-                       return -1;
-               find->size = strtoll(optarg, &p, 10);
-               if (*p != 0 || find->size < 1)
+               if (gpt_size_get(gpt, &find->size) == -1)
                        return -1;
                break;
        case 't':
@@ -1008,44 +1036,18 @@
 int
 gpt_add_ais(gpt_t gpt, off_t *alignment, u_int *entry, off_t *size, int ch)
 {
-       int64_t human_num;
-       off_t sectors;
-       char *p;
-
        switch (ch) {
        case 'a':
-               if (*alignment > 0)
-                       return -1;
-               if (dehumanize_number(optarg, &human_num) < 0)
-                       return -1;
-               *alignment = human_num;
-               if (*alignment < 1)
+               if (gpt_human_get(alignment) == -1)
                        return -1;
                return 0;
        case 'i':
-               if (*entry > 0)
-                       return -1;
-               *entry = strtoul(optarg, &p, 10);
-               if (*p != 0 || *entry < 1)
+               if (gpt_entry_get(entry) == -1)
                        return -1;
                return 0;
        case 's':
-               if (*size > 0)
-                       return -1;
-               sectors = strtoll(optarg, &p, 10);
-               if (sectors < 1)
+               if (gpt_size_get(gpt, size) == -1)
                        return -1;
-               if (*p == '\0' || ((*p == 's' || *p == 'S') && p[1] == '\0')) {
-                       *size = sectors * gpt->secsz;
-                       return 0;
-               }
-               if ((*p == 'b' || *p == 'B') && p[1] == '\0') {
-                       *size = sectors;
-                       return 0;
-               }
-               if (dehumanize_number(optarg, &human_num) < 0)
-                       return -1;
-               *size = human_num;
                return 0;
        default:
                return -1;
@@ -1142,3 +1144,28 @@
                return -1;
        return 0;
 }
+int
+gpt_uuid_get(gpt_t gpt, gpt_uuid_t *uuid)
+{
+       if (!gpt_uuid_is_nil(*uuid))
+               return -1;
+       if (gpt_uuid_parse(optarg, *uuid) != 0) {
+               gpt_warn(gpt, "Can't parse uuid");
+               return -1;
+       }
+       return 0;
+}
+
+int



Home | Main Index | Thread Index | Old Index