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>
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;