Page 1 of 1

memory corrupt when load invalid image

Posted: 2011-01-27T12:33:55-07:00
by jstph
Hi,
I try to load a memory image by using BlobToImage. It causes access violation error when loading an invalid image. I wonder if I missed image format checking or something. Any help will be greatly appreciated.

my env:
ImageMagick version: 6.6.7
Build in MS VS 10.
Running under win 2008 R2

From debug, it looks like the problem is at ThrowException when call p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
error message is: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

The following is my test case. The logo.esp file is from the Image fold of ImageMagick source package.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include "magick/MagickCore.h"

int main(int argc, char** argv)
{
	  FILE* file = NULL;
          //load a bogus file 
	  if( fopen_s( &file, "logo.eps", "rb" ) != 0)
          {
			printf("load file error");
			exit(1);
	  }

		fseek(file, 0, SEEK_END);

		size_t length = ftell(file);

		fseek(file, 0, SEEK_SET);

		BYTE* databyte = new BYTE[length];

		int rlength = fread(databyte, 1, length, file);

                Image* image = NULL;
                ImageInfo *image_info;
                ExceptionInfo exception;

                image_info = CloneImageInfo(NULL);

               if (!image_info)
               {
                    return 0;
               }

		try
		{
			//image = PingBlob(image_info, databyte, length , &exception);
			image = BlobToImage(image_info, databyte, length, &exception);
		}
		catch(...)
		{
			return 1;
		}

		delete [] databyte;
		fclose(file);

		if(image != NULL)
		{
			DestroyImage(image);
		}
                return 0;
}

Re: memory corrupt when load invalid image

Posted: 2011-01-27T12:49:10-07:00
by magick
You did not initialize the exception variable. Use:

Code: Select all

  ExceptionInfo
    *exception;

  ImageInfo
    *image_info;

  MagickBooleanType
    status;

  MagickCoreGenesis(*argv,MagickTrue);
  exception=AcquireExceptionInfo();
  image_info=AcquireImageInfo();
  // your custom code here
  image_info=DestroyImageInfo(image_info);
  exception=DestroyExceptionInfo(exception);
  MagickCoreTerminus();

Re: memory corrupt when load invalid image

Posted: 2011-01-27T12:57:37-07:00
by jstph
Thank you very much.
It is the problem. I thought I need to initialize exception, but couldn't find any sample code. I am new to ImageMagick. Do you know if there is any code example or reference I can read ?
Again, thank you for your help.