diff --git a/src/client/nbd_proxy.cpp b/src/client/nbd_proxy.cpp index 26b6c6f9..27c58e92 100644 --- a/src/client/nbd_proxy.cpp +++ b/src/client/nbd_proxy.cpp @@ -412,12 +412,7 @@ public: } else if (pos == 1) { - char c = 0; - int n = 0; - if (sscanf(args[i], "/dev/nbd%d%c", &n, &c) == 1) - cfg["dev_num"] = n; - else - cfg["dev_num"] = args[i]; + cfg["dev_num"] = args[i]; pos++; } } @@ -436,11 +431,13 @@ public: } return -1; } - char c = 0; - int n = -1; - if (sscanf(cfg["dev_num"].as_string().c_str(), "%d%c", &n, &c) != 1 || n < 0) + std::string dev = cfg["dev_num"].as_string(); + std::string s = dev.substr(0, 8) == "/dev/nbd" ? dev.substr(8) : dev; + // The round trip refuses signs, spaces and overflows, which would name another device + int n = strtol(s.c_str(), NULL, 10); + if (n < 0 || std::to_string(n) != s) { - fprintf(stderr, "invalid device name: %s, expected /dev/nbdN\n", cfg["dev_num"].as_string().c_str()); + fprintf(stderr, "invalid device name: %s, expected /dev/nbdN\n", dev.c_str()); exit(1); } return n; diff --git a/tests/test_img_online_resize.sh b/tests/test_img_online_resize.sh index e096d673..fdeda373 100755 --- a/tests/test_img_online_resize.sh +++ b/tests/test_img_online_resize.sh @@ -9,6 +9,14 @@ if ! (mount | grep '/dev type devtmpfs'); then sudo bash -c 'mount udev /dev/ -t devtmpfs && mount devpts /dev/pts -t devpts' fi +# A sign, a space or a number that overflows int must not resolve to /dev/nbd0 +for NAME in /dev/nbd+0 "/dev/nbd 0" /dev/nbd4294967296; do + OUT=$(sudo build/src/client/vitastor-nbd unmap "$NAME" 2>&1 || true) + if [[ "$OUT" != *"invalid device name"* ]]; then + format_error "unmap '$NAME' did not fail on the device name: $OUT" + fi +done + NBD_DEV=$(sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG map --image testimg) trap "sudo build/src/client/vitastor-nbd unmap $NBD_DEV"' || true; kill -9 $(jobs -p)' EXIT if sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG netlink-map $NBD_DEV --image testimg; then