This patch fixes a PHP5 call time pass-by-reference error

Senpai - June 15, 2008 - 04:04
Project:Apache Solr Search Integration
Version:5.x-1.x-dev
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:closed
Description

Line 71 of the apachesolr_search.module throws a PHP call time pass-by-reference error.

<?php
70 
foreach ($response->documents as $doc) {
71    $extra = node_invoke_nodeapi(&$doc, 'search result');
?>

The nodeapi documentation mentions an implied pass-by-reference in the api structure's parameter one, so I don't think this is needed in line 71, and removing it fixes the error.

Patch attached.

#1

Senpai - June 15, 2008 - 04:05

Ok, maybe this time the patch will stick.

V (look below, and tell me if it's there?) V

AttachmentSize
apachesolr_nodeapi_passbyreference.patch838 bytes

#2

Senpai - June 17, 2008 - 00:05

Take 2. There's another pass by reference error going on with the module_invoke_all() in apachesolr.module, and if I read things right, the intent is to allow other modules to alter the $document variable, not alter a copy of it.

This patch addresses both of these PHP5 warnings with each module file getting a one line change.

AttachmentSize
apachesolr_pass_by_reference_errors.patch1.61 KB

#3

Senpai - June 17, 2008 - 00:24
Status:patch (code needs review)» patch (code needs work)

Disregard the patch in #2 for now. I didn't think it through enough, and it causes cron to fatal error at apachesolr.module line 299 "Call to a member function setMultiValue() on a non-object".

Doh!

#4

robertDouglass - July 19, 2008 - 18:17
Status:patch (code needs work)» fixed

Commit #2. Thanks.

#5

Senpai - July 20, 2008 - 21:55

@Robert: Did you change/fix my #2 patch before committing it? I was trying to say that the patch I submitted in comment #2 causes a fatal error in certain cases. #2 is no good.

#6

robertDouglass - July 20, 2008 - 22:15

No, I committed #1, which tested out ok. The issue in #2 will have to be looked at separately.

#7

drunken monkey - August 1, 2008 - 12:36
Status:fixed» patch (code needs review)

The attached patch should repair the second error.
I changed it a little compared to the one I posted to the duplicate issue, this version of the patch should work in any case, I think. I just remembered that PHP allows calling a variable function by simply writing the variable and the arguments. That way, only the definition of the function is important and if its defined with the first parameter being passed by reference (as it should, if it intends to change the object), all's well.
I don't really know, whether the version with call_user_func_array() would have worked too.

Anyways, please review!

AttachmentSize
call-time_reference.patch931 bytes

#8

febbraro - August 4, 2008 - 16:59

#7 patch worked for me. Thanks.

#9

robertDouglass - September 7, 2008 - 13:30
Priority:normal» critical

If febbraro and drunkenmonkey both sign off on #7, please commit to both branches.

#10

rszrama - September 15, 2008 - 20:42

I don't see how #7 fixes the call-time pass-by-reference. It still uses &$ in the replacement foreach().

I'm curious... given the way PHP handles objects, do we need to make this a reference at all. Has it been tested w/ just passing $document? It's kind of funky when PHP passes references or not w/ objects, which is why I'm a little unsure... I just know I've been burned by it passing an object by reference when I thought it was by value. : ?

#11

JacobSingh - September 18, 2008 - 06:06

Indeed, Robert is correct. It is the implementers who must receive the request by reference, that is what the ticket is about in the first place.

I've done 3 things (sorry, one may not be the task in this issue, but it made sense):

1. Moved the hook invocation to the bottom of the Document building code. Why? Because it is a lot more useful. Modules will sometimes want to change things provided by the node based additions in apachesolr.module, and this allows them to do so. Before, they could only add items, and what they added could easily have gotten overridden by the node, content and taxonomy based manipulations happening later on. In the future, I'd like to see this refactored, so there are different indexing modules which can be switched on and off which add stuff to the document. So apachesolr_cck and apachesolr_taxonomy which would implement this hook. That seems like a more flexible architechture. At any rate, I don't see any objection to moving the function

2. I fixed the patch above and simply removed the & before the $document. Since no modules are even using this hook now, I don't think this will cause any issues

3. I added documentation to the readme for module maintainers on how to use the hook.

AttachmentSize
apachesolr_update_index_hook_moved_and_ref_fixed.diff2.2 KB

#12

robertDouglass - October 5, 2008 - 17:30
Status:patch (code needs review)» fixed

Fixed in D5 and D6. Thanks!

#13

Anonymous (not verified) - October 19, 2008 - 17:31
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.