From a213747294118bb327bc642406f68341ee7f830e Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Sat, 6 Dec 2014 03:16:33 +0000 Subject: [PATCH] [GB.XML] * BUG: Parsing errors are now correctly thrown. * BUG: Fixed some memory leaks when an error is raised. git-svn-id: svn://localhost/gambas/trunk@6711 867c0c6c-44f3-4631-809d-bfa615b0a4ec --- gb.xml/src/CDocument.cpp | 4 +++ gb.xml/src/CElement.cpp | 2 ++ gb.xml/src/CExplorer.cpp | 1 + gb.xml/src/CReader.cpp | 1 + gb.xml/src/html/CHTMLDocument.cpp | 27 --------------- gb.xml/src/parser.cpp | 31 ++++++++++------- gb.xml/src/utils.cpp | 55 ++++++++++++++----------------- gb.xml/src/utils.h | 5 +-- 8 files changed, 55 insertions(+), 71 deletions(-) diff --git a/gb.xml/src/CDocument.cpp b/gb.xml/src/CDocument.cpp index 617c99161..341edd57b 100644 --- a/gb.xml/src/CDocument.cpp +++ b/gb.xml/src/CDocument.cpp @@ -76,6 +76,7 @@ catch(XMLParseException &e) { GB.Error(e.errorWhat); GB.ReturnNull(); + XMLParseException_Cleanup(&e); } END_METHOD @@ -95,6 +96,7 @@ try catch(XMLParseException &e) { GB.Error(e.errorWhat); + XMLParseException_Cleanup(&e); } END_METHOD @@ -129,6 +131,7 @@ else catch(XMLParseException &e) { GB.Error(e.errorWhat); + XMLParseException_Cleanup(&e); } } @@ -156,6 +159,7 @@ try catch(XMLParseException &e) { GB.Error(e.errorWhat); + XMLParseException_Cleanup(&e); } END_METHOD diff --git a/gb.xml/src/CElement.cpp b/gb.xml/src/CElement.cpp index d11d2c8bb..cb2b71fcf 100644 --- a/gb.xml/src/CElement.cpp +++ b/gb.xml/src/CElement.cpp @@ -140,6 +140,7 @@ try catch(XMLParseException &e) { GB.Error(e.errorWhat); + XMLParseException_Cleanup(&e); } END_METHOD @@ -254,6 +255,7 @@ try catch(XMLParseException &e) { GB.Error(e.errorWhat); + XMLParseException_Cleanup(&e); } GB.ReturnObject(array); diff --git a/gb.xml/src/CExplorer.cpp b/gb.xml/src/CExplorer.cpp index fcc8f1f43..cf0be41ab 100644 --- a/gb.xml/src/CExplorer.cpp +++ b/gb.xml/src/CExplorer.cpp @@ -88,6 +88,7 @@ try catch(XMLParseException &e) { GB.Error(e.errorWhat); + XMLParseException_Cleanup(&e); } diff --git a/gb.xml/src/CReader.cpp b/gb.xml/src/CReader.cpp index 0b963aa8a..72fe3c0f7 100644 --- a/gb.xml/src/CReader.cpp +++ b/gb.xml/src/CReader.cpp @@ -50,6 +50,7 @@ try catch(XMLParseException &e) { GB.Error(e.errorWhat); + XMLParseException_Cleanup(&e); } END_METHOD diff --git a/gb.xml/src/html/CHTMLDocument.cpp b/gb.xml/src/html/CHTMLDocument.cpp index 7a694ff98..2f9aed63d 100644 --- a/gb.xml/src/html/CHTMLDocument.cpp +++ b/gb.xml/src/html/CHTMLDocument.cpp @@ -184,33 +184,6 @@ HtmlDocument_AddScriptIfIE(THIS, STRING(path), LENGTH(path), STRINGOPT(cond, "IE END_METHOD -/*BEGIN_METHOD(CDocument_forceSetContent, GB_STRING data) - -try -{ - THIS->setContent(STRING(data), true); -} -catch(HTMLParseException &e) -{ - GB.Error(e.what()); -} - -END_METHOD*/ -/* -BEGIN_METHOD(CDocument_fromString, GB_STRING content) - -try -{ - (STRING(content), LENGTH(content)); -} -catch(XMLParseException &e) -{ - GB.Error(e.what()); -} - -END_METHOD*/ - - GB_DESC CDocumentStyleSheetsDesc[] = { GB_DECLARE(".HtmlDocumentStyleSheets", 0), GB_VIRTUAL_CLASS(), diff --git a/gb.xml/src/parser.cpp b/gb.xml/src/parser.cpp index 17d1476d9..4df85a92e 100644 --- a/gb.xml/src/parser.cpp +++ b/gb.xml/src/parser.cpp @@ -64,6 +64,13 @@ else \ XMLNode_appendChild(curElement, _elmt); \ } +#define THROW(_ex) for(size_t i = 0; i < *nodeCount; i++)\ +{\ + XMLNode_Free(elements[i]);\ +}\ +free(elements);\ +throw(_ex) + Node** parseXML(char const *data, const size_t lendata, size_t *nodeCount) throw(XMLParseException) { *nodeCount = 0; @@ -129,21 +136,21 @@ Node** parseXML(char const *data, const size_t lendata, size_t *nodeCount) throw if(!curElement)//Pas d'élément courant { //ERREUR : CLOSING TAG WHEREAS NONE IS OPEN - throw(XMLParseException_New("Closing tag whereas none is open", + THROW(XMLParseException_New("Closing tag whereas none is open", data, lendata, pos - 1)); } if((endData) < pos + curElement->lenTagName)//Impossible que les tags correspondent { //ERREUR : TAG MISMATCH - throw(XMLParseException_New("Tag mismatch", + THROW(XMLParseException_New("Tag mismatch", data, lendata, pos - 1)); } //Les tags ne correspondent pas else if(memcmp(pos, curElement->tagName, curElement->lenTagName) != 0) { //ERREUR : TAG MISMATCH - throw(XMLParseException_New("Tag mismatch", + THROW(XMLParseException_New("Tag mismatch", data, lendata, pos - 1)); } else//Les tags correspondent, on remonte @@ -165,7 +172,7 @@ Node** parseXML(char const *data, const size_t lendata, size_t *nodeCount) throw if(!tag)//Commentaire sans fin { //ERREUR : NEVER-ENDING COMMENT - throw(XMLParseException_New("Never-ending comment", + THROW(XMLParseException_New("Never-ending comment", data, lendata, pos - 1)); } @@ -182,7 +189,7 @@ Node** parseXML(char const *data, const size_t lendata, size_t *nodeCount) throw if(!tag)//Cdata sans fin { //ERREUR : UNENDED CDATA - throw(XMLParseException_New("Never-ending CDATA", + THROW(XMLParseException_New("Never-ending CDATA", data, lendata, pos - 1)); } @@ -198,7 +205,7 @@ Node** parseXML(char const *data, const size_t lendata, size_t *nodeCount) throw tag = (char*)memchr(pos, '>', endData - pos); if(!tag)//Doctype sans fin { - throw(XMLParseException_New("Never-ending DOCTYPE", + THROW(XMLParseException_New("Never-ending DOCTYPE", data, lendata, pos - 1)); } @@ -208,7 +215,7 @@ Node** parseXML(char const *data, const size_t lendata, size_t *nodeCount) throw else// ... ? { //ERREUR : INVALID TAG - throw(XMLParseException_New("Invalid Tag", + THROW(XMLParseException_New("Invalid Tag", data, lendata, pos - 1)); } } @@ -217,7 +224,7 @@ Node** parseXML(char const *data, const size_t lendata, size_t *nodeCount) throw tag = (char*)memchrs(pos, endData - pos, "?>", 2);//Looking for the end of the PI if(!tag)//Endless PI { - throw(XMLParseException_New("Never-ending Processing instruction", + THROW(XMLParseException_New("Never-ending Processing instruction", data, lendata, pos - 1)); } @@ -228,7 +235,7 @@ Node** parseXML(char const *data, const size_t lendata, size_t *nodeCount) throw else// ... ? { //ERREUR : INVALID TAG - throw(XMLParseException_New("Invalid Tag", + THROW(XMLParseException_New("Invalid Tag", data, lendata, pos - 1)); } }//Si tout va bien, on a un nouvel élément @@ -239,7 +246,7 @@ Node** parseXML(char const *data, const size_t lendata, size_t *nodeCount) throw if(pos > endData) { //ERREUR : NEVER-ENDING TAG - throw(XMLParseException_New("Never-ending tag", + THROW(XMLParseException_New("Never-ending tag", data, lendata, pos - 1)); } } @@ -282,7 +289,7 @@ Node** parseXML(char const *data, const size_t lendata, size_t *nodeCount) throw else { //ERREUR : INVALID TAG - throw(XMLParseException_New("Invalid tag", + THROW(XMLParseException_New("Invalid tag", data, lendata, pos - 1)); } } @@ -295,7 +302,7 @@ Node** parseXML(char const *data, const size_t lendata, size_t *nodeCount) throw if(delimiter != '"' && delimiter != '\'') { //ERREUR : EXPECTED ATTRIBUTE DELIMITER - throw(XMLParseException_New("Expected attribute delimiter", + THROW(XMLParseException_New("Expected attribute delimiter", data, lendata, pos - 1)); } pos++; diff --git a/gb.xml/src/utils.cpp b/gb.xml/src/utils.cpp index af29cf3c1..ca304ac81 100644 --- a/gb.xml/src/utils.cpp +++ b/gb.xml/src/utils.cpp @@ -287,18 +287,18 @@ void ThrowXMLParseException(const char* nerror, const char *text, const size_t l throw XMLParseException_New(nerror, text, lenText, posFailed); } -XMLParseException* XMLParseException_New()//Void initializer +XMLParseException XMLParseException_New()//Void initializer { - XMLParseException *exception = new XMLParseException; - memset(exception, 0, sizeof(XMLParseException)); - exception->line = 1; - exception->column = 1; + XMLParseException exception; + memset(&exception, 0, sizeof(XMLParseException)); + exception.line = 1; + exception.column = 1; return exception; } -XMLParseException* XMLParseException_New(const char *nerror, size_t posFailed) +XMLParseException XMLParseException_New(const char *nerror, size_t posFailed) { - XMLParseException *exception = XMLParseException_New(); + XMLParseException exception = XMLParseException_New(); size_t lenError; char *error; @@ -306,60 +306,55 @@ XMLParseException* XMLParseException_New(const char *nerror, size_t posFailed) error = (char*) malloc(lenError); memcpy(error, nerror, lenError); - exception->errorWhat = (char*)malloc(37 + lenError); - sprintf(exception->errorWhat, "Parse error : %s !\n Position %zu", error, (size_t)posFailed); - exception->errorWhat[36 + lenError] = 0; + exception.errorWhat = (char*)malloc(37 + lenError); + sprintf(exception.errorWhat, "Parse error : %s !\n Position %zu", error, (size_t)posFailed); + exception.errorWhat[36 + lenError] = 0; free(error); return exception; } -XMLParseException* XMLParseException_New(const char *nerror, const char *data, const size_t lenData, const char *posFailed) throw() +XMLParseException XMLParseException_New(const char *nerror, const char *data, const size_t lenData, const char *posFailed) throw() { - XMLParseException *exception = XMLParseException_New(); + XMLParseException exception = XMLParseException_New(); size_t lenError; - char *error; lenError = strlen(nerror) + 1; - error = (char*) malloc(lenError); - memcpy(error, nerror, lenError); //Parse error : (errorText) !\n Line 123456789 , Column 123456789 : \n (near) if(posFailed == 0) { - exception->errorWhat = (char*)malloc(17 + lenError); - sprintf(exception->errorWhat, "Parse error : %s !", error); - exception->errorWhat[16 + lenError] = 0; + exception.errorWhat = (char*)malloc(17 + lenError); + sprintf(exception.errorWhat, "Parse error : %s !", nerror); + exception.errorWhat[16 + lenError] = 0; return exception; } else if(!data || !lenData) { - exception->errorWhat = (char*)malloc(37 + lenError); - sprintf(exception->errorWhat, "Parse error : %s !\n Position %zu", error, (size_t)posFailed); - exception->errorWhat[36 + lenError] = 0; + exception.errorWhat = (char*)malloc(37 + lenError); + sprintf(exception.errorWhat, "Parse error : %s !\n Position %zu", nerror, (size_t)posFailed); + exception.errorWhat[36 + lenError] = 0; return exception; } if(posFailed > data + lenData || posFailed < data) return exception; - XMLParseException_AnalyzeText(exception, data, lenData, posFailed); + XMLParseException_AnalyzeText(&exception, data, lenData, posFailed); - exception->errorWhat = (char*)malloc(61 + lenError + exception->lenNear); - memset(exception->errorWhat, 0, 61 + lenError + exception->lenNear); - sprintf(exception->errorWhat, "Parse error : %s !\n Line %zu , Column %zu : \n %s", error, exception->line, exception->column, exception->near); - exception->errorWhat[60 + lenError + exception->lenNear] = 0; + exception.errorWhat = (char*)malloc(61 + lenError + exception.lenNear); + memset(exception.errorWhat, 0, 61 + lenError + exception.lenNear); + sprintf(exception.errorWhat, "Parse error : %s !\n Line %zu , Column %zu : \n %s", nerror, exception.line, exception.column, exception.near); + exception.errorWhat[60 + lenError + exception.lenNear] = 0; return exception; } - -void XMLParseException_Free(XMLParseException* &ex) throw() +void XMLParseException_Cleanup(XMLParseException *ex) { + if(ex->errorWhat) free(ex->errorWhat); if(ex->near) free(ex->near); - free(ex); - ex = 0; } void XMLParseException_AnalyzeText(XMLParseException *ex, const char *text, const size_t lenText, const char *posFailed) throw() diff --git a/gb.xml/src/utils.h b/gb.xml/src/utils.h index 45808f11a..48a08fca1 100644 --- a/gb.xml/src/utils.h +++ b/gb.xml/src/utils.h @@ -46,8 +46,9 @@ void insertString(char *&src, size_t &lenSrc, const char *insert, size_t lenInse bool GB_MatchString(const char *str, size_t lenStr, const char *pattern, size_t lenPattern, int mode = GB_STRCOMP_BINARY); -XMLParseException* XMLParseException_New(const char *nerror, size_t posFailed); -XMLParseException* XMLParseException_New(const char *nerror, const char *data, const size_t lenData, const char *posFailed) throw(); +XMLParseException XMLParseException_New(const char *nerror, size_t posFailed); +XMLParseException XMLParseException_New(const char *nerror, const char *data, const size_t lenData, const char *posFailed) throw(); +void XMLParseException_Cleanup(XMLParseException *ex); void ThrowXMLParseException(const char* nerror, const char *text, const size_t lenText, const char *posFailed);