Problem/Motivation
When using PHP 7.4, these notices appear if no connection to Varnish could be established:
Notice: Trying to access array offset on value of type bool in varnish_get_status() (line 258 of .../varnish/varnish.module).
Notice: Trying to access array offset on value of type null in varnish_get_status() (line 258 of .../varnish/varnish.module).
Errors would also occur if the 'sockets' extension was not loaded:
Warning: array_shift() expects parameter 1 to be array, bool given in varnish_get_status() (line 257 of /var/www/html/sites/all/modules/contrib/varnish/varnish.module).
And after fixing the one above:
Warning: Invalid argument supplied for foreach() in varnish_requirements() (line 20 of .../varnish/varnish.install).
Steps to reproduce
_varnish_terminal_run() returns FALSEif the 'sockets' extension was not loaded. If no connection could be established, $stat['status'] will be FALSE.
In this case, the code in varnish_get_status() does not handle such a return value. This leads to the PHP notices listed above.
$status = _varnish_terminal_run(array('status'));
$terminals = explode(' ', variable_get('varnish_control_terminal', '127.0.0.1:6082'));
foreach ($terminals as $terminal) {
$stat = array_shift($status);
$results[$terminal] = ($stat['status']['code'] == 200) ? VARNISH_SERVER_STATUS_UP : VARNISH_SERVER_STATUS_DOWN;
}
Proposed resolution
return NULL; if $status is FALSE.
Check if isset when looping for status is up or down.
Add code to install file to check for the sockets requirement.
Remaining tasks
Review and test patch
Commit
Comments
Comment #2
solideogloria commentedComment #3
solideogloria commentedUploaded wrong patch file. This one returns NULL instead of VARNISH_SERVER_STATUS_DOWN, when the sockets extension is not found.
Comment #4
solideogloria commentedUpdated return types in function doc comments.
Consideration: these patches do not take advantage of static caching in
varnish_get_status()because of the return value. I figured it'd be better to keep existing behavior, and the sockets won't actually be called in this case anyway. Since that's why the cache is there, it doesn't seem necessary to cache empty or NULL values.Comment #5
gerzenstl commentedThe patch works fine.
For the tests I used:
* PHP: 7.4.16
* Drupal: 7.81
* Varnish module: 1.9
Comment #6
pobster commentedFor the requirements check, it'd be better if you outright check using https://www.php.net/manual/en/function.extension-loaded.php for sockets being present?
Also, just an opinion, but this;
Would be a lot neater if it just wrapped and returned the empty array like this;
Then there's no need to alter the return typehint.
Comment #7
steven jones commentedPatch seems to work fine. I'm marking as RTBC because while not perfect, it does at least solve the problem of the notices.
The module maintainer can decide if they want to reject the fix in favour of a better/different solution.
Comment #8
steven jones commentedComment #9
max lovric commentedIgnore
Comment #10
max lovric commentedComment #11
max lovric commented7.x-1.1 patch
Comment #13
tree2009 commented