Skip to content

bug(core): WebPlugin removeListener removes wrong listener when target not found #8269

@maniktyagi04

Description

@maniktyagi04

Bug Report

Description

When is called with a listener function that was never added, it incorrectly removes the last listener in the array instead of doing nothing.

Root Cause

In , line 100-101:

const index = listeners.indexOf(listenerFunc);
this.listeners[eventName].splice(index, 1);

When listenerFunc is not found, indexOf returns -1, and splice(-1, 1) removes the last element.

Expected Behavior

Calling removeListener with a non-existent listener should do nothing.

Actual Behavior

The last listener in the array is incorrectly removed.

Steps to Reproduce

  1. Add two listeners to an event
  2. Try to remove a third listener that was never added
  3. Observe that the second listener is removed instead

Proposed Fix

Add a check before splicing:

const index = listeners.indexOf(listenerFunc);
if (index !== -1) {
  this.listeners[eventName].splice(index, 1);
}

Impact

  • Severity: Medium
  • Affected: Web platform implementations using WebPlugin
  • Breaking: No

Additional Context

I have a PR ready with the fix and test case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions