Skip to content

Commit 8f74452

Browse files
wjwwooddhood
authored andcommitted
use rcutils_format_string() rather than malloc and rcutils_snprintf() (#240)
1 parent 99c05cf commit 8f74452

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

rcl/test/rcl/test_rcl.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,24 @@ class CLASSNAME (TestRCLFixture, RMW_IMPLEMENTATION) : public ::testing::Test
5353
struct FakeTestArgv
5454
{
5555
FakeTestArgv()
56-
: argc(2)
56+
: allocator(rcutils_get_default_allocator()), argc(2)
5757
{
58-
this->argv = reinterpret_cast<char **>(malloc(2 * sizeof(char *)));
58+
this->argv =
59+
reinterpret_cast<char **>(allocator.allocate(2 * sizeof(char *), allocator.state));
5960
if (!this->argv) {
6061
throw std::bad_alloc();
6162
}
62-
static const size_t size = 10;
63-
this->argv[0] = reinterpret_cast<char *>(malloc(size * sizeof(char)));
64-
rcutils_snprintf(this->argv[0], size, "foo");
65-
this->argv[1] = reinterpret_cast<char *>(malloc(size * sizeof(char)));
66-
rcutils_snprintf(this->argv[1], size, "bar");
63+
this->argv[0] = rcutils_format_string(allocator, "%s", "foo");
64+
if (!this->argv[0]) {
65+
allocator.deallocate(this->argv, allocator.state);
66+
throw std::bad_alloc();
67+
}
68+
this->argv[1] = rcutils_format_string(allocator, "%s", "bar");
69+
if (!this->argv[1]) {
70+
allocator.deallocate(this->argv[0], allocator.state);
71+
allocator.deallocate(this->argv, allocator.state);
72+
throw std::bad_alloc();
73+
}
6774
}
6875

6976
~FakeTestArgv()
@@ -72,13 +79,14 @@ struct FakeTestArgv
7279
if (this->argc > 0) {
7380
size_t unsigned_argc = this->argc;
7481
for (size_t i = 0; i < unsigned_argc; --i) {
75-
free(this->argv[i]);
82+
allocator.deallocate(this->argv[i], allocator.state);
7683
}
7784
}
7885
}
79-
free(this->argv);
86+
allocator.deallocate(this->argv, allocator.state);
8087
}
8188

89+
rcutils_allocator_t allocator;
8290
int argc;
8391
char ** argv;
8492

0 commit comments

Comments
 (0)