]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
osdc/Objecter: always add `\0' after strncpy()
authorKefu Chai <kchai@redhat.com>
Mon, 1 Apr 2019 03:09:28 +0000 (11:09 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 3 Apr 2019 10:56:49 +0000 (18:56 +0800)
strncpy() does not ensure that the dest str is nul terminated, so we
need to add the nul terminator here. another option is strlcpy(), but it
need to link against libbsd if glibc is used.

this change also silences warning like

In function ‘char* strncpy(char*, const char*, size_t)’,
    inlined from ‘virtual void
ObjectOperation::C_ObjectOperation_decodewatchers::finish(int)’ at
../src/osdc/Objecter.h:534:15:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:34: warning:
‘char* __builtin_strncpy(char*, const char*, long unsigned int)’
specified bound 256 equals destination size [-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos
(__dest));
      |
~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/osdc/Objecter.h

index 0ced9655c6216279247daa796cbc46e06e9f0b82..702743f567aadc7402e47c95f0f255c4d8db37c5 100644 (file)
@@ -537,7 +537,8 @@ struct ObjectOperation {
            for (auto i = resp.entries.begin(); i != resp.entries.end(); ++i) {
              obj_watch_t ow;
              std::string sa = i->addr.get_legacy_str();
-             strncpy(ow.addr, sa.c_str(), 256);
+             strncpy(ow.addr, sa.c_str(), sizeof(ow.addr) - 1);
+             ow.addr[sizeof(ow.addr) - 1] = '\0';
              ow.watcher_id = i->name.num();
              ow.cookie = i->cookie;
              ow.timeout_seconds = i->timeout_seconds;