Skip to content

Free the RE2 object of a pattern Compile refuses - #293

Closed
nitzan-b-sp wants to merge 1 commit into
wasilibs:mainfrom
nitzan-b-sp:fix-compile-leak-on-error
Closed

nitzan-b-sp wants to merge 1 commit into
wasilibs:mainfrom
nitzan-b-sp:fix-compile-leak-on-error

Conversation

@nitzan-b-sp

Copy link
Copy Markdown

Refs #292

Problem

Compile creates the RE2 object, reads its error code, and on a refused pattern returns the error without deleting
the object. Nothing else holds the pointer, so each refusal stays in the module's memory for the life of the process.
A program compiling patterns it doesn't control grows with every invalid one: 100,000 compiles of (abc add about
19 MB per round, and a 1 KB unclosed group about 3 KB per compile. The issue has the repro and the numbers.

Change

  • internal/re2.go: delete the RE2 object as soon as its error has been read. reError copies the error argument into
    a Go string first, so the message is unaffected. deleteRE exists in both the wasm2go and the re2_cgo builds, so
    the fix covers both.
  • internal/re2_wasm2go_compile_test.go: compiles a refused 4 KB pattern 2,000 times and checks the module's memory
    doesn't grow. It fails without the change (the memory grows from 0.3 MB to 25 MB) and passes with it. It is wasm2go only,
    since that is where the memory can be read.

Testing

The two other points in #292 (a memory budget for Compile, and turning off RE2's error logging in the wasm2go build)
are not in this PR. I'm happy to send them separately if you want them.

Compile returned the parse error without deleting the RE2 object, so every refused pattern stayed in the module's memory. Refs wasilibs#292.
@anuraaga anuraaga closed this Nov 16, 2026
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