From: "Petteri Räty" <betelgeuse@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/libbash:master commit in: src/core/
Date: Thu, 9 Jun 2011 07:27:06 +0000 (UTC) [thread overview]
Message-ID: <96ad12582fcae25e1fc49d3848a77a27952df15a.betelgeuse@gentoo> (raw)
commit: 96ad12582fcae25e1fc49d3848a77a27952df15a
Author: Petteri Räty <petsku <AT> petteriraty <DOT> eu>
AuthorDate: Fri Jun 3 16:57:32 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Mon Jun 6 07:06:20 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=96ad1258
Core: fix memory leaks in bash_ast::init_parser
If init_parser threw an exception the already allocated memory wasn't
freed. Now we use our own wrapper antlr_pointer to make sure the memory
always gets freed.
---
src/core/bash_ast.cpp | 35 +++++++++++++----------------------
src/core/bash_ast.h | 24 ++++++++++++++----------
2 files changed, 27 insertions(+), 32 deletions(-)
diff --git a/src/core/bash_ast.cpp b/src/core/bash_ast.cpp
index 8f06311..b0ef4f8 100644
--- a/src/core/bash_ast.cpp
+++ b/src/core/bash_ast.cpp
@@ -52,47 +52,38 @@ bash_ast::bash_ast(const std::string& script_path,
init_parser(script, script_path);
}
-bash_ast::~bash_ast()
-{
- nodes->free(nodes);
- parser->free(parser);
- token_stream->free(token_stream);
- lexer->free(lexer);
- input->close(input);
-}
-
void bash_ast::init_parser(const std::string& script, const std::string& script_path)
{
- input = antlr3NewAsciiStringInPlaceStream(
+ input.reset(antlr3NewAsciiStringInPlaceStream(
reinterpret_cast<pANTLR3_UINT8>(const_cast<char*>(script.c_str())),
// We do not support strings longer than the max value of ANTLR3_UNIT32
boost::numeric_cast<ANTLR3_UINT32>(script.size()),
- NULL);
+ NULL));
- if(input == NULL)
+ if(!input)
throw interpreter_exception("Unable to open file " + script + " due to malloc() failure");
input->fileName = input->strFactory->newStr(
input->strFactory,
reinterpret_cast<pANTLR3_UINT8>(const_cast<char*>(script_path.c_str())));
- lexer = libbashLexerNew(input);
- if ( lexer == NULL )
+ lexer.reset(libbashLexerNew(input.get()));
+ if(!lexer)
throw interpreter_exception("Unable to create the lexer due to malloc() failure");
- token_stream = antlr3CommonTokenStreamSourceNew(
- ANTLR3_SIZE_HINT, lexer->pLexer->rec->state->tokSource);
- if (token_stream == NULL)
+ token_stream.reset(antlr3CommonTokenStreamSourceNew(
+ ANTLR3_SIZE_HINT, lexer->pLexer->rec->state->tokSource));
+ if(!token_stream)
throw interpreter_exception("Out of memory trying to allocate token stream");
- parser = libbashParserNew(token_stream);
- if (parser == NULL)
+ parser.reset(libbashParserNew(token_stream.get()));
+ if(!parser)
throw interpreter_exception("Out of memory trying to allocate parser");
- ast = parse(parser);
+ ast = parse(parser.get());
if(parser->pParser->rec->getNumberOfSyntaxErrors(parser->pParser->rec))
throw interpreter_exception("Something wrong happened while parsing");
- nodes = antlr3CommonTreeNodeStreamNewTree(ast, ANTLR3_SIZE_HINT);
+ nodes.reset(antlr3CommonTreeNodeStreamNewTree(ast, ANTLR3_SIZE_HINT));
}
std::string bash_ast::get_dot_graph()
@@ -127,7 +118,7 @@ std::string bash_ast::get_tokens(std::function<std::string(ANTLR3_UINT32)> token
// output line number for the first line
result << line_counter++ << "\t";
- pANTLR3_VECTOR token_list = token_stream->getTokens(token_stream);
+ pANTLR3_VECTOR token_list = token_stream->getTokens(token_stream.get());
unsigned token_size = token_list->size(token_list);
for(unsigned i = 0u; i != token_size; ++i)
diff --git a/src/core/bash_ast.h b/src/core/bash_ast.h
index d5d7f83..aa045bf 100644
--- a/src/core/bash_ast.h
+++ b/src/core/bash_ast.h
@@ -40,17 +40,25 @@ struct libbashLexer_Ctx_struct;
struct libbashParser_Ctx_struct;
class interpreter;
+template<typename T>
+class antlr_pointer: public std::unique_ptr<T, std::function<void(T*)>>
+{
+ typedef std::unique_ptr<T, std::function<void(T*)>> parent;
+public:
+ antlr_pointer(T* p = 0) : parent(p, [](T* to_delete) { to_delete->free(to_delete); }) {};
+};
+
/// \class bash_ast
/// \brief a wrapper class that helps interpret from istream and string
class bash_ast: public boost::noncopyable
{
- pANTLR3_INPUT_STREAM input;
+ antlr_pointer<ANTLR3_INPUT_STREAM_struct> input;
std::string script;
- libbashLexer_Ctx_struct* lexer;
- pANTLR3_COMMON_TOKEN_STREAM token_stream;
- libbashParser_Ctx_struct* parser;
+ antlr_pointer<libbashLexer_Ctx_struct> lexer;
+ antlr_pointer<ANTLR3_COMMON_TOKEN_STREAM_struct> token_stream;
+ antlr_pointer<libbashParser_Ctx_struct> parser;
pANTLR3_BASE_TREE ast;
- pANTLR3_COMMON_TREE_NODE_STREAM nodes;
+ antlr_pointer<ANTLR3_COMMON_TREE_NODE_STREAM_struct> nodes;
std::function<pANTLR3_BASE_TREE(libbashParser_Ctx_struct*)> parse;
void init_parser(const std::string& script, const std::string& script_path);
@@ -62,8 +70,6 @@ public:
bash_ast(const std::string& script_path,
std::function<pANTLR3_BASE_TREE(libbashParser_Ctx_struct*)> p=parser_start);
- ~bash_ast();
-
static void walker_start(plibbashWalker tree_parser);
static int walker_arithmetics(plibbashWalker tree_parser);
@@ -81,9 +87,7 @@ public:
interpret_with(interpreter& walker, Functor walk)
{
set_interpreter(&walker);
- std::unique_ptr<libbashWalker_Ctx_struct, std::function<void(plibbashWalker)>> p_tree_parser(
- libbashWalkerNew(nodes),
- [](plibbashWalker tree_parser) { tree_parser->free(tree_parser); });
+ antlr_pointer<libbashWalker_Ctx_struct> p_tree_parser(libbashWalkerNew(nodes.get()));
return walk(p_tree_parser.get());
}
next reply other threads:[~2011-06-09 7:27 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-06-09 7:27 Petteri Räty [this message]
-- strict thread matches above, loose matches on Subject: below --
2011-08-04 14:24 [gentoo-commits] proj/libbash:master commit in: src/core/ Petteri Räty
2011-07-08 14:03 Petteri Räty
2011-06-11 8:52 Petteri Räty
2011-06-11 8:24 Petteri Räty
2011-06-09 11:46 Petteri Räty
2011-06-03 14:48 Petteri Räty
2011-05-23 14:34 Petteri Räty
2011-05-06 10:29 Petteri Räty
2011-04-20 14:04 Petteri Räty
2011-04-08 11:12 Petteri Räty
2011-04-06 7:43 Petteri Räty
2011-04-02 15:50 Petteri Räty
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=96ad12582fcae25e1fc49d3848a77a27952df15a.betelgeuse@gentoo \
--to=betelgeuse@gentoo.org \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox