Skip to content

fix: prevent panic in cache reset methods when cache is None#1337

Open
saschabuehrle wants to merge 1 commit intorust-lang:masterfrom
saschabuehrle:fix/onepass-cache-reset-panic
Open

fix: prevent panic in cache reset methods when cache is None#1337
saschabuehrle wants to merge 1 commit intorust-lang:masterfrom
saschabuehrle:fix/onepass-cache-reset-panic

Conversation

@saschabuehrle
Copy link

Bug

Fixes #1155meta::Cache::reset can panic if called on a different regex than it was created with.

Root Cause

The reset methods in OnePassCache, HybridCache, and ReverseHybridCache checked if builder.0 was Some but then called self.0.as_mut().unwrap() without verifying self.0 was also Some. This could happen when a cache was created with ::none() (which sets the internal cache to None) but then reset with a builder that had an engine.

Fix

The fix checks if self.0 is Some before calling reset:

  • If Some, reset the existing cache
  • If None, create a new cache from the builder's engine

This allows caches to be safely reset with any compatible regex, which is the intended behavior of the reset method.

Testing

The reproduction case from the issue now works without panicking:

let re1 = Regex::new("").unwrap();
let re2 = Regex::new("\\b").unwrap();
re1.create_cache().reset(&re2); // No longer panics

Greetings, saschabuehrle

Fixed panic in OnePassCache, HybridCache, and ReverseHybridCache reset
methods when attempting to reset a None cache with a Some builder.

The issue was that reset() checked if builder.0 was Some but then
called self.0.as_mut().unwrap() without verifying self.0 was also Some.
This could happen when a cache was created with ::none() but reset with
a builder that had an engine.

The fix checks if self.0 is Some before calling reset, and if None,
creates a new cache from the builder's engine.

Fixes rust-lang#1155
@pawamoy
Copy link

pawamoy commented Mar 20, 2026

Sorry to interrupt, I wanted to let you know that this PR is most probably entirely AI-generated, including replies to your comments. Just see the batch of PRs they opened recently to form your own opinion.

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.

meta::Cache::reset can panic

2 participants