search_excerpt() incorrectly assumes that search_simplify_excerpt_match() always returns an array, however this is not true: The function returns FALSE when there are no matches.

With PHP 7.4, this will generate a notice, as the code is trying to use as an array a Boolean value.

Comments

Taran2L created an issue. See original summary.

taran2l’s picture

Status: Active » Needs review
StatusFileSize
new992 bytes
taran2l’s picture

Issue summary: View changes
avpaderno’s picture

Issue summary: View changes
hardik_patel_12’s picture

@Taran2L thankyou for patch, patch in comment #2 LGTM.

hardik_patel_12’s picture

Status: Needs review » Reviewed & tested by the community
izmeez’s picture

Thank you. Patch in comment #2 addresses php 7.4 notices when using search block:

Deprecated: Array and string offset access syntax with curly braces is deprecated in /modules/search/search.extender.inc on line 222

Notice: Trying to access array offset on value of type null in theme_pager() (line 331 of /includes/pager.inc).

mcdruid’s picture

D8's search.module has a search_excerpt() function but the code has diverged from D7 to the extent that there is no search_simplify_excerpt_match(), and therefore this problem wasn't there when PHP 7.4 compatibility was addressed.

 * @return
 *   FALSE if no match is found. If a match is found, return an associative
 *   array with element 'where' giving the position of the match, and element
 *   'keyword' giving the actual word found in the text at that position.
 */
function search_simplify_excerpt_match($key, $text, $offset, $boundary) {

Looking at that, I think the patch in #2 looks great.

Re-running the tests for that patch, including with PHP 7.4.

mcdruid’s picture

Issue tags: +Pending Drupal 7 commit

Same methodology as #3084955-9: Invalid characters passed for attempted conversion, these have been ignored in _color_unpack() to compare PHP 7.4 exceptions before this patch with after:

$ curl -s https://www.drupal.org/pift-ci-job/1601005 | grep -o 'exception: .* Line [0-9]* of .*:' | perl -pe 's#<.*?>##g' | sort | uniq -c | sort -rn | diff - <(curl -s https://www.drupal.org/pift-ci-job/1603581 | grep -o 'exception: .* Line [0-9]* of .*:' | perl -pe 's#<.*?>##g' | sort | uniq -c | sort -rn)
1c1
<  189257 exception: [Notice] Line 6656 of includes/common.inc:
---
>  189339 exception: [Notice] Line 6656 of includes/common.inc:
4d3
<     549 exception: [Notice] Line 1175 of modules/search/search.module:

Again, I'm not sure why the number of Notices from element_children() changes, but that should be addressed by #3084935: element_children() and DrupalRequestSanitizer::stripDangerousValues() should not use integer keys as array

The other difference in results confirms that this patch resolves the problem in search_excerpt().

So I'm happy with this patch, and think it's ready for final review.

fabianx’s picture

Status: Reviewed & tested by the community » Needs work

I disagree with the fix here.

a) The syntax if ($a = b()) leads often to errors down the road, so let's please avoid it.

b) That is not the same check - before we checked explicitly for the 'where' component - now we only check for $info

c) The more simpler fix is:

if (isset($info['where'])) {
}

or

if (!empty($info['where'])) {
}

unless I miss something.

mcdruid’s picture

Status: Needs work » Needs review
StatusFileSize
new548 bytes
new535 bytes

I was thinking we could be fairly confident about what would be returned, but I suppose if search_simplify_excerpt_match() itself was changed, any assumptions we've made here might cause problems (as unlikely as that seems).

Here's a patch which just checks if (isset($info['where'])) as suggested. Simple is good :)

mcdruid’s picture

Status: Needs review » Reviewed & tested by the community

The simple patch LGTM.

RTBC and ready for the next round of committer reviews.

fabianx’s picture

Assigned: Unassigned » mcdruid

Let's get this in, thanks all.
(gave myself credit for the solution)

  • mcdruid committed 942c394 on 7.x
    Issue #3084965 by mcdruid, Taran2L, Fabianx: Trying to access array...
mcdruid’s picture

Assigned: mcdruid » Unassigned
Status: Reviewed & tested by the community » Fixed
Issue tags: -Pending Drupal 7 commit

Great - thanks all!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.