INF : Restore Database UI fails with error: "Length cannot be less than zero. Parameter name: length"

Recently we came across a situation where one of the customer was trying to restore a Database backup using SSMS and while going to Options tab UI failed with the error:

"Length cannot be less than zero. Parameter name: length"

We later found that RESTORE FILELISTONLY of the backup file showed incorrect path to the Database file.

LogicalName PhysicalName        

--------------------------------------------------

Test D:\Test.mdf 

Test_log D:Test_log.ldf     

So as you see, someone has altered the path of the LDF file to D:Test_log.ldf instead of D:\Test_log.ldf. Customer was not aware that who altered the Database file path and how it got reflected in the backup . But then customer was able to restore the same backup file using T-SQL(which is explained later in this section)

Here I wanted to provide the repro steps which will explain on what circumstances we receive this error.

The issue occurs when you have the below conditions true.

1. You have a database with database file on the Root Drive (i.e. C:\ or D:\)

2. You alter the path of database files to another root drive and miss \ character.

3. You altered the Database as READ-ONLY.

4. You took a backup of the Database.

5. You are trying to Restore this backup using SSMS.

Repro Steps:

=============

1. Create a Database . Here I am using the DB name as ‘RestoreCheck’.

--Creating Database

CREATEDATABASE [RestoreCheck] ON PRIMARY

( NAME =N'RestoreCheck',FILENAME=N'D:\RestoreCheck.mdf', SIZE = 3072KB , FILEGROWTH = 1024KB )

LOGON

( NAME =N'RestoreCheck_log',FILENAME=N'D:\RestoreCheck_log.ldf', SIZE = 2048KB , FILEGROWTH = 10%)

GO

2. Alter the Database LDF file path(Notice that the path which I am giving here is incorrect. I am missing a ‘\’ after the drive letter D: )

USEmaster;
GO
ALTERDATABASE [restorecheck]
MODIFYFILE
( NAME ='RestoreCheck_log',FILENAME=N'D:restorecheck_log.ldf'
)
GO

3. Mark the Database as
‘Read Only’

USE [master]
GO
ALTERDATABASE [restorecheck] SET READ_ONLYWITH NO_WAIT
GO

4. Backup the Database and here I am taking a FULL backup.

backupdatabase [restorecheck] todisk='D:\restorecheckfull.bak'

5. Using SQL Server Management Studio, Try to now Restore the backup taken on same server with a different name. Here I am trying to restore the database with name ‘copy_restorecheck’ . When you choose the backup set and click on “Options”, you will encounter the error:

"Length cannot be less than zero. Parameter name: length"

6. Below is the screen shot of the error:

clip_image002

7. We can use the T-SQL Command and restore the backup to resolve the problem, as shown below:

RESTOREDATABASE [copy_restorecheck] FROM

 DISK=N'D:\restorecheckfull.bak'

withrecovery,

  MOVEN'restorecheck'TO

  N'C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\copy_restorecheck.mdf',

   MOVEN'restorecheck_log'TO

   N'C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\copy_restorecheck.ldf',

   NOUNLOAD, STATS= 10

GO

Key Points:

=========

  • Root Drive: As mentioned earlier the above repro is only for the incorrect altered path pointing to database file present in root drive. For example, if the incorrect path of the Database LDF file is altered to D:restorecheck_log.ldf, then during restore from SSMS UI, we will receive the error which is mentioned in screen shot.

    "Length cannot be less than zero. Parameter name: length"

  • READ-ONLY: As mentioned in the Repro steps, the issue occurs only on database which is marked as READ-ONLY after the alter of incorrect path and before taking the backup

 

Regards ,
Durai Murugan

Support Engineer, Microsoft SQL Server Support

Reviewed by:
Balmukund Lakhani
Technical Lead, Microsoft SQL Server Support

Pranab Mazumdar
Support Escalation Engineer, Microsoft SQL Server Support