memory corrupt when load invalid image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
jstph
Posts: 31
Joined: 2011-01-27T10:07:43-07:00
Authentication code: 8675308

memory corrupt when load invalid image

Post 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;
}
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: memory corrupt when load invalid image

Post 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();
jstph
Posts: 31
Joined: 2011-01-27T10:07:43-07:00
Authentication code: 8675308

Re: memory corrupt when load invalid image

Post 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.
Post Reply