quamotion / AS.TurboJpegWrapper

Libjpeg-Turbo wrapper for .Net - Forked from https://bitbucket.org/Sergey_Terekhin/as.turbojpegwrapper
MIT License
37 stars 28 forks source link

Internal exception occurs when decompressing a truncated JPG #17

Closed erikcturner closed 5 years ago

erikcturner commented 5 years ago

A truncated JPEG image presented to the Decompress method causes the .NET application to crash with error code 0xC0000374 (Heap Corruption).

When using the command line tool associated with libjpegturbo (djpeg.exe), the crash does not occur but you do get an error message "Premature end of JPEG file".

Updated: The problem seems to be in GetErrorAndThrow when calling TjGetErrorStr. Updated: The following code seems to fix the issue:

TurboJpegImport.cs

[DllImport(UnmanagedLibrary, CallingConvention = CallingConvention.Cdecl, 
                 CharSet = CharSet.Ansi, EntryPoint = "tjGetErrorStr")]
public static extern IntPtr TjGetErrorStr();

TjUtils.cs

public static void GetErrorAndThrow()
{
    var errorPtr = TurboJpegImport.TjGetErrorStr();
    var errorMessage = Marshal.PtrToStringAnsi(errorPtr);
    throw new TJException(errorMessage);
}

OS: Windows 10 IDE: VS2015 TurboJpegWrapper Version: 1.5.55 .NET 4.5 (not Core and not Standard)