Fix an undefined behavior for bit shift in citus_stat_tenants.c (#7954)

DESCRIPTION: Fixes an undefined behavior that could happen when
computing tenant score for citus_stat_tenants

Add check for shift size, reset to zero in case of overflow

Fixes #7953.

---------

Co-authored-by: Onur Tirtir <onurcantirtir@gmail.com>
pull/8158/head^2
manaldush 2025-09-04 13:57:45 +03:00 committed by GitHub
parent 8ece8acac7
commit 2834fa26c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 1 deletions

View File

@ -49,6 +49,9 @@ ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
#define STAT_TENANTS_COLUMNS 9
#define ONE_QUERY_SCORE 1000000000
/* this doesn't attempt dereferencing given input and is computed in compile-time, so it's safe */
#define TENANT_STATS_SCORE_FIELD_BIT_LENGTH (sizeof(((TenantStats *) NULL)->score) * 8)
static char AttributeToTenant[MAX_TENANT_ATTRIBUTE_LENGTH] = "";
static CmdType AttributeToCommandType = CMD_UNKNOWN;
static int AttributeToColocationGroupId = INVALID_COLOCATION_ID;
@ -605,8 +608,13 @@ ReduceScoreIfNecessary(TenantStats *tenantStats, TimestampTz queryTime)
*/
if (periodCountAfterLastScoreReduction > 0)
{
tenantStats->score >>= periodCountAfterLastScoreReduction;
tenantStats->lastScoreReduction = queryTime;
/* addtional check to avoid undefined behavior */
tenantStats->score = (periodCountAfterLastScoreReduction <
TENANT_STATS_SCORE_FIELD_BIT_LENGTH)
? tenantStats->score >> periodCountAfterLastScoreReduction
: 0;
}
}