]> err.no Git - util-linux/commitdiff
mkswap: more robust strtoull() usage
authorKarel Zak <kzak@redhat.com>
Tue, 30 Mar 2010 12:10:08 +0000 (14:10 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 30 Mar 2010 12:45:37 +0000 (14:45 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/mkswap.c

index c199937523e2467c608782481af45f4f98ebf1bb..bc6c1d2916e649aaefaa98467395b71b182343cc 100644 (file)
@@ -559,13 +559,18 @@ main(int argc, char ** argv) {
                usage();
        }
        if (block_count) {
-               /* this silly user specified the number of blocks
-                  explicitly */
-               char *tmp;
-               int blocks_per_page = pagesize/1024;
-               PAGES = strtoull(block_count,&tmp,0)/blocks_per_page;
-               if (*tmp)
+               /* this silly user specified the number of blocks explicitly */
+               char *tmp = NULL;
+               long long blks;
+
+               errno = 0;
+               blks = strtoll(block_count, &tmp, 0);
+               if ((tmp && *tmp) ||
+                   (errno != 0 && (blks == ULLONG_MAX || blks == 0)) ||
+                   blks < 0)
                        usage();
+
+               PAGES = blks / (pagesize / 1024);
        }
        sz = get_size(device_name);
        if (!PAGES) {