]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common/pick_address.cc: silence GCC warning
authorKefu Chai <kchai@redhat.com>
Thu, 5 Dec 2019 07:34:12 +0000 (15:34 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 11 Dec 2019 02:04:16 +0000 (10:04 +0800)
* use `string_view` instead of string
* use `auto` instead of `int` to silence the warning
* use `enum class` instead of plain `enum`
* do not specify the value of enum, as
  we don't care about it

this change silences following warning

```
../src/common/pick_address.cc: In function ‘int get_iface_numa_node(const string&, int*)’:
../src/common/pick_address.cc:517:11: warning: comparison of integer expressions of different signedness: ‘int’ and ‘const size_type’ {aka ‘const long unsigned int’} [-Wsign-com\
pare]
  517 |   if (pos != string::npos ) {
      |       ~~~~^~~~~~~~~~~~~~~
```

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/CMakeLists.txt
src/common/pick_address.cc

index 83cb101da774a77018c737cd218967d78e9f2f45..8b363766b9076e17e0b7ff726b2463f2105e676a 100644 (file)
@@ -387,6 +387,7 @@ set(ceph_common_deps
   Boost::date_time
   Boost::iostreams
   StdFilesystem::filesystem
+  fmt::fmt
   ${BLKID_LIBRARIES}
   ${Backtrace_LIBRARIES}
   ${BLKIN_LIBRARIES}
index 7c38de659c84ef9fe8b8a38d55ff95cbb76ebf69..ec464c1b8e165119c20e8c227525456c2d2f125b 100644 (file)
  */
 
 #include "common/pick_address.h"
+
+#include <netdb.h>
+#include <string>
+#include <string.h>
+#include <vector>
+
+#include <fmt/format.h>
+
 #include "include/ipaddr.h"
 #include "include/str_list.h"
 #include "common/ceph_context.h"
 #include "common/errno.h"
 #include "common/numa.h"
 
-#include <netdb.h>
-#include <string>
-#include <string.h>
-#include <vector>
-
 #define dout_subsys ceph_subsys_
 
 const struct sockaddr *find_ip_in_subnet_list(
@@ -511,21 +514,23 @@ int get_iface_numa_node(
   const std::string& iface,
   int *node)
 {
-  enum { IFACE_PHY_PORT = 1, IFACE_BOND_PORT = 2} ifatype = IFACE_PHY_PORT;
-  string ifa = iface;
-  int pos = ifa.find(":");
-  if (pos != string::npos) {
-    ifa.erase(pos);
+  enum class iface_t {
+    PHY_PORT,
+    BOND_PORT
+  } ifatype = iface_t::PHY_PORT;
+  string_view ifa{iface};
+  if (auto pos = ifa.find(":"); pos != ifa.npos) {
+    ifa.remove_suffix(ifa.size() - pos);
   }
-  string fn = std::string("/sys/class/net/") + ifa + "/device/numa_node";
+  string fn = fmt::format("/sys/class/net/{}/device/numa_node", ifa);
   int fd = ::open(fn.c_str(), O_RDONLY);
   if (fd < 0) {
-    fn = std::string("/sys/class/net/") + ifa + "/bonding/slaves";
+    fn = fmt::format("/sys/class/net/{}/bonding/slaves", ifa);
     fd = ::open(fn.c_str(), O_RDONLY);
     if (fd < 0) {
       return -errno;
     }
-    ifatype = IFACE_BOND_PORT;
+    ifatype = iface_t::BOND_PORT;
   }
 
   int r = 0;
@@ -541,7 +546,7 @@ int get_iface_numa_node(
   }
 
   switch (ifatype) {
-  case IFACE_PHY_PORT:
+  case iface_t::PHY_PORT:
     *node = strtoll(buf, &endptr, 10);
     if (endptr != buf + strlen(buf)) {
       r = -EINVAL;
@@ -549,7 +554,7 @@ int get_iface_numa_node(
     }
     r = 0;
     break;
-  case IFACE_BOND_PORT:
+  case iface_t::BOND_PORT:
     int bond_node = -1;
     std::vector<std::string> sv;
     std::string ifacestr = buf;