File tree 2 files changed +16
-0
lines changed
test/HtmlToOpenXml.Tests/ImageFormats
2 files changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,12 @@ public static Size KeepAspectRatio(Size actualSize, Size preferredSize)
119
119
private static FileType DetectFileType ( SequentialBinaryReader reader )
120
120
{
121
121
byte [ ] magicBytes = new byte [ MaxMagicBytesLength ] ;
122
+ var availableBytes = reader . BaseStream . Length - reader . BaseStream . Position ;
123
+ // reasonably, we can assume that if we are at the end of the stream and we read the header,
124
+ // the image content must be invalid or truncated.
125
+ if ( availableBytes < MaxMagicBytesLength )
126
+ return FileType . Unrecognized ;
127
+
122
128
reader . Read ( magicBytes , 0 , MaxMagicBytesLength ) ;
123
129
124
130
var headerSpan = magicBytes . AsSpan ( ) ;
Original file line number Diff line number Diff line change @@ -63,5 +63,15 @@ public ImageHeader.FileType GuessFormat_ReturnsFileType(string resourceName)
63
63
Assert . That ( success , Is . EqualTo ( true ) ) ;
64
64
return guessType ;
65
65
}
66
+
67
+ [ Test ( ExpectedResult = ImageHeader . FileType . Unrecognized ) ]
68
+ public ImageHeader . FileType GuessFormat_WithEmpty_ReturnsFileType ( )
69
+ {
70
+ using var memoryStream = new MemoryStream ( ) ;
71
+ bool success = ImageHeader . TryDetectFileType ( memoryStream , out var guessType ) ;
72
+
73
+ Assert . That ( success , Is . EqualTo ( false ) ) ;
74
+ return guessType ;
75
+ }
66
76
}
67
77
}
You can’t perform that action at this time.
0 commit comments