From 5a7670e42693e312d14e702e26d07709cd291b3f Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 10 Oct 2018 20:17:37 +0100 Subject: [PATCH] Minor OrderedStreamingForkController improvements Follows-up c94dea7029cae. * Avoid strlen() where a strict check suffices. * Use substr() for both comparison and stripping, avoid potentially expensive, but also fragile, because strlen()-1 can produce $str[-1], which would > PHP Notice: Uninitialized string offset: -1 Luckily, the implied null would work as expected, given that `null !== "\n"` and produces the same result. Change-Id: If61e5e412aaa2dc7c00c4441b3b7bd5f04160ec8 --- includes/OrderedStreamingForkController.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/includes/OrderedStreamingForkController.php b/includes/OrderedStreamingForkController.php index 11abc81c1f..5424b13285 100644 --- a/includes/OrderedStreamingForkController.php +++ b/includes/OrderedStreamingForkController.php @@ -135,13 +135,16 @@ class OrderedStreamingForkController extends ForkController { protected function consumeNoFork() { while ( !feof( $this->input ) ) { $data = fgets( $this->input ); - if ( $data[ strlen( $data ) - 1 ] == "\n" ) { + if ( substr( $data, -1 ) === "\n" ) { + // Strip any final new line used to delimit lines of input. + // The last line of input might not have it, though. $data = substr( $data, 0, -1 ); } - if ( strlen( $data ) !== 0 ) { - $result = call_user_func( $this->workCallback, $data ); - fwrite( $this->output, "$result\n" ); + if ( $data === '' ) { + continue; } + $result = call_user_func( $this->workCallback, $data ); + fwrite( $this->output, "$result\n" ); } } @@ -163,12 +166,12 @@ class OrderedStreamingForkController extends ForkController { $this->updateAvailableSockets( $sockets, $used, $sockets ? 0 : 5 ); } while ( !$sockets ); } - // Strip the trailing \n. The last line of a file might not have a trailing - // \n though - if ( $data[ strlen( $data ) - 1 ] == "\n" ) { + if ( substr( $data, - 1 ) === "\n" ) { + // Strip any final new line used to delimit lines of input. + // The last line of input might not have it, though. $data = substr( $data, 0, -1 ); } - if ( strlen( $data ) === 0 ) { + if ( $data === '' ) { continue; } $socket = array_pop( $sockets ); -- 2.20.1