Skip to content

Fix NullPointerException in regex-based attribute equal filter with empty pattern - #3324

Merged
mbastian merged 1 commit into
masterfrom
fix/filter-regex-npe-null-pattern
Sep 27, 2026
Merged

mbastian merged 1 commit into
masterfrom
fix/filter-regex-npe-null-pattern

Conversation

@mbastian

Copy link
Copy Markdown
Member

Description

Fixes a NullPointerException in EqualStringFilter.evaluate() affecting 4 users across 9 events in production.

Mechanism: EqualStringFilter (modules/FiltersPlugin/src/main/java/org/gephi/filters/plugin/attribute/AttributeEqualBuilder.java:144-200) declares private Pattern regex; with no initializer. The only place that ever assigns it is setPattern(String) (line 189-192), which does this.regex = Pattern.compile(pattern);. evaluate() (line 165-179) calls regex.matcher(valString).matches() at line 173 whenever useRegex is true, with no null check.

regex stays null while useRegex is true in two situations:

  1. A freshly constructed filter (pattern = "", regex = null by default) has setUseRegex(true) called on it without setPattern(...) ever being called first — e.g. directly from the filter UI.
  2. Loading a saved project with such a filter: FilterModelPersistenceProvider.readQuery() (modules/FiltersImpl/src/main/java/org/gephi/filters/FilterModelPersistenceProvider.java:325) only calls property.setValue(value) if (property != null && textBuffer.length() > 0). When the filter's pattern was "" at save time, the persisted <parameter> element for pattern is empty text, so setPattern() is skipped entirely on load — while the useRegex=true parameter (non-empty text "true") is replayed via setUseRegex(true). The filter comes back from persistence with useRegex=true and regex still null.

Either path leads straight into FilterProcessor.processNodeFilter() (modules/FiltersImpl/src/main/java/org/gephi/filters/FilterProcessor.java:155) calling nodeFilter.evaluate(graph, n) on every node, which throws on the first node with a non-null attribute value.

Stack trace (GEPHI-6CG)
java.lang.NullPointerException: Cannot invoke "java.util.regex.Pattern.matcher(java.lang.CharSequence)" because "this.regex" is null
    at org.gephi.filters.plugin.attribute.AttributeEqualBuilder$EqualStringFilter.evaluate(AttributeEqualBuilder.java:173)
    at org.gephi.filters.FilterProcessor.processNodeFilter(FilterProcessor.java:155)
    at org.gephi.filters.FilterProcessor.process(FilterProcessor.java:121)
    at org.gephi.filters.FilterThread.filter(FilterThread.java:160)
    at org.gephi.filters.FilterThread.run(FilterThread.java:132)

Fix: initialize regex from the default pattern value at field declaration (private Pattern regex = Pattern.compile(pattern);), relying on pattern's initializer running first since it's declared above regex. This guarantees regex is never null regardless of how the filter reaches its default/deserialized state, instead of depending entirely on setPattern() having been called at some point. This keeps the regex-empty-pattern semantics consistent with the non-regex branch: an empty pattern matches only empty-string attribute values ("".matches("") is true, same as "".equals(valString)), rather than silently treating the filter as always-match or never-match.

Sentry: https://gephi.sentry.io/issues/GEPHI-6CG

Checklist

  • Merged with master beforehand

Added tests?

  • 👍 yes

Added regression tests to AttributeEqualBuilderTest: one constructs a filter and calls setUseRegex(true) without ever calling setPattern(...), then evaluates it; the other calls setPattern("") explicitly (covering the persistence-reload path) before setUseRegex(true) and evaluates. Both previously threw the NPE and now correctly return false for a non-empty attribute value.

Added to documentation?

  • 👍 README.md
  • 👍 API Changes
  • 👍 Additional documentation in docs
  • 👍 Relevant code documentation
  • 🙅 no, because they aren't needed

🤖 Generated with Claude Code

EqualStringFilter.evaluate() calls regex.matcher(...) whenever useRegex
is true, but the regex field is only ever assigned inside setPattern().
A filter left in its default state (pattern="", regex=null) with
useRegex set to true throws a NullPointerException instead of matching.

This happens both from the UI, when setUseRegex(true) is called without
setPattern() ever being called, and when reloading a saved project:
FilterModelPersistenceProvider.readQuery() only calls setValue() on a
persisted parameter when its serialized text is non-empty, so a pattern
of "" is never replayed through setPattern() on load, leaving regex
null even though useRegex=true is restored.

Initialize regex from the default pattern value at field declaration so
it can never be null, keeping the empty-pattern regex semantics
consistent with the non-regex branch (pattern.equals(valString)),
which already matches only empty-string values for pattern="".

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@mbastian
mbastian requested a review from a team as a code owner September 27, 2026 15:20
@mbastian mbastian added this to the 0.11.4 milestone Sep 27, 2026
@mbastian
mbastian merged commit 5933ac0 into master Sep 27, 2026
3 checks passed
@mbastian
mbastian deleted the fix/filter-regex-npe-null-pattern branch November 17, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant