From 0d2b817fca12b42a13cba4d1da501d4cf58bf940 Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Mon, 10 Jun 2019 16:08:05 +0200 Subject: [PATCH] Fix user_pref_cache_array_rows if fetch_all fails DbQuery::fetch_all() can return false if the query execution fails. In that case, the foreach triggers a PHP "Invalid argument supplied for foreach()" warning. Also, improve code readability by storing the user ID in a variable to avoid repeated typecasts. Fixes #25850 --- core/user_pref_api.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/core/user_pref_api.php b/core/user_pref_api.php index 5f7d300085..2df4a43f86 100644 --- a/core/user_pref_api.php +++ b/core/user_pref_api.php @@ -331,14 +331,18 @@ function user_pref_cache_array_rows( array $p_user_id_array, $p_project_id = ALL $t_query->bind( 'user_array', $c_user_id_array ); $t_query->bind( 'project_id', (int)$p_project_id ); - foreach( $t_query->fetch_all() as $t_row ) { - if( !isset( $g_cache_user_pref[(int)$t_row['user_id']] ) ) { - $g_cache_user_pref[(int)$t_row['user_id']] = array(); + $t_result = $t_query->fetch_all(); + if( $t_result ) { + foreach( $t_result as $t_row ) { + $t_user_id = (int)$t_row['user_id']; + if( !isset( $g_cache_user_pref[$t_user_id] ) ) { + $g_cache_user_pref[$t_user_id] = array(); + } + $g_cache_user_pref[$t_user_id][(int)$p_project_id] = $t_row; + + # remove found users from required set. + unset( $c_user_id_array[$t_user_id] ); } - $g_cache_user_pref[(int)$t_row['user_id']][(int)$p_project_id] = $t_row; - - # remove found users from required set. - unset( $c_user_id_array[(int)$t_row['user_id']] ); } # cache users that are not found as false (i.e. negative cache)