Skip to content

Commit

Permalink
test/objectstore/test_bluefs: refactor write_single_file()
Browse files Browse the repository at this point in the history
and avoid the false positive of coverity warning:

 Use after free (USE_AFTER_FREE)

Signed-off-by: Kefu Chai <kchai@redhat.com>
  • Loading branch information
tchaikov committed Sep 22, 2017
1 parent 38751c7 commit e51b4d1
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/test/objectstore/test_bluefs.cc
Expand Up @@ -11,6 +11,7 @@
#include "global/global_init.h"
#include "common/ceph_argparse.h"
#include "include/stringify.h"
#include "include/scope_guard.h"
#include "common/errno.h"
#include <gtest/gtest.h>

Expand Down Expand Up @@ -137,7 +138,6 @@ TEST(BlueFS, small_appends) {

void write_data(BlueFS &fs, uint64_t rationed_bytes)
{
BlueFS::FileWriter *h;
int j=0, r=0;
uint64_t written_bytes = 0;
rationed_bytes -= ALLOC_SIZE;
Expand All @@ -151,19 +151,20 @@ void write_data(BlueFS &fs, uint64_t rationed_bytes)
while (1) {
string file = "file.";
file.append(to_string(j));
BlueFS::FileWriter *h;
ASSERT_EQ(0, fs.open_for_write(dir, file, &h, false));
ASSERT_NE(nullptr, h);
auto sg = make_scope_guard([&fs, h] { fs.close_writer(h); });
bufferlist bl;
char *buf = gen_buffer(ALLOC_SIZE);
bufferptr bp = buffer::claim_char(ALLOC_SIZE, buf);
bl.push_back(bp);
h->append(bl.c_str(), bl.length());
r = fs.fsync(h);
if (r < 0) {
fs.close_writer(h);
break;
}
written_bytes += g_conf->bluefs_alloc_size;
fs.close_writer(h);
j++;
if ((rationed_bytes - written_bytes) <= g_conf->bluefs_alloc_size) {
break;
Expand All @@ -190,27 +191,26 @@ void create_single_file(BlueFS &fs)

void write_single_file(BlueFS &fs, uint64_t rationed_bytes)
{
BlueFS::FileWriter *h;
stringstream ss;
string dir = "dir.test";
string file = "testfile";
int r=0;
const string dir = "dir.test";
const string file = "testfile";
uint64_t written_bytes = 0;
rationed_bytes -= ALLOC_SIZE;
while (1) {
BlueFS::FileWriter *h;
ASSERT_EQ(0, fs.open_for_write(dir, file, &h, false));
ASSERT_NE(nullptr, h);
auto sg = make_scope_guard([&fs, h] { fs.close_writer(h); });
bufferlist bl;
char *buf = gen_buffer(ALLOC_SIZE);
bufferptr bp = buffer::claim_char(ALLOC_SIZE, buf);
bl.push_back(bp);
h->append(bl.c_str(), bl.length());
r = fs.fsync(h);
int r = fs.fsync(h);
if (r < 0) {
fs.close_writer(h);
break;
}
written_bytes += g_conf->bluefs_alloc_size;
fs.close_writer(h);
if ((rationed_bytes - written_bytes) <= g_conf->bluefs_alloc_size) {
break;
}
Expand Down Expand Up @@ -358,22 +358,23 @@ TEST(BlueFS, test_simple_compaction_sync) {
ASSERT_EQ(0, fs.mkfs(fsid));
ASSERT_EQ(0, fs.mount());
{
BlueFS::FileWriter *h;
for (int i=0; i<10; i++) {
string dir = "dir.";
dir.append(to_string(i));
ASSERT_EQ(0, fs.mkdir(dir));
for (int j=0; j<10; j++) {
string file = "file.";
file.append(to_string(j));
BlueFS::FileWriter *h;
ASSERT_EQ(0, fs.open_for_write(dir, file, &h, false));
ASSERT_NE(nullptr, h);
auto sg = make_scope_guard([&fs, h] { fs.close_writer(h); });
bufferlist bl;
char *buf = gen_buffer(4096);
bufferptr bp = buffer::claim_char(4096, buf);
bl.push_back(bp);
h->append(bl.c_str(), bl.length());
fs.fsync(h);
fs.close_writer(h);
}
}
}
Expand Down Expand Up @@ -410,22 +411,23 @@ TEST(BlueFS, test_simple_compaction_async) {
ASSERT_EQ(0, fs.mkfs(fsid));
ASSERT_EQ(0, fs.mount());
{
BlueFS::FileWriter *h;
for (int i=0; i<10; i++) {
string dir = "dir.";
dir.append(to_string(i));
ASSERT_EQ(0, fs.mkdir(dir));
for (int j=0; j<10; j++) {
string file = "file.";
file.append(to_string(j));
BlueFS::FileWriter *h;
ASSERT_EQ(0, fs.open_for_write(dir, file, &h, false));
ASSERT_NE(nullptr, h);
auto sg = make_scope_guard([&fs, h] { fs.close_writer(h); });
bufferlist bl;
char *buf = gen_buffer(4096);
bufferptr bp = buffer::claim_char(4096, buf);
bl.push_back(bp);
h->append(bl.c_str(), bl.length());
fs.fsync(h);
fs.close_writer(h);
}
}
}
Expand Down

0 comments on commit e51b4d1

Please sign in to comment.