From 8360d48296ecf62dee8bb322f1b94c11345d01cb Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Thu, 15 Aug 2024 15:09:32 -0700 Subject: [PATCH] fix: audit `sqlx_postgres::types::chrono` for overflowing casts --- sqlx-postgres/src/types/chrono/date.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sqlx-postgres/src/types/chrono/date.rs b/sqlx-postgres/src/types/chrono/date.rs index 475f41f4..5fc0a8f0 100644 --- a/sqlx-postgres/src/types/chrono/date.rs +++ b/sqlx-postgres/src/types/chrono/date.rs @@ -23,7 +23,11 @@ impl PgHasArrayType for NaiveDate { impl Encode<'_, Postgres> for NaiveDate { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result { // DATE is encoded as the days since epoch - let days = (*self - postgres_epoch_date()).num_days() as i32; + let days: i32 = (*self - postgres_epoch_date()) + .num_days() + .try_into() + .map_err(|_| format!("value {self:?} would overflow binary encoding for Postgres DATE"))?; + Encode::::encode(days, buf) }