use rcutils_format_string() rather than malloc and rcutils_snprintf() (#240)

This commit is contained in:
William Woodall 2018-06-16 13:47:51 -07:00 committed by dhood
parent 99c05cf3b4
commit 8f744520bd

View file

@ -53,17 +53,24 @@ public:
struct FakeTestArgv struct FakeTestArgv
{ {
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) { if (!this->argv) {
throw std::bad_alloc(); throw std::bad_alloc();
} }
static const size_t size = 10; this->argv[0] = rcutils_format_string(allocator, "%s", "foo");
this->argv[0] = reinterpret_cast<char *>(malloc(size * sizeof(char))); if (!this->argv[0]) {
rcutils_snprintf(this->argv[0], size, "foo"); allocator.deallocate(this->argv, allocator.state);
this->argv[1] = reinterpret_cast<char *>(malloc(size * sizeof(char))); throw std::bad_alloc();
rcutils_snprintf(this->argv[1], size, "bar"); }
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() ~FakeTestArgv()
@ -72,13 +79,14 @@ struct FakeTestArgv
if (this->argc > 0) { if (this->argc > 0) {
size_t unsigned_argc = this->argc; size_t unsigned_argc = this->argc;
for (size_t i = 0; i < unsigned_argc; --i) { 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; int argc;
char ** argv; char ** argv;