Skip to content

Commit

Permalink
use rcutils_format_string() rather than malloc and rcutils_snprintf() (
Browse files Browse the repository at this point in the history
  • Loading branch information
wjwwood authored and dhood committed Jun 16, 2018
1 parent 99c05cf commit 8f74452
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions rcl/test/rcl/test_rcl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,24 @@ class CLASSNAME (TestRCLFixture, RMW_IMPLEMENTATION) : public ::testing::Test
struct FakeTestArgv
{
FakeTestArgv()
: argc(2)
: allocator(rcutils_get_default_allocator()), argc(2)
{
this->argv = reinterpret_cast<char **>(malloc(2 * sizeof(char *)));
this->argv =
reinterpret_cast<char **>(allocator.allocate(2 * sizeof(char *), allocator.state));
if (!this->argv) {
throw std::bad_alloc();
}
static const size_t size = 10;
this->argv[0] = reinterpret_cast<char *>(malloc(size * sizeof(char)));
rcutils_snprintf(this->argv[0], size, "foo");
this->argv[1] = reinterpret_cast<char *>(malloc(size * sizeof(char)));
rcutils_snprintf(this->argv[1], size, "bar");
this->argv[0] = rcutils_format_string(allocator, "%s", "foo");
if (!this->argv[0]) {
allocator.deallocate(this->argv, allocator.state);
throw std::bad_alloc();
}
this->argv[1] = rcutils_format_string(allocator, "%s", "bar");
if (!this->argv[1]) {
allocator.deallocate(this->argv[0], allocator.state);
allocator.deallocate(this->argv, allocator.state);
throw std::bad_alloc();
}
}

~FakeTestArgv()
Expand All @@ -72,13 +79,14 @@ struct FakeTestArgv
if (this->argc > 0) {
size_t unsigned_argc = this->argc;
for (size_t i = 0; i < unsigned_argc; --i) {
free(this->argv[i]);
allocator.deallocate(this->argv[i], allocator.state);
}
}
}
free(this->argv);
allocator.deallocate(this->argv, allocator.state);
}

rcutils_allocator_t allocator;
int argc;
char ** argv;

Expand Down

0 comments on commit 8f74452

Please sign in to comment.