From 047200c11e3a8b96c51b5f1450b53e0a7cfaae50 Mon Sep 17 00:00:00 2001 From: Brian Wolff Date: Tue, 4 Oct 2022 10:39:21 -0700 Subject: [PATCH] Make sure that lua stack trace is valid UTF-8. This fixes a warning on php8.1 related to preg_match_all returning null when given invalid UTF-8. I made a separate patch to change the null into an exception Ic0c9083b In a sense, this is a follow-up to ec103b69667b4dfdc8bbe8. Bug: T319218 Change-Id: Ia17fc2fa428ec35bdbd242f1127fcdff501fb741 --- includes/Engines/LuaSandbox/LuaSandboxInterpreter.php | 11 ++++++++++- .../LuaStandalone/LuaStandaloneInterpreter.php | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/includes/Engines/LuaSandbox/LuaSandboxInterpreter.php b/includes/Engines/LuaSandbox/LuaSandboxInterpreter.php index 16b936b2..958b81cf 100644 --- a/includes/Engines/LuaSandbox/LuaSandboxInterpreter.php +++ b/includes/Engines/LuaSandbox/LuaSandboxInterpreter.php @@ -81,7 +81,16 @@ class LuaSandboxInterpreter extends LuaInterpreter { protected function convertSandboxError( LuaSandboxError $e ) { $opts = []; if ( isset( $e->luaTrace ) ) { - $opts['trace'] = $e->luaTrace; + $trace = $e->luaTrace; + foreach ( $trace as &$val ) { + $val = array_map( static function ( $val ) { + if ( is_string( $val ) ) { + $val = Validator::cleanUp( $val ); + } + return $val; + }, $val ); + } + $opts['trace'] = $trace; } $message = Validator::cleanUp( $e->getMessage() ); if ( preg_match( '/^(.*?):(\d+): (.*)$/', $message, $m ) ) { diff --git a/includes/Engines/LuaStandalone/LuaStandaloneInterpreter.php b/includes/Engines/LuaStandalone/LuaStandaloneInterpreter.php index cb543c88..36859ef7 100644 --- a/includes/Engines/LuaStandalone/LuaStandaloneInterpreter.php +++ b/includes/Engines/LuaStandalone/LuaStandaloneInterpreter.php @@ -416,6 +416,14 @@ class LuaStandaloneInterpreter extends LuaInterpreter { $message['value'] = $m[3]; } if ( isset( $message['trace'] ) ) { + foreach ( $message['trace'] as &$val ) { + $val = array_map( static function ( $val ) { + if ( is_string( $val ) ) { + $val = Validator::cleanUp( $val ); + } + return $val; + }, $val ); + } $opts['trace'] = array_values( $message['trace'] ); } throw $this->engine->newLuaError( $message['value'], $opts );