Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
This issue is not triggered by any test.
When running ./scripts/run-tests.sh
with some incorrect param (and no --color param):
php ./scripts/run-tests.sh --verbose --some_option
the following PHP notice is displayed:
PHP Notice: Trying to access array offset on value of type null in ./scripts/run-tests.sh on line 732
Affected code:
732 if ($args['color']) {
733 echo "\033[" . $color_code . "m" . $message . "\033[0m";
734 }
Proposed resolution
Fix PHP notice by wrapping the code in isset()
Remaining tasks
Patch review
User interface changes
None
API changes
None
Data model changes
None
Release notes snippet
See the parent issue
Comment | File | Size | Author |
---|---|---|---|
#20 | 3126140-20.patch | 434 bytes | longwave |
Comments
Comment #2
Taran2LComment #3
joseph.olstadComment #4
Ayesh CreditAttribution: Ayesh commentedShouldn't this be `!empty($args['color'])` call instead of isset()?
`$args['color'] = 0` will evaluate to false in current code, but true with `isset()`.
Test failures are not related to this patch either way :)
Comment #5
longwaveAgree with #4, this should be !empty()
Comment #6
joseph.olstadcombine isset with empty, avoid all notices.
Comment #7
MustangGB CreditAttribution: MustangGB commentedNo, they are mutually exclusive, choose one or the other.
https://www.php.net/manual/en/function.empty.php
empty() does not generate a warning if the variable does not exist.
Comment #8
joseph.olstadThanks, yes I agree, empty , there's no need for a micro optimisation here as this script is not actually called thousands of times in a thread like some others are (for loops in code that is run on every bootstrap for instance).
#2770065: array_key_exists() micro-optimization
isset()
being the fastest of the two, double condition is faster but out of scope for this patch.new patch
Comment #9
joseph.olstadComment #10
mcdruidYup, we either need
isset($args['color'])
or!empty($args['color'])
. The last patch has justempty($args['color'])
which is wrong.I think
isset($args['color'])
is probably more consistent with what we've been doing elsewhere, and as @joseph.olstad mentioned I don't think we care too much about the relative performance in this case.How about other references to elements of the
$args
global?...do these not have the same issue?
Comment #11
joseph.olstadok , ya oops about my last patch , how about this:
Comment #12
joseph.olstadComment #13
mcdruidThanks!
This is going to be tricky to review as I suppose we're stuck with only manual testing!
It looks like there's real mix of
isset()
andempty()
/!empty()
in the existing code.Although it might be nice to be more consistent, I think in this case I'd prefer to keep the changes to a minimum, so my vote would be only change references to array elements that'll cause PHP 7.4 exceptions and leave anything else that's already there alone.
Comment #14
longwaveThe real question here to me is why is only
color
affected, when we have code to ensure defaults exist in the $args array - this looks like it should solve this for all cases:Comment #15
Ayesh CreditAttribution: Ayesh commentedI really hope I'm missing something here...
isset()
andempty()
are not interchangeable.isset()
doesn't care about the "truthiness" of the value. It will simply return true if the object property, array key, or variable exists and is not null.empty()
will do it, and more, by loosely evaluating its truthiness.I do apologize if my comment in #4 sounded misleading. It was not against the use of
isset
vs!empty
, but rather the semantically correct use of it. Infunction simpletest_script_parse_args()
, you will see that the$args
array is initialized and the keycolor
is set toFALSE
. So withisset($args['color])
, evenFALSE
evaluates to true.Comment #16
Ayesh CreditAttribution: Ayesh commentedI cross-posted above #14. I totally agree with @longwave I think we need to investigate why
color
key was missing but nothing else.Can anyone else actually reproduce this bug?
Comment #17
mcdruidI had totally missed the initialisation of the defaults for
$args
- I hadn't really read the code properly at all, so I'm glad other people are :)Comment #18
mcdruidAs for reproducing the issue, yes I think I can:
Comment #19
mcdruidIs the problem that this:
...is called from within
simpletest_script_parse_args()
before that function returns, which means that$args
(with the defaults set) has not yet gone into the global scope.So at this point:
...isn't the global
$args
we're looking for, so to speak.Simplest fix would probably be to check if it's not empty then (that should work both in the case there's nothing there and it's been initialised to the default of FALSE).
Comment #20
longwaveI investigated the same way and found the same issue as #19,
$args
is not in global scope by the timesimpletest_script_print()
is called in the case of invalid parameters, so the only thing we need to fix is the check inside that function. In theory we could do a more comprehensive fix and correct the scoping but practically I think we should go for the smallest fix that works.I also agree that
!empty()
is correct here as FALSE should not pass the condition.Comment #21
mcdruidThanks!
Comment #22
joseph.olstadyes I like the minimalist approach, I was just throwing the kitchen sink in there for discussion.
+ patch 20 thanks @longwave
Comment #23
Taran2L+1 RTBC
Comment #24
Fabianx CreditAttribution: Fabianx at Tag1 Consulting commentedRTBM - thanks all!
Comment #26
mcdruidThanks everyone!