From: Alex Zhang Date: Sun, 29 Sep 2019 09:33:58 +0000 (-0700) Subject: common: Fix multiple logical errors in get_device_id. X-Git-Tag: v14.2.10~213^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=0ba1e7c451d1f000f68e19a09a7300304b81d51d;p=ceph.git common: Fix multiple logical errors in get_device_id. 0. If blkdev.serial exists, the serial should be used. The original impl seems wrong (if serial does not exist, then use the value from the uninitialized buffer, or even worse, use the value from the last call (model)) 1. When using fallback methods, device id should only be returned when both model and serial are present. The original impl looks like a logical error. Signed-off-by: Difan Zhang (cherry picked from commit 3e09dfd8254abf3c4ff85d6c1b1fafc4b38eae7d) --- diff --git a/src/common/blkdev.cc b/src/common/blkdev.cc index ea6b5e8c396..c2f2e319f83 100644 --- a/src/common/blkdev.cc +++ b/src/common/blkdev.cc @@ -542,15 +542,22 @@ std::string get_device_id(const std::string& devname, if (!blkdev.model(buf, sizeof(buf))) { model = buf; } - if (blkdev.serial(buf, sizeof(buf))) { + if (!blkdev.serial(buf, sizeof(buf))) { serial = buf; } - if (!model.size() || serial.size()) { - if (err) { + if (err) { + if (model.empty() && serial.empty()) { + *err = std::string("fallback method has no model nor serial'"); + return {}; + } else if (model.empty()) { *err = std::string("fallback method has serial '") + serial - + "'but no model"; + + "' but no model'"; + return {}; + } else if (serial.empty()) { + *err = std::string("fallback method has model '") + model + + "' but no serial'"; + return {}; } - return {}; } device_id = model + "_" + serial;