Page MenuHomePhorge

Fix preamble-support
ClosedPublic

Authored by avivey on Apr 7 2023, 10:25.

Details

Summary

Phutil is not yet loaded during preamble, so we can't use it.

This change fixes a regression introduced here, fixing PHP 8.1 support:

rP96ae4ba13acb: PHP 8.1: fixes for strlen() not accepting NULL anymore, part 2

Ref T15064

Test Plan

Included test script.

Diff Detail

Repository
rP Phorge
Branch
arcpatch-D25114
Lint
Lint Warnings
SeverityLocationCodeMessage
Warningsupport/startup/__tests__/PreambleUtilsTestCase.php:106XHP132Partial Catch
Advicesupport/startup/__tests__/PreambleUtilsTestCase.php:85XHP55Unnecessary Final Modifier
Unit
No Test Coverage
Build Status
Buildable 241
Build 241: arc lint + arc unit

Event Timeline

avivey requested review of this revision.Apr 7 2023, 10:25
support/startup/preamble-utils.php
24

I suggest a strict check there.

I also strongly would like to push this that is what we exactly want, so, exclude NULL, have a string, and have it non-empty:

if(is_string($forwarded_for) && $forwarded_for !== '') {
support/startup/preamble-utils.php
24

Ouch note that both the patch and my suggestions are incorrect.

The correct way is the total reverse, so:

if ($forwarded_for === null || !strlen($forwarded_for)) {

Or, what I would like to use:

if(!is_string($forwarded_for) || $forwarded_for === '') {
This revision now requires changes to proceed.Apr 7 2023, 13:39

Hi @avivey thanks for this fix in my regression. Feel free to update this to just negate the if and make it correct, or here there is a quick fix ready to be accepted:

D25119: Fix regression in preamble

(I've done that just in order to be nice and don't bother you with additional fatigue for my regression. So, if you have time just continue here, or move to D25119)

Since the bug you found was major, and my tests didn't catch it, I'm going to do another round of more in-depth testing for this...

support/startup/preamble-utils.php
24

To reduce count of double-negatives, let's add phutil_is_empty_string() function.

support/startup/preamble-utils.php
24

actually, it might end up more confusing with NULL inputs.

add tests and keep simplest code

Added a test-script to check all possible inputs. It's not run automatically, because this isn't part of the main code.

Anyway, turns out that $forwarded_for in line 23 is never null, because "isset() will return false when checking a variable that has been assigned to null." (https://www.php.net/manual/en/function.isset); so strlen is fine here.

Thank you for this fix and for the tests

I still recommend this:

if(!is_string($forwarded_for) || $forwarded_for === '') {

In order to exclude non-strings, non empty strings, and with O(1) complexity instead of O(N) or something depending on the length of the string, so that we minimize CPU cycles and Greta Thunberg goes like "wooooow Phorge"

support/startup/preamble-utils.php
19

Yeah the NULL check is already done.

This revision is now accepted and ready to land.Apr 9 2023, 18:32
This revision was automatically updated to reflect the committed changes.