@@ -393,10 +393,7 @@ private static unsafe int Write(
393
393
UIntPtr len ,
394
394
GitObjectType type )
395
395
{
396
- if ( len . ToUInt64 ( ) > long . MaxValue )
397
- {
398
- return ( int ) GitErrorCode . Error ;
399
- }
396
+ long length = ConverToLong ( len ) ;
400
397
401
398
OdbBackend odbBackend = MarshalOdbBackend ( backend ) ;
402
399
if ( odbBackend == null )
@@ -406,9 +403,9 @@ private static unsafe int Write(
406
403
407
404
try
408
405
{
409
- using ( var stream = new UnmanagedMemoryStream ( ( byte * ) data , ( long ) len . ToUInt64 ( ) ) )
406
+ using ( var stream = new UnmanagedMemoryStream ( ( byte * ) data , length ) )
410
407
{
411
- return odbBackend . Write ( new ObjectId ( oid ) , stream , ( long ) len . ToUInt64 ( ) , type . ToObjectType ( ) ) ;
408
+ return odbBackend . Write ( new ObjectId ( oid ) , stream , length , type . ToObjectType ( ) ) ;
412
409
}
413
410
}
414
411
catch ( Exception ex )
@@ -421,15 +418,12 @@ private static unsafe int Write(
421
418
private static int WriteStream (
422
419
out IntPtr stream_out ,
423
420
IntPtr backend ,
424
- UIntPtr length ,
421
+ UIntPtr len ,
425
422
GitObjectType type )
426
423
{
427
424
stream_out = IntPtr . Zero ;
428
425
429
- if ( length . ToUInt64 ( ) > long . MaxValue )
430
- {
431
- return ( int ) GitErrorCode . Error ;
432
- }
426
+ long length = ConverToLong ( len ) ;
433
427
434
428
OdbBackend odbBackend = MarshalOdbBackend ( backend ) ;
435
429
if ( odbBackend == null )
@@ -442,7 +436,7 @@ private static int WriteStream(
442
436
try
443
437
{
444
438
OdbBackendStream stream ;
445
- int toReturn = odbBackend . WriteStream ( ( long ) length . ToUInt64 ( ) , objectType , out stream ) ;
439
+ int toReturn = odbBackend . WriteStream ( length , objectType , out stream ) ;
446
440
447
441
if ( toReturn == 0 )
448
442
{
@@ -578,6 +572,18 @@ private unsafe int CallbackMethod(ObjectId id)
578
572
}
579
573
}
580
574
575
+ internal static long ConverToLong ( UIntPtr len )
576
+ {
577
+ if ( len . ToUInt64 ( ) > long . MaxValue )
578
+ {
579
+ throw new InvalidOperationException (
580
+ string . Format ( "Provided length ({0}) exceeds long.MaxValue ({1})." ,
581
+ len . ToUInt64 ( ) , long . MaxValue ) ) ;
582
+ }
583
+
584
+ return ( long ) len . ToUInt64 ( ) ;
585
+ }
586
+
581
587
/// <summary>
582
588
/// Flags used by subclasses of OdbBackend to indicate which operations they support.
583
589
/// </summary>
0 commit comments