Skip to content

Fix %clear infinite loop and enable recursive macro support#230

Open
magicelk235 wants to merge 1 commit into
netwide-assembler:masterfrom
magicelk235:fix/preproc-clear-and-rmacro
Open

Fix %clear infinite loop and enable recursive macro support#230
magicelk235 wants to merge 1 commit into
netwide-assembler:masterfrom
magicelk235:fix/preproc-clear-and-rmacro

Conversation

@magicelk235
Copy link
Copy Markdown

What's this about?

I ran into an issue where %clear context define would hang the preprocessor indefinitely. After digging into it, I found a simple bug in the token parsing loop - it never moved to the next token, so it just kept processing the same one forever.

While I was in there, I also noticed the recursive macro (rmacro) support was commented out with #if 0. I've re-enabled it and fixed up the reference counting so macros can properly recurse without leaking memory or crashing.

Changes

%clear fix:

  • Added t = t->next at the end of the option parsing loop
  • Changed error handling to break instead of setting t = NULL (which would crash on the next iteration)

Recursive macro support:

  • Restored MMacroInvocation struct that saves/restores macro state between recursive calls
  • Re-enabled push_mmacro() to save state before recursing
  • Added pop_rmacro() to restore state when unwinding
  • Added unref_mmacro() helper for cleaner reference count management
  • Fixed the macro exit path to properly clean up at each recursion level

Testing

Verified %clear context define no longer hangs and error messages for invalid options still work correctly.

Copilot AI review requested due to automatic review settings May 8, 2026 16:45
@magicelk235 magicelk235 force-pushed the fix/preproc-clear-and-rmacro branch from c37f9c2 to 52ff78e Compare May 8, 2026 16:47
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates NASM’s preprocessor (asm/preproc.c) to (1) fix a hang when parsing %clear options (notably %clear context define), and (2) re-enable recursive multi-line macro (%rmacro) support by restoring the saved-invocation mechanism and adjusting cleanup/reference handling.

Changes:

  • Fix %clear option parsing loop to advance tokens and avoid the previous infinite loop.
  • Reintroduce recursive mmacro invocation save/restore (MMacroInvocation, push_mmacro(), pop_rmacro()).
  • Adjust recursive macro unwind logic and reference counting helpers to support recursion.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread asm/preproc.c
Comment thread asm/preproc.c Outdated
Comment on lines +8794 to +8798
if (fm->in_progress == 0) {
/* Only do full cleanup when all recursion levels done */
if (fm->nolist & NL_LINE) {
istk->noline--;
} else if (!istk->noline) {
Comment thread asm/preproc.c
Comment on lines 6962 to +6966
i->prev = m->prev;
i->params = m->params;
i->iline = m->iline;
i->iname = m->iname;
i->mstk = m->mstk;
@magicelk235 magicelk235 force-pushed the fix/preproc-clear-and-rmacro branch from d047a5c to 505a802 Compare May 8, 2026 16:58
…pport

Fix infinite loop in %clear directive parsing. When parsing options like
"%clear context define", the while loop never advanced to the next token,
causing the preprocessor to hang indefinitely on the same token.

Also re-enable the previously disabled recursive macro (rmacro) support:
- Restore MMacroInvocation struct for saving/restoring macro state
- Re-enable push_mmacro() to save invocation state before recursion
- Add pop_rmacro() to properly restore state after recursive expansion
- Add unref_mmacro() helper for reference counting during recursion
- Fix macro exit handling to properly clean up at each recursion level
@magicelk235 magicelk235 force-pushed the fix/preproc-clear-and-rmacro branch from 505a802 to 6394ef1 Compare May 8, 2026 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants