Skip to content

Commit

Permalink
Fix user_pref_cache_array_rows if fetch_all fails
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dregad committed Jun 10, 2019
1 parent 7ed1892 commit 0d2b817
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions core/user_pref_api.php
Expand Up @@ -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)
Expand Down

0 comments on commit 0d2b817

Please sign in to comment.