prisma / tiberius

TDS 7.2+ (Microsoft SQL Server) driver for Rust
Apache License 2.0
329 stars 122 forks source link

Panic while reading datetime field with date < 1900 #316

Open squareduck opened 1 year ago

squareduck commented 1 year ago

I have a database with DateTime field like this 1899-12-30 00:00:00.000.

Trying to read this field with time feature results in panic - attempting to multiply with overflow:

        // Here `self` is tiberius::Row
        let value = self.try_get::<time::PrimitiveDateTime, _>(name);

Results in:

thread '<unnamed>' panicked at 'attempt to multiply with overflow', /Users/squareduck/.config/rust/cargo/registry/src/index.crates.io-6f17d22bba15001f/tiberius-0.12.2/src/tds/time/time.rs:15:31

In SQL Server docs it's said that DateTime supports dates from year 1753, and only for Date type the lower bound is 1900: https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql?view=sql-server-ver16

So to me it looks like the culprit is this starting year value: https://github.com/prisma/tiberius/blob/8f66a699dfa041e7b5f736c7e94f92c945453c9e/src/tds/time/time.rs#L57

janpio commented 1 year ago

That is very possible. Do you have a suggestion on how to fix it and might be able to submit a PR with tests?