]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
swapon: fix discard option parsing
authorKarel Zak <kzak@redhat.com>
Wed, 21 Sep 2016 12:47:32 +0000 (14:47 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 21 Sep 2016 13:08:50 +0000 (15:08 +0200)
The current code does not work as expected if there is an option
behind the discard=<arg>, for example:

  swapon /dev/sdc -o discard=once,pri=10

ignores "once" the result is SWAP_FLAG_DISCARD; strace:

Old version:

  swapon("/dev/sdc", SWAP_FLAG_PREFER|SWAP_FLAG_DISCARD|10) = 0

Fixed version:

  swapon("/dev/sdc", SWAP_FLAG_PREFER|SWAP_FLAG_DISCARD|SWAP_FLAG_DISCARD_ONCE|10) = 0

Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/swapon.c

index 62a776d4018357b7b1c21506fe7e1be7d7ebe300..0ee5caf448904e8f7adc8b9578e3e536152cf3c3 100644 (file)
@@ -688,6 +688,7 @@ static int swapon_by_uuid(struct swapon_ctl *ctl, const char *uuid)
 static int parse_options(struct swap_prop *props, const char *options)
 {
        char *arg = NULL;
+       size_t argsz = 0;
 
        assert(props);
        assert(options);
@@ -695,16 +696,16 @@ static int parse_options(struct swap_prop *props, const char *options)
        if (mnt_optstr_get_option(options, "nofail", NULL, 0) == 0)
                props->no_fail = 1;
 
-       if (mnt_optstr_get_option(options, "discard", &arg, NULL) == 0) {
+       if (mnt_optstr_get_option(options, "discard", &arg, &argsz) == 0) {
                props->discard |= SWAP_FLAG_DISCARD;
 
                if (arg) {
                        /* only single-time discards are wanted */
-                       if (strcmp(arg, "once") == 0)
+                       if (strncmp(arg, "once", argsz) == 0)
                                props->discard |= SWAP_FLAG_DISCARD_ONCE;
 
                        /* do discard for every released swap page */
-                       if (strcmp(arg, "pages") == 0)
+                       if (strncmp(arg, "pages", argsz) == 0)
                                props->discard |= SWAP_FLAG_DISCARD_PAGES;
                }
        }