From 4ad60fbae51ef4a0b4a4beaa5128d823063c158c Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Thu, 6 Aug 2015 10:46:28 -0700 Subject: [PATCH] init: replace strdup() in parse_config() Previously, the action, command, and service structs contained char*s that referenced memory within the buffer returned by the strdup() of the input buffer of parse_config. This prevented this entire memory region from being freed, leaking contents that would never be referenced again. The changes to convert the previous action, command, and service structs to C++ classes created explicit ownership of the contents within each class in the form of std::strings. With these changes, there are no remaining references to the memory allocated by this strdup(), which can now be freed. This commit replaces the strdup() with std::vector to allow for the copied string to be freed when it goes out of scope instead of relying on the C strdup() and free() functions. Change-Id: Id0a5f711e33363082ba201afda6b26043998cb1c --- init/init_parser.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/init/init_parser.cpp b/init/init_parser.cpp index 3ed1d585b..12f44f7cb 100644 --- a/init/init_parser.cpp +++ b/init/init_parser.cpp @@ -315,10 +315,14 @@ static void parse_config(const char *fn, const std::string& data) int nargs = 0; + //TODO: Use a parser with const input and remove this copy + std::vector data_copy(data.begin(), data.end()); + data_copy.push_back('\0'); + parse_state state; state.filename = fn; state.line = 0; - state.ptr = strdup(data.c_str()); // TODO: fix this code! + state.ptr = &data_copy[0]; state.nexttoken = 0; state.parse_line = parse_line_no_op;