dlemstra / Magick.NET

The .NET library for ImageMagick
Apache License 2.0
3.46k stars 416 forks source link

Arithmetic operation resulted in an overflow on MagickImage.cs #1477

Closed iam-ziaullah closed 1 month ago

iam-ziaullah commented 1 year ago

Magick.NET version

lts

Environment (Operating system, version and so on)

Win

Description

We are using Magick.Net to process some PDFs and then create tiles of those PDFs. Now the problem occurs whenever we try to process a PDF document that have zoom level of 6400% and the exception that we got is something like at ImageMagick.MagickImageInfo..ctor(String fileName) in /_/src/Magick.NET/MagickImageInfo.cs:line 76 at ImageMagick.MagickImageInfo.Read(String fileName) in /_/src/Magick.NET/MagickImageInfo.cs:line 286 at ImageMagick.MagickImageInfo.Initialize(IMagickImage1 image) in /_/src/Magick.NET/MagickImageInfo.cs:line 297 at ImageMagick.MagickImage.NativeMagickImage.getHeight() in //src/Magick.NET/Native/MagickImage.cs:line 3084 ts=2023-11-09T04:45:44.3708311Z level=err exception=Exception err="System.Exception: General Exception thrown: System.OverflowException: Arithmetic operation resulted in an overflow.`

most probably the problem is with this Method as the variable on line 3070 is Uint and it can store 0 to 4294967295 and it is casted into int which can store -2147483648 to 2147483647

Steps to Reproduce

When we process a pdf document that has a 6400% zoom then we Get Arithmetic Operation. the stack trace is

at ImageMagick.MagickImageInfo..ctor(String fileName) in /_/src/Magick.NET/MagickImageInfo.cs:line 76 at ImageMagick.MagickImageInfo.Read(String fileName) in /_/src/Magick.NET/MagickImageInfo.cs:line 286 at ImageMagick.MagickImageInfo.Initialize(IMagickImage1 image) in /_/src/Magick.NET/MagickImageInfo.cs:line 297 at ImageMagick.MagickImage.NativeMagickImage.getHeight() in //src/Magick.NET/Native/MagickImage.cs:line 3084 ts=2023-11-09T04:45:44.3708311Z level=err exception=Exception err="System.Exception: General Exception thrown: System.OverflowException: Arithmetic operation resulted in an overflow.`

dlemstra commented 1 year ago

Can you please share a file that can be used to reproduce this? I really need something to debug this.

iam-ziaullah commented 1 year ago

You can spin it off this way

using System; public class MagickImageSpinOff { public MagickFormat GetImageFormat(string filePath) { try { var m = new MagickImageInfo(filePath); return m.Format; } catch (ObjectDisposedException ex) { throw new Exception($"ObjectdisposedException thrown: {ex}"); } catch (Exception ex) { throw new Exception($"General Exception thrown: {ex}"); } } } public class HelloWorld { public static void Main(string[] args) { var format = GetImageFormat("Path to a pdf file that have 6400% zoom") } }

dlemstra commented 1 year ago

Thanks for giving a code sample but I really need a file otherwise I cannot reproduce it.

iam-ziaullah commented 1 month ago

This can be closed the problem was with the file