From 8c216322b065119f5c4d643294a8fa5e7e7a2504 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 12 Jun 2014 10:14:43 +0200
Subject: [PATCH] fix segfaults in composers

---
 alib2/src/ExceptionToXMLComposer.cpp          |  6 ++++--
 alib2/src/ExceptionToXMLComposer.h            |  2 +-
 alib2/src/alphabet/SymbolToStringComposer.cpp |  4 ++--
 alib2/src/alphabet/SymbolToXMLComposer.cpp    | 10 +++-------
 alib2/src/alphabet/SymbolToXMLComposer.h      |  1 -
 alib2/src/string/StringToStringComposer.cpp   |  6 +++---
 6 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/alib2/src/ExceptionToXMLComposer.cpp b/alib2/src/ExceptionToXMLComposer.cpp
index 0fa7073ea1..d480a2b1b0 100644
--- a/alib2/src/ExceptionToXMLComposer.cpp
+++ b/alib2/src/ExceptionToXMLComposer.cpp
@@ -9,7 +9,9 @@
 
 namespace alib {
 
-void ExceptionToXMLComposer::Visit(std::list<sax::Token>& out, const AlibException& exception) {
+void ExceptionToXMLComposer::Visit(void* userData, const AlibException& exception) {
+	std::list<sax::Token> &out = *((std::list<sax::Token>*) userData);
+	
 	out.push_back(sax::Token("exception", sax::Token::START_ELEMENT));
 	out.push_back(sax::Token(exception.getCause(), sax::Token::CHARACTER));
 	out.push_back(sax::Token("exception", sax::Token::END_ELEMENT));
@@ -17,7 +19,7 @@ void ExceptionToXMLComposer::Visit(std::list<sax::Token>& out, const AlibExcepti
 
 std::list<sax::Token> ExceptionToXMLComposer::compose(const AlibException& exception) {
 	std::list<sax::Token> out;
-	Visit(out, exception);
+	Visit((void*) &out, exception);
 	return out;
 }
 
diff --git a/alib2/src/ExceptionToXMLComposer.h b/alib2/src/ExceptionToXMLComposer.h
index 9ebba83e6c..e262e3aab5 100644
--- a/alib2/src/ExceptionToXMLComposer.h
+++ b/alib2/src/ExceptionToXMLComposer.h
@@ -18,7 +18,7 @@ namespace alib {
  * This class contains methods to print XML representation of string to the output stream.
  */
 class ExceptionToXMLComposer {
-	static void Visit(std::list<sax::Token>&, const AlibException& exception);
+	static void Visit(void* userData, const AlibException& exception);
 public:
 	/**
 	 * Prints XML representation of AlibException to the output stream.
diff --git a/alib2/src/alphabet/SymbolToStringComposer.cpp b/alib2/src/alphabet/SymbolToStringComposer.cpp
index a7f9af4506..b69879e49d 100644
--- a/alib2/src/alphabet/SymbolToStringComposer.cpp
+++ b/alib2/src/alphabet/SymbolToStringComposer.cpp
@@ -32,7 +32,7 @@ void SymbolToStringComposer::Visit(void* userData, const LabeledSymbol& symbol)
 	std::stringstream &out = *((std::stringstream*) userData);
 
 	out << '\'';
-	Visit(out, symbol.getLabel());
+	Visit(userData, symbol.getLabel());
 	out << '\'';
 }
 
@@ -49,7 +49,7 @@ void SymbolToStringComposer::Visit(void* userData, const Symbol& symbol) {
 
 std::string SymbolToStringComposer::compose(const Symbol& symbol) {
 	std::stringstream out;
-	Visit(out, symbol);
+	Visit((void*) &out, symbol);
 	return std::move(out).str();
 }
 
diff --git a/alib2/src/alphabet/SymbolToXMLComposer.cpp b/alib2/src/alphabet/SymbolToXMLComposer.cpp
index 13ec4c900d..94c457be31 100644
--- a/alib2/src/alphabet/SymbolToXMLComposer.cpp
+++ b/alib2/src/alphabet/SymbolToXMLComposer.cpp
@@ -8,13 +8,9 @@
 #include "SymbolToXMLComposer.h"
 #include "LabeledSymbol.h"
 
-namespace alphabet {
-
-void SymbolToXMLComposer::Visit(void* userData, const label::Label& label) const {
-	std::list<sax::Token> &out = *((std::list<sax::Token>*) userData);
+#include "../ToXMLComposers.h"
 
-	out.push_back(sax::Token((std::string) label, sax::Token::CHARACTER));
-}
+namespace alphabet {
 
 void SymbolToXMLComposer::Visit(void* userData, const BlankSymbol&) const {
 	std::list<sax::Token> &out = *((std::list<sax::Token>*) userData);
@@ -27,7 +23,7 @@ void SymbolToXMLComposer::Visit(void* userData, const LabeledSymbol& symbol) con
 	std::list<sax::Token> &out = *((std::list<sax::Token>*) userData);
 
 	out.push_back(sax::Token("LabeledSymbol", sax::Token::START_ELEMENT));
-	Visit(userData, symbol.getLabel());
+	out.splice(out.end(), alib::ToXMLComposers::labelComposer.compose(symbol.getLabel()));
 	out.push_back(sax::Token("LabeledSymbol", sax::Token::END_ELEMENT));
 }
 
diff --git a/alib2/src/alphabet/SymbolToXMLComposer.h b/alib2/src/alphabet/SymbolToXMLComposer.h
index 75829d68c2..ab826bfd79 100644
--- a/alib2/src/alphabet/SymbolToXMLComposer.h
+++ b/alib2/src/alphabet/SymbolToXMLComposer.h
@@ -19,7 +19,6 @@ namespace alphabet {
  * This class contains methods to print XML representation of string to the output stream.
  */
 class SymbolToXMLComposer : public SymbolBase::const_visitor_type {
-	void Visit(void*, const label::Label& slabel) const;
 	void Visit(void*, const Symbol& symbol) const;
 	void Visit(void*, const LabeledSymbol& symbol) const;
 	void Visit(void*, const BlankSymbol& symbol) const;
diff --git a/alib2/src/string/StringToStringComposer.cpp b/alib2/src/string/StringToStringComposer.cpp
index e82c45516e..3f3f3af21f 100644
--- a/alib2/src/string/StringToStringComposer.cpp
+++ b/alib2/src/string/StringToStringComposer.cpp
@@ -46,7 +46,7 @@ void StringToStringComposer::Visit(void* userData, const CyclicString& string) {
 			else
 				out << ' ';
 
-			Visit(out, symbol);
+			Visit(userData, symbol);
 		}
 		out << ">";
 
@@ -69,7 +69,7 @@ void StringToStringComposer::Visit(void* userData, const LinearString& string) {
 			else
 				out << ' ';
 	
-			Visit(out, symbol);
+			Visit(userData, symbol);
 		}
 
 	}
@@ -88,7 +88,7 @@ void StringToStringComposer::Visit(void* userData, const String& string) {
 
 std::string StringToStringComposer::compose(const String& string) {
 	std::stringstream out;
-	Visit(out, string);
+	Visit((void*) &out, string);
 	return std::move(out).str();
 }
 
-- 
GitLab