Skip to content

Commit

Permalink
Fix PHP Notice in user_cache_row()
Browse files Browse the repository at this point in the history
If the function receives an invalid user ID, it will issue an Undefined
offset notice, as it does not check for actual existence of the ID in
the users' cache after calling user_cache_array_rows().

PHPDoc does state that a valid user ID is expected, but since the
function includes error handling code, it makes sense to account for
this case, unlikely as it may be.

Fixes #25850
  • Loading branch information
dregad committed Jun 10, 2019
1 parent 0d2b817 commit 2cee661
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions core/user_api.php
Expand Up @@ -83,23 +83,21 @@ function user_cache_row( $p_user_id, $p_trigger_errors = true ) {

if( !isset( $g_cache_user[$c_user_id] ) ) {
user_cache_array_rows( array( $c_user_id ) );
}

$t_user_row = $g_cache_user[$c_user_id];
if( !isset( $g_cache_user[$c_user_id] ) ) {
if( $p_trigger_errors ) {
throw new ClientException(
sprintf( "User id '%d' not found.", (integer)$p_user_id ),
ERROR_USER_BY_ID_NOT_FOUND,
array( (integer)$p_user_id )
);
}

if( !$t_user_row ) {
if( $p_trigger_errors ) {
throw new ClientException(
sprintf( "User id '%d' not found.", (integer)$p_user_id ),
ERROR_USER_BY_ID_NOT_FOUND,
array( (integer)$p_user_id )
);
return false;
}

return false;
}

return $t_user_row;
return $g_cache_user[$c_user_id];
}

/**
Expand Down Expand Up @@ -131,14 +129,16 @@ function user_cache_array_rows( array $p_user_id_array ) {
$t_query = 'SELECT * FROM {user} WHERE id IN (' . implode( ',', $t_sql_in_params ) . ')';
$t_result = db_query( $t_query, $t_params );

while( $t_row = db_fetch_array( $t_result ) ) {
$c_user_id = (int)$t_row['id'];
$g_cache_user[$c_user_id] = $t_row;
unset( $c_user_id_array[$c_user_id] );
}
# set the remaining ids to false as not-found
foreach( $c_user_id_array as $t_id ) {
$g_cache_user[$t_id] = false;
if( $t_result !== false ) {
while( $t_row = db_fetch_array( $t_result ) ) {
$c_user_id = (int)$t_row['id'];
$g_cache_user[$c_user_id] = $t_row;
unset( $c_user_id_array[$c_user_id] );
}
# set the remaining ids to false as not-found
foreach( $c_user_id_array as $t_id ) {
$g_cache_user[$t_id] = false;
}
}
}

Expand Down Expand Up @@ -1790,4 +1790,4 @@ function user_has_more_than_one_project( $p_user_id ) {
}
}
return true;
}
}

0 comments on commit 2cee661

Please sign in to comment.