From ad6543e4e7a35dcbe1e140290e441c66bf5e50e2 Mon Sep 17 00:00:00 2001 From: nealsid Date: Wed, 22 Apr 2009 00:55:31 +0000 Subject: [PATCH] Fix memory leak when using the basic source line resolver, plus the optimization to load using in-memory buffers. Moved from manually allocating/deallocating memory to using a scoped_array A=nealsid R=tiger feng git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@329 4c0a9323-5329-0410-9bdc-e9ce6186880e --- src/processor/basic_source_line_resolver.cc | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/processor/basic_source_line_resolver.cc b/src/processor/basic_source_line_resolver.cc index bba86599..fe04439e 100644 --- a/src/processor/basic_source_line_resolver.cc +++ b/src/processor/basic_source_line_resolver.cc @@ -278,20 +278,28 @@ bool BasicSourceLineResolver::Module::LoadMapFromBuffer( // have to copy because modifying the result of string::c_str is not // permitted size_t map_buffer_length = strlen(map_buffer_c_str); - char *map_buffer_chars = new char[map_buffer_length]; + + // If the length is 0, we can still pretend we have a symbol file. This is + // for scenarios that want to test symbol lookup, but don't necessarily care if + // certain modules do not have any information, like system libraries. + if (map_buffer_length == 0) { + return true; + } + + scoped_array map_buffer_chars(new char[map_buffer_length]); if (map_buffer_chars == NULL) { BPLOG(ERROR) << "Memory allocation of " << map_buffer_length << " bytes failed"; return false; } - strncpy(map_buffer_chars, map_buffer_c_str, map_buffer_length); + strncpy(map_buffer_chars.get(), map_buffer_c_str, map_buffer_length); if (map_buffer_chars[map_buffer_length - 1] == '\n') { map_buffer_chars[map_buffer_length - 1] = '\0'; } char *buffer; - buffer = strtok_r(map_buffer_chars, "\r\n", &save_ptr); + buffer = strtok_r(map_buffer_chars.get(), "\r\n", &save_ptr); while (buffer != NULL) { ++line_number; @@ -300,14 +308,12 @@ bool BasicSourceLineResolver::Module::LoadMapFromBuffer( if (!ParseFile(buffer)) { BPLOG(ERROR) << "ParseFile on buffer failed at " << ":" << line_number; - delete [] map_buffer_chars; return false; } } else if (strncmp(buffer, "STACK ", 6) == 0) { if (!ParseStackInfo(buffer)) { BPLOG(ERROR) << "ParseStackInfo failed at " << ":" << line_number; - delete [] map_buffer_chars; return false; } } else if (strncmp(buffer, "FUNC ", 5) == 0) { @@ -315,7 +321,6 @@ bool BasicSourceLineResolver::Module::LoadMapFromBuffer( if (!cur_func.get()) { BPLOG(ERROR) << "ParseFunction failed at " << ":" << line_number; - delete [] map_buffer_chars; return false; } // StoreRange will fail if the function has an invalid address or size. @@ -329,7 +334,6 @@ bool BasicSourceLineResolver::Module::LoadMapFromBuffer( if (!ParsePublicSymbol(buffer)) { BPLOG(ERROR) << "ParsePublicSymbol failed at " << ":" << line_number; - delete [] map_buffer_chars; return false; } } else if (strncmp(buffer, "MODULE ", 7) == 0) { @@ -343,14 +347,12 @@ bool BasicSourceLineResolver::Module::LoadMapFromBuffer( if (!cur_func.get()) { BPLOG(ERROR) << "Found source line data without a function at " << ":" << line_number; - delete [] map_buffer_chars; return false; } Line *line = ParseLine(buffer); if (!line) { BPLOG(ERROR) << "ParseLine failed at " << line_number << " for " << buffer; - delete [] map_buffer_chars; return false; } cur_func->lines.StoreRange(line->address, line->size,