-
Notifications
You must be signed in to change notification settings - Fork 286
Fix memory leaks in XML parser #8929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,12 @@ int yyxmlerror(xml_parsert &xml_parser, void *scanner, const std::string &error) | |||||||||
| // unreachable code | ||||||||||
| #pragma warning(disable:4702) | ||||||||||
| #endif | ||||||||||
|
|
||||||||||
| // Bison-generated yydestruct only handles symbols with %destructor; | ||||||||||
| // suppress the warning about unhandled enum values in that switch. | ||||||||||
| #if defined(__GNUC__) || defined(__clang__) | ||||||||||
| #pragma GCC diagnostic ignored "-Wswitch-enum" | ||||||||||
| #endif | ||||||||||
| %} | ||||||||||
|
|
||||||||||
| %parse-param {xml_parsert &xml_parser} | ||||||||||
|
|
@@ -33,10 +39,13 @@ int yyxmlerror(xml_parsert &xml_parser, void *scanner, const std::string &error) | |||||||||
| %union {char *s;} | ||||||||||
|
|
||||||||||
| %token STARTXMLDECL | ||||||||||
| %token VERSION STARTPI ENDPI EQ SLASH CLOSE END | ||||||||||
| %token <s> ENCODING NAME VALUE DATA COMMENT START | ||||||||||
| %token VERSION ENDPI EQ SLASH CLOSE END | ||||||||||
| %token <s> ENCODING NAME VALUE DATA COMMENT START STARTPI | ||||||||||
| %type <s> name_opt | ||||||||||
|
|
||||||||||
| // Memory management: ensure allocated string tokens are freed during error recovery | ||||||||||
| %destructor { free($$); } ENCODING NAME VALUE DATA COMMENT START STARTPI name_opt | ||||||||||
|
Comment on lines
+46
to
+47
|
||||||||||
| // Memory management: ensure allocated string tokens are freed during error recovery | |
| %destructor { free($$); } ENCODING NAME VALUE DATA COMMENT START STARTPI name_opt | |
| // Memory management is handled explicitly in semantic actions via free(...) | |
| // No %destructor is used for string-valued symbols to avoid double-free. |
Copilot
AI
Mar 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding %destructor free($$) for string-valued symbols means any semantic value freed manually while the symbol is still on the parse stack (e.g., the mid-rule action in element : START { ... free($1); } ...) risks double-free during error recovery. To avoid this, only free these values once the symbol is no longer on the stack (end-of-rule action), or set the semantic value to nullptr after freeing.
Copilot
AI
Mar 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the new %destructor that frees STARTPI/NAME on error recovery, freeing $1/$2 in this mid-rule action can cause a double-free if a parse error happens later in this PI (the tokens are still on the parse stack and will be destroyed during error recovery). Move these frees to an end-of-rule action (after ENDPI) or null out the semantic values after freeing so the destructor becomes a no-op.
| { free($1); free($2); xml_parser.stack.push_back(&xml_parser.parse_tree.xml); } | |
| { xml_parser.stack.push_back(&xml_parser.parse_tree.xml); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
#pragma GCC diagnostic ignored "-Wswitch-enum"is applied for the remainder of the generated parser translation unit. To avoid masking unrelated switch-enum warnings in this file, consider using#pragma GCC diagnostic pushbefore the ignore and a matching#pragma GCC diagnostic popin the epilogue (third%%section) to scope the suppression.