EPPlusSoftware / EPPlus

EPPlus-Excel spreadsheets for .NET
https://epplussoftware.com
Other
1.78k stars 273 forks source link

Stack Buffer Overrun Exception when a worksheet contains a picture #812

Open niclasbb opened 1 year ago

niclasbb commented 1 year ago

I use EPPlus in a web application running in Azure. Version of .NET Framework is 4.8.4526.0.

Accessing worksheets containing drawings of type picture makes the application to chrash.

Crashdump from Azure:

InlinedCallFrame InlinedCallFrame DomainNeutralILStubClass.IL_STUB_PInvoke System.Drawing.Image.FromStream OfficeOpenXml.Drawing.GenericImageHandler.GetImageBounds OfficeOpenXml.ExcelImageSettings.GetImageBounds OfficeOpenXml.Drawing.PictureStore.GetImageBounds OfficeOpenXml.Drawing.PictureStore.AddImage OfficeOpenXml.Drawing.PictureStore.SavePicture OfficeOpenXml.Drawing.ExcelImage.SetImageContainer OfficeOpenXml.Drawing.ExcelPicture..ctor OfficeOpenXml.Drawing.ExcelDrawing.GetDrawingFromNode OfficeOpenXml.Drawing.ExcelDrawings.AddDrawings OfficeOpenXml.Drawing.ExcelDrawings..ctor OfficeOpenXml.ExcelWorksheets.Delete

JanKallman commented 1 year ago

Are you using the latest version of EPPlus?

niclasbb commented 1 year ago

Yes, version 6.1.3

JanKallman commented 1 year ago

Looks like a missing GDI issue. Are you using the free tier in Azure?

hakano14 commented 1 year ago

No - Standard S3 Plan ( 4 vCPU and 7 GB Memory)

JanKallman commented 1 year ago

Does the workbooks open correctly if you run EPPlus locally?

hakano14 commented 1 year ago

Yes - everything works perfect locally and on-premise servers. But crashes direct in Azure App Service (Windows Plan - .Net Framework).

niclasbb commented 1 year ago

If we remove the picture from the worksheet it works as well.

JanKallman commented 1 year ago

It likely crashes here... https://github.com/EPPlusSoftware/EPPlus/blob/c61adffbfb19313b8135eef6c5e12f13d52a0451/src/EPPlus/Drawing/ImageHandling/GenericImageHandler.cs#L57 It's weird as it should catch the Exception and use the build in image handler. Also, if you use Windows it should have GDI available. Is this your setting: image Do you have a more detailed stack trace of the error?

niclasbb commented 1 year ago

Yes that is the correct setting.

This is the file with the image causing the error. I have tried with some other images and this is the only one that fails. Maybe there is a problem with this image?

Offermall_err.xlsx

JanKallman commented 1 year ago

This is an EMF image. I wonder if Azure's GDI implementation does not support EMF's. I will try to see if I can replicate this issue. We have a build in handler for all image types, but it seems that it does not fallback on it when an exception is thrown in this case.

JanKallman commented 1 year ago

Obviously the quick fix for you is to change the image to a png or jpg and see if it works.

niclasbb commented 1 year ago

Yes other image types seem to work.

JanKallman commented 1 year ago

I will remove the usage of System.Drawing.Common in for .NET framework in the GenericImageHandler , so it will use the build-in instead (Note that .NET core, .NET 6 & 7 already uses it). This should solve this issue.