1- // ----------------------------------------------------------------------------------
1+ // ----------------------------------------------------------------------------------
22//
33// Copyright Microsoft Corporation
44// Licensed under the Apache License, Version 2.0 (the "License");
@@ -281,8 +281,11 @@ private static bool IsReservedFileName(string fileName)
281281
282282 private string ResolveFileNameConflict ( string baseFileName )
283283 {
284- // TODO - MaxFileNameLength could be <= 0.
285284 int maxFileNameLength = this . getMaxFileNameLength ( ) ;
285+ if ( maxFileNameLength <= 0 )
286+ {
287+ throw new ArgumentOutOfRangeException ( nameof ( maxFileNameLength ) , "Max file name length must be greater than zero." ) ;
288+ }
286289
287290 Func < string , bool > conflict = delegate ( string fileName )
288291 {
@@ -295,12 +298,16 @@ private string ResolveFileNameConflict(string baseFileName)
295298 {
296299 string postfixString = string . Format ( " ({0})" , count ) ;
297300
298- // TODO - trimLength could be be larger than pathAndFilename.Length, what do we do in this case?
299301 int trimLength = ( fileName . Length + postfixString . Length + extension . Length ) - maxFileNameLength ;
300302
301303 if ( trimLength > 0 )
302304 {
303- fileName = fileName . Remove ( fileName . Length - trimLength ) ;
305+ int startIndex = fileName . Length - trimLength ;
306+ if ( startIndex < 0 )
307+ {
308+ startIndex = 0 ;
309+ }
310+ fileName = fileName . Remove ( startIndex ) ;
304311 }
305312
306313 return string . Format ( "{0}{1}{2}" , fileName , postfixString , extension ) ;
0 commit comments