util: Fix memory leak in PatternMatch (#282)

When allocating a new string object in tcl you need increment and
then decremenet the ref counter in order for objects to be correctly
collected.

Signed-off-by: Ethan Mahintorabi <ethanmoon@google.com>
This commit is contained in:
Ethan Mahintorabi 2025-08-11 16:25:47 -07:00 committed by GitHub
parent 82fd625199
commit 6d18003c04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 2 additions and 0 deletions

View File

@ -89,7 +89,9 @@ PatternMatch::compileRegexp()
anchored_pattern += '$';
Tcl_Obj *pattern_obj = Tcl_NewStringObj(anchored_pattern.c_str(),
anchored_pattern.size());
Tcl_IncrRefCount(pattern_obj);
regexp_ = Tcl_GetRegExpFromObj(interp_, pattern_obj, flags);
Tcl_DecrRefCount(pattern_obj);
if (regexp_ == nullptr && interp_)
throw RegexpCompileError(pattern_);
}