From f88b20dccd0d499aa2c1427a9ca00fcdfe8b9143 Mon Sep 17 00:00:00 2001 From: alhazred Date: Fri, 14 Nov 2025 13:23:25 +1100 Subject: [PATCH] Fix memory allocator mismatch in error message handling Replace strdup() calls with sqlite3_mprintf() to ensure consistent memory management. Error messages allocated with strdup() (standard malloc) were being freed with sqlite3_free(), causing crashes. Fixes #2 --- src/cypher/cypher-parser.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cypher/cypher-parser.c b/src/cypher/cypher-parser.c index df90660..dc92f5b 100644 --- a/src/cypher/cypher-parser.c +++ b/src/cypher/cypher-parser.c @@ -169,19 +169,24 @@ static void parserSetError(CypherParser *pParser, CypherLexer *pLexer, const cha CypherAst *cypherParse(CypherParser *pParser, const char *zQuery, char **pzErrMsg) { if (!zQuery) { - if (pzErrMsg) *pzErrMsg = strdup("Query string is NULL"); + if (pzErrMsg) *pzErrMsg = sqlite3_mprintf("%s", "Query string is NULL"); return NULL; } CypherLexer *pLexer = cypherLexerCreate(zQuery); if (!pLexer) { - if (pzErrMsg) *pzErrMsg = strdup("Failed to create lexer"); + if (pzErrMsg) *pzErrMsg = sqlite3_mprintf("%s", "Failed to create lexer"); return NULL; } pParser->pAst = parseQuery(pLexer, pParser); - if (pParser->zErrorMsg) { - if (pzErrMsg) *pzErrMsg = strdup(pParser->zErrorMsg); + /* Only set error message if parsing failed (pAst is NULL) */ + if (!pParser->pAst && pParser->zErrorMsg) { + if (pzErrMsg) { + /* Copy error message using sqlite3_mprintf to ensure proper + * memory management compatibility with sqlite3_free */ + *pzErrMsg = sqlite3_mprintf("%s", pParser->zErrorMsg); + } } cypherLexerDestroy(pLexer);