Skip to content

The Str::before implementation:Found it! **Line 216** has the bug: #8

Description

@alexstandiford

The Str::before implementation:Found it! Line 216 has the bug:

public static function before($subject, $before = ' '): string
{
    return substr($subject, 0, strpos($subject, $before) - strlen($subject));
}

The Bug

The formula is wrong: strpos($subject, $before) - strlen($subject)

When you call Str::before('wpl::public/assets/icon.svg', '::'):

  • $subject = 'wpl::public/assets/icon.svg'
  • $before = '::'
  • strpos($subject, $before) = 3 (position of ::)
  • strlen($subject) = 28
  • Result: substr($subject, 0, 3 - 28) = substr($subject, 0, -25)

This returns the string from position 0, going backwards 25 characters from the end, which would be 'wpl:' (including the first colon).

The Fix

Should be:

return substr($subject, 0, strpos($subject, $before));

The length parameter should just be the position of $before, not position - strlen($subject).Bug identified in mu-plugins/vendor/phpnomad/utils/lib/Helpers/Str.php:216

The Str::before() method is using incorrect math. It should return everything before the delimiter, but instead it's doing strpos($subject, $before) - strlen($subject) which creates a negative offset and returns incorrect results.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions