mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

System.IndexOutOfRangeException when trying to open DB with password #1504

Open panthernet opened 4 years ago

panthernet commented 4 years ago

Hi, I'm following the v5 guide but stuck with the System.IndexOutOfRangeException exception when I try to access the database with the password. Here's the code, .NET Core 3.1.

var str = $"Filename={path};Password='{verylongpwd}'"; using var db = new LiteDatabase(str);

The string sample is: Filename=C:\Users\yup\AppData\Roaming\corex\storecore.file;Password='1495c305c5312dd1a9a18d9502daa0369216763ca7a6f537ddbe290241cf8aad1ca326313adec74bb98d1955747347cf0e3f087899d8bb2e0aa002ff825e1c0f25eaa79e5dfbf1c0e2daf6746a3a3f140244b764204c20c0ccede3521eaf8537ae32d4b13a04f1c387f56a8d6fa095bc53451c1892a46b8182afd94559cd7377aebc8d4a2b4883c637a359e6e67e1d8c2d789721351ebb000409329b2e875d21278b7c76724c68729e53dac50168564b8c3432018212a111c952e593829b42c296458cc0020174aaef9ca6b5661ca965004404c2bbb256bc41a8aa5c5349c615e40328a3263c45e5f96e61048149e98aa8b6f2afb59d73379e1dce5429752d8d'

If I add ; to the end of the string, I get

System.ArgumentOutOfRangeException: 'Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. (Parameter 'ticks')'

It is very confusing for the moment.

mbdavid commented 4 years ago

Hi @panthernet, can you paste your stacktrace?

panthernet commented 4 years ago

Here you go

   at LiteDB.DictionaryExtensions.<ParseKeyValue>g__ReadValue|2_1(<>c__DisplayClass2_0& )
   at LiteDB.DictionaryExtensions.ParseKeyValue(IDictionary`2 dict, String connectionString)
   at LiteDB.ConnectionString..ctor(String connectionString)
   at LiteDB.LiteDatabase..ctor(String connectionString, BsonMapper mapper)
   at X.DB.DBManager.<Session>d__1.MoveNext() in D:\Projects\x\x\Classes\DB\DBManager.cs:line 86
panthernet commented 4 years ago

And this is for the ticks exception

   at System.DateTime..ctor(Int64 ticks, DateTimeKind kind)
   at LiteDB.BufferSliceExtensions.ReadDateTime(BufferSlice buffer, Int32 offset)
   at LiteDB.Engine.HeaderPage..ctor(PageBuffer buffer)
   at LiteDB.Engine.LiteEngine..ctor(EngineSettings settings)
   at LiteDB.ConnectionString.CreateEngine()
   at LiteDB.LiteDatabase..ctor(ConnectionString connectionString, BsonMapper mapper)
   at LiteDB.LiteDatabase..ctor(String connectionString, BsonMapper mapper)
   at X.DB.DBManager.<Session>d__1.MoveNext() in D:\Projects\x\x\Classes\DB\DBManager.cs:line 86
0verflux commented 4 years ago

same error here

I get this error when using a connection string. I did what the document says, and i get this error: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

This exception was originally thrown at this call stack:
  LiteDB.DictionaryExtensions.ParseKeyValue.__ReadValue|2_1(ref LiteDB.DictionaryExtensions.<>c__DisplayClass2_0)
  LiteDB.DictionaryExtensions.ParseKeyValue(System.Collections.Generic.IDictionary<string, string>, string)
  LiteDB.ConnectionString.ConnectionString(string)
  LiteDB.LiteRepository.LiteRepository(string, LiteDB.BsonMapper)
    SampleForm.Form1.Form1_Load(object, System.EventArgs) in Form1.cs
  System.Windows.Forms.Form.OnLoad(System.EventArgs)

App.config

<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>

    <connectionStrings>
        <add name="LiteDB" connectionString="Filename=C:\Users\<user>\Desktop\Database.db;Password='1234'" />
    </connectionStrings>
</configuration>

Source

    using (var db = new LiteDatabase(System.Configuration.ConfigurationManager.ConnectionStrings["LiteDB"].ConnectionString)) { ... }
mbdavid commented 4 years ago

Ok, I found a bug in ConnectionString parser, when finalize with '. I'm fixing.... for now, just remove open/close '

alex-fomin commented 4 years ago

Got the same error:

Unhandled exception. System.ArgumentOutOfRangeException: Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. (Parameter 'ticks')
   at System.DateTime..ctor(Int64 ticks, DateTimeKind kind)
   at LiteDB.BufferSliceExtensions.ReadDateTime(BufferSlice buffer, Int32 offset)
   at LiteDB.Engine.HeaderPage..ctor(PageBuffer buffer)
   at LiteDB.Engine.LiteEngine..ctor(EngineSettings settings)
   at LiteDB.ConnectionString.CreateEngine()
   at LiteDB.LiteDatabase..ctor(ConnectionString connectionString, BsonMapper mapper)
   at LiteDB.LiteDatabase..ctor(String connectionString, BsonMapper mapper)
   at ConsoleApp1.Program.Main(String[] args) in 

My DB does not have any passwords set.

lbnascimento commented 4 years ago

@alex-fomin Could you provide your datafile and the connection string you're using?

alex-fomin commented 4 years ago

The datafile is pretty big (73Mb): https://www.dropbox.com/s/5agkno8ikr2mb2t/ProcessingQueue.db?dl=0 Connection string is just "ProcessingQueue.db"

lbnascimento commented 4 years ago

@alex-fomin The file you sent appears to be a SQLite datafile.

malilevi1 commented 4 years ago

Ok, I found a bug in ConnectionString parser, when finalize with '. I'm fixing.... for now, just remove open/close '

I believe the same issue is with $ character. While interpolating some strings I mistyped the name of the DB and got the same error. Oh, and in my case, I have no password on the DB.

System.ArgumentOutOfRangeException: Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
Parameter name: ticks
   at System.DateTime..ctor(Int64 ticks, DateTimeKind kind)
   at LiteDB.BufferSliceExtensions.ReadDateTime(BufferSlice buffer, Int32 offset)
   at LiteDB.Engine.LiteEngine..ctor(EngineSettings settings)
   at LiteDB.ConnectionString.CreateEngine()
   at LiteDB.LiteDatabase..ctor(ConnectionString connectionString, BsonMapper mapper)
alex-nicholas commented 3 years ago

I have a similar scenario.

System.ArgumentOutOfRangeException: Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks at System.DateTime..ctor(Int64 ticks, DateTimeKind kind) at LiteDB.BufferSliceExtensions.ReadDateTime(BufferSlice buffer, Int32 offset) at LiteDB.Engine.LiteEngine..ctor(EngineSettings settings) at LiteDB.SharedEngine.OpenDatabase() at LiteDB.SharedEngine.Pragma(String name, BsonValue value)

DB is located in C:\Users\\AppData\Roaming\\\Autosave.db DB is password protected - 16 characters, Alpha-Numerics only - upper and lower case. DB connection mode is shared. The full connection string is: (Identifiers removed - but can guarantee there are no unusual characters or symbols at all) Filename=C:\users\\appdata\roaming\\\autosave.db;Password=;Connection=shared No surrounding quotation marks, no internal quotation marks, symbols, irregular characters etc. The Connection string works fine on over 100 user machines since the version was released containing LiteDb a week ago.

The user in question installed the application, and on the first-run - the DB was generated, not long after opening the app, the app performs a GetAll on a collection in the DB - the GetAll call triggered the exception.

I have a copy of the DB and I get the same issue if I try to use my app to access the data. I also get the issue if I try to connect to the DB using LiteDb Studio.

For the user - deleting the DB that was originally created and letting the app create a new one resolved the issue. @lbnascimento - would the db be helpful to you at all in debugging the issue?

hmi95 commented 1 year ago

I have the same problem described by @alex-nicholas Is there a solution?