Skip to content

Commit

Permalink
misc coverity & kw fixes and cleanups
Browse files Browse the repository at this point in the history
pick from ceph#29316

Change-Id: Ia0c42515d57fdc96cf1be376868b3c544113849f
Signed-off-by: songweibin <song.weibin@zte.com.cn>
  • Loading branch information
Songweibin committed Aug 2, 2019
1 parent f5d0db1 commit 6dc8c57
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/common/util.cc
Expand Up @@ -159,7 +159,7 @@ void collect_sys_info(map<string, string> *m, CephContext *cct)
break;
char key[40];
long long value;
int r = sscanf(line, "%s %lld", key, &value);
int r = sscanf(line, "%39s %lld", key, &value);
if (r == 2) {
if (strcmp(key, "MemTotal:") == 0)
(*m)["mem_total_kb"] = boost::lexical_cast<string>(value);
Expand Down
4 changes: 2 additions & 2 deletions src/crush/mapper.c
Expand Up @@ -775,11 +775,11 @@ static void crush_choose_indep(const struct crush_map *map,
out2, rep,
recurse_tries, 0,
0, NULL, r, choose_args);
if (out2[rep] == CRUSH_ITEM_NONE) {
if (out2 && out2[rep] == CRUSH_ITEM_NONE) {
/* placed nothing; no leaf */
break;
}
} else {
} else if (out2) {
/* we already have a leaf! */
out2[rep] = item;
}
Expand Down
2 changes: 1 addition & 1 deletion src/librbd/internal.cc
Expand Up @@ -185,7 +185,7 @@ bool compare_by_name(const child_info_t& c1, const child_info_t& c2)
info.num_objs = Striper::get_num_objects(ictx->layout, info.size);
info.order = obj_order;
strncpy(info.block_name_prefix, ictx->object_prefix.c_str(),
RBD_MAX_BLOCK_NAME_SIZE);
RBD_MAX_BLOCK_NAME_SIZE - 1);
info.block_name_prefix[RBD_MAX_BLOCK_NAME_SIZE - 1] = '\0';

// clear deprecated fields
Expand Down
4 changes: 4 additions & 0 deletions src/mount/mount.ceph.c
Expand Up @@ -40,6 +40,10 @@ static char *mount_resolve_src(const char *orig_str)
char *mount_path;
char *src;
char *buf = strdup(orig_str);
if (!buf) {
fprintf(stderr, "%s: failed to allocate memory\n", __func__);
return NULL;
}

mount_path = strstr(buf, ":/");
if (!mount_path) {
Expand Down
3 changes: 2 additions & 1 deletion src/os/filestore/BtrfsFileStoreBackend.cc
Expand Up @@ -466,7 +466,8 @@ int BtrfsFileStoreBackend::destroy_checkpoint(const string& name)
btrfs_ioctl_vol_args vol_args;
memset(&vol_args, 0, sizeof(vol_args));
vol_args.fd = 0;
strncpy(vol_args.name, name.c_str(), sizeof(vol_args.name));
strncpy(vol_args.name, name.c_str(), sizeof(vol_args.name) - 1);
vol_args.name[sizeof(vol_args.name) - 1] = '\0';

int ret = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_DESTROY, &vol_args);
if (ret) {
Expand Down
3 changes: 2 additions & 1 deletion src/osdc/Objecter.h
Expand Up @@ -533,7 +533,8 @@ struct ObjectOperation {
obj_watch_t ow;
ostringstream sa;
sa << i->addr;
strncpy(ow.addr, sa.str().c_str(), 256);
strncpy(ow.addr, sa.str().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;
Expand Down
3 changes: 2 additions & 1 deletion src/test/librados_test_stub/TestWatchNotify.cc
Expand Up @@ -47,7 +47,8 @@ int TestWatchNotify::list_watchers(const std::string& o,
watcher->watch_handles.begin();
it != watcher->watch_handles.end(); ++it) {
obj_watch_t obj;
strcpy(obj.addr, it->second.addr.c_str());
strncpy(obj.addr, it->second.addr.c_str(), sizeof(obj.addr) - 1);
obj.addr[sizeof(obj.addr) - 1] = '\0';
obj.watcher_id = static_cast<int64_t>(it->second.gid);
obj.cookie = it->second.handle;
obj.timeout_seconds = 30;
Expand Down
3 changes: 2 additions & 1 deletion src/test/librbd/managed_lock/test_mock_BreakRequest.cc
Expand Up @@ -81,7 +81,8 @@ class TestMockManagedLockBreakRequest : public TestMockFixture {
expect.WillOnce(Return(r));
} else {
obj_watch_t watcher;
strcpy(watcher.addr, (address + ":0/0").c_str());
strncpy(watcher.addr, (address + ":0/0").c_str(), sizeof(watcher.addr) - 1);
watcher.addr[sizeof(watcher.addr) - 1] = '\0';
watcher.cookie = watch_handle;

std::list<obj_watch_t> watchers;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/cephfs/Dumper.cc
Expand Up @@ -237,7 +237,7 @@ int Dumper::undump(const char *dump_file, bool force)
if (strstr(buf, "fsid")) {
uuid_d fsid;
char fsid_str[40];
sscanf(strstr(buf, "fsid"), "fsid %s", fsid_str);
sscanf(strstr(buf, "fsid"), "fsid %39s", fsid_str);
r = fsid.parse(fsid_str);
if (!r) {
derr << "Invalid fsid" << dendl;
Expand Down
10 changes: 5 additions & 5 deletions src/tools/scratchtool.c
Expand Up @@ -106,9 +106,12 @@ static int do_rados_getxattrs(rados_ioctx_t io_ctx, const char *oid,
static int testrados(void)
{
char tmp[32];
int i, r;
int i, r, safe;
int ret = 1; //set 1 as error case
rados_t cl;
const char *oid = "foo_object";
const char *exkeys[] = { "a", "b", "c", NULL };
const char *exvals[] = { "1", "2", "3", NULL };

if (rados_create(&cl, NULL) < 0) {
printf("error initializing\n");
Expand Down Expand Up @@ -217,7 +220,6 @@ static int testrados(void)
char buf[128], buf2[128];
time(&tm);
snprintf(buf, 128, "%s", ctime(&tm));
const char *oid = "foo_object";
r = rados_write(io_ctx, oid, buf, strlen(buf) + 1, 0);
printf("rados_write = %d\n", r);
r = rados_read(io_ctx, oid, buf2, sizeof(buf2), 0);
Expand All @@ -238,8 +240,6 @@ static int testrados(void)
goto out_err_cleanup;
if (do_rados_getxattr(io_ctx, oid, "c", "3"))
goto out_err_cleanup;
const char *exkeys[] = { "a", "b", "c", NULL };
const char *exvals[] = { "1", "2", "3", NULL };
if (do_rados_getxattrs(io_ctx, oid, exkeys, exvals))
goto out_err_cleanup;

Expand Down Expand Up @@ -275,7 +275,7 @@ static int testrados(void)
rados_completion_t c;
rados_aio_create_completion(0, 0, 0, &c);
rados_aio_write(io_ctx, "c", c, buf, 100, 0);
int safe = rados_aio_is_safe(c);
safe = rados_aio_is_safe(c);
printf("a should not yet be safe and ... %s\n", safe ? "is":"is not");
assert(!safe);
rados_aio_flush(io_ctx);
Expand Down

0 comments on commit 6dc8c57

Please sign in to comment.