]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
Fix small memory leak
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index e92be4fe608a214f4be96f6013e97f657b7ecc58..e68d55f3c5cf74f53fc836c29f1ad15deb72ea7f 100644 (file)
--- a/util.c
+++ b/util.c
@@ -146,16 +146,16 @@ int get_linux_version()
 {
        struct utsname name;
        char *cp;
-       int a,b,c;
+       int a = 0, b = 0,c = 0;
        if (uname(&name) <0)
                return -1;
 
        cp = name.release;
        a = strtoul(cp, &cp, 10);
-       if (*cp != '.') return -1;
-       b = strtoul(cp+1, &cp, 10);
-       if (*cp != '.') return -1;
-       c = strtoul(cp+1, NULL, 10);
+       if (*cp == '.')
+               b = strtoul(cp+1, &cp, 10);
+       if (*cp == '.')
+               c = strtoul(cp+1, &cp, 10);
 
        return (a*1000000)+(b*1000)+c;
 }
@@ -535,6 +535,7 @@ int check_raid(int fd, char *name)
        struct supertype *st = guess_super(fd);
 
        if (!st) return 0;
+       st->ignore_hw_compat = 1;
        st->ss->load_super(st, fd, name);
        /* Looks like a raid array .. */
        fprintf(stderr, Name ": %s appears to be part of a raid array:\n",
@@ -1119,9 +1120,8 @@ int must_be_container(int fd)
 static int get_gpt_last_partition_end(int fd, unsigned long long *endofpart)
 {
        struct GPT gpt;
-       unsigned char buf[512];
        unsigned char empty_gpt_entry[16]= {0};
-       struct GPT_part_entry *part;
+       struct GPT_part_entry part;
        unsigned long long curr_part_end;
        unsigned all_partitions, entry_size;
        unsigned part_nr;
@@ -1129,8 +1129,9 @@ static int get_gpt_last_partition_end(int fd, unsigned long long *endofpart)
        *endofpart = 0;
 
        BUILD_BUG_ON(sizeof(gpt) != 512);
-       /* read GPT header */
+       /* skip protective MBR */
        lseek(fd, 512, SEEK_SET);
+       /* read GPT header */
        if (read(fd, &gpt, 512) != 512)
                return 0;
 
@@ -1147,28 +1148,19 @@ static int get_gpt_last_partition_end(int fd, unsigned long long *endofpart)
            entry_size > 512)
                return -1;
 
-       /* read first GPT partition entries */
-       if (read(fd, buf, 512) != 512)
-               return 0;
-
-       part = (struct GPT_part_entry*)buf;
-
        for (part_nr=0; part_nr < all_partitions; part_nr++) {
+               /* read partition entry */
+               if (read(fd, &part, entry_size) != (ssize_t)entry_size)
+                       return 0;
+
                /* is this valid partition? */
-               if (memcmp(part->type_guid, empty_gpt_entry, 16) != 0) {
+               if (memcmp(part.type_guid, empty_gpt_entry, 16) != 0) {
                        /* check the last lba for the current partition */
-                       curr_part_end = __le64_to_cpu(part->ending_lba);
+                       curr_part_end = __le64_to_cpu(part.ending_lba);
                        if (curr_part_end > *endofpart)
                                *endofpart = curr_part_end;
                }
 
-               part = (struct GPT_part_entry*)((unsigned char*)part + entry_size);
-
-               if ((unsigned char *)part >= buf + 512) {
-                       if (read(fd, buf, 512) != 512)
-                               return 0;
-                       part = (struct GPT_part_entry*)buf;
-               }
        }
        return 1;
 }
@@ -1378,7 +1370,7 @@ int open_subarray(char *dev, char *subarray, struct supertype *st, int quiet)
                if (!quiet)
                        fprintf(stderr, Name ": Couldn't open %s, aborting\n",
                                dev);
-               return 2;
+               return -1;
        }
 
        st->devnum = fd2devnum(fd);
@@ -1581,7 +1573,7 @@ int mdmon_running(int devnum)
 
 int start_mdmon(int devnum)
 {
-       int i;
+       int i, skipped;
        int len;
        pid_t pid;      
        int status;
@@ -1612,8 +1604,13 @@ int start_mdmon(int devnum)
        switch(fork()) {
        case 0:
                /* FIXME yuk. CLOSE_EXEC?? */
-               for (i=3; i < 100; i++)
-                       close(i);
+               skipped = 0;
+               for (i=3; skipped < 20; i++)
+                       if (close(i) < 0)
+                               skipped++;
+                       else
+                               skipped = 0;
+
                for (i=0; paths[i]; i++)
                        if (paths[i][0])
                                execl(paths[i], "mdmon",