Discussion:
memcpy_s destination buffer size?
(too old to reply)
Starglider 4
2010-01-30 21:18:50 UTC
Permalink
Hi,

I integrated some old code into my engine to load and display
3ds models. The auxDIBImageload function was depricated,
so I injected LoadBitmapFile() to replace it, leaving
everything else intact. Here's the code:

AUX_RGBImageRec *TextureImage[1];
// Set the pointer to NULL
memset(TextureImage,0,sizeof(void *)*1);

// Load the bitmap and assign our pointer to it
// TextureImage[0] = auxDIBImageLoad(name);
BITMAPINFOHEADER *bih;
unsigned char* image;
image=LoadBitmapFile(name,bih);
TextureImage[0]->sizeX=bih->biWidth;
TextureImage[0]->sizeY=bih->biHeight;
int s=bih->biSizeImage;
memcpy_s(TextureImage[0]+sizeof(GLint)*2,s,image,s);

I'm not sure what to do with the dest. buffer size (s).

Please help me out.

//M
Igor Tandetnik
2010-01-30 21:41:53 UTC
Permalink
Post by Starglider 4
I integrated some old code into my engine to load and display
3ds models. The auxDIBImageload function was depricated,
so I injected LoadBitmapFile() to replace it, leaving
AUX_RGBImageRec *TextureImage[1];
// Set the pointer to NULL
memset(TextureImage,0,sizeof(void *)*1);
Why not simply

TextureImage[0] = NULL;
Post by Starglider 4
// Load the bitmap and assign our pointer to it
// TextureImage[0] = auxDIBImageLoad(name);
BITMAPINFOHEADER *bih;
unsigned char* image;
image=LoadBitmapFile(name,bih);
TextureImage[0]->sizeX=bih->biWidth;
TextureImage[0]->sizeY=bih->biHeight;
int s=bih->biSizeImage;
memcpy_s(TextureImage[0]+sizeof(GLint)*2,s,image,s);
I'm not sure what to do with the dest. buffer size (s).
You specify how much memory you've allocated for the buffer pointed to by TextureImage[0], minus sizeof(GLint)*2. As far as I can tell, you haven't allocated anything: TextureImage[0] is still NULL, and the program should crash at the assignment to TextureImage[0]->sizeX.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Starglider 4
2010-01-31 01:16:57 UTC
Permalink
What's wrong here?

int _2i=sizeof(int)*2;

// Create a place to store the texture
AUX_RGBImageRec *TextureImage[1];
TextureImage[0]=(AUX_RGBImageRec*)malloc(1000*1000*3+_2i);

// Load the bitmap and assign our pointer to it
// TextureImage[0] = auxDIBImageLoad(name);
BITMAPINFOHEADER
*bih=(BITMAPINFOHEADER*)malloc(sizeof(BITMAPINFOHEADER));//
char* image;//
image=LoadBitmapFile(name,bih);
TextureImage[0]->sizeX=bih->biWidth;
TextureImage[0]->sizeY=bih->biHeight;
int s=bih->biSizeImage;
memcpy_s(TextureImage[0]+_2i,1000*1000*3,image,s);

MK
Post by Starglider 4
I integrated some old code into my engine to load and display
3ds models. The auxDIBImageload function was depricated,
so I injected LoadBitmapFile() to replace it, leaving
AUX_RGBImageRec *TextureImage[1];
// Set the pointer to NULL
memset(TextureImage,0,sizeof(void *)*1);
Why not simply

TextureImage[0] = NULL;
Post by Starglider 4
// Load the bitmap and assign our pointer to it
// TextureImage[0] = auxDIBImageLoad(name);
BITMAPINFOHEADER *bih;
unsigned char* image;
image=LoadBitmapFile(name,bih);
TextureImage[0]->sizeX=bih->biWidth;
TextureImage[0]->sizeY=bih->biHeight;
int s=bih->biSizeImage;
memcpy_s(TextureImage[0]+sizeof(GLint)*2,s,image,s);
I'm not sure what to do with the dest. buffer size (s).
You specify how much memory you've allocated for the buffer pointed to by
TextureImage[0], minus sizeof(GLint)*2. As far as I can tell, you haven't
allocated anything: TextureImage[0] is still NULL, and the program should
crash at the assignment to TextureImage[0]->sizeX.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily
a good idea. It is hard to be sure where they are going to land, and it
could be dangerous sitting under them as they fly overhead. -- RFC 1925
Igor Tandetnik
2010-01-31 03:34:36 UTC
Permalink
Post by Starglider 4
What's wrong here?
AUX_RGBImageRec *TextureImage[1];
TextureImage[0]=(AUX_RGBImageRec*)malloc(1000*1000*3+_2i);
memcpy_s(TextureImage[0]+_2i,1000*1000*3,image,s);
Since TextureImage[0] is of type AUX_RGBImageRec*, TextureImage[0]+_2i is at offset _2i*sizeof(AUX_RGBImageRec) bytes from the beginning of the buffer, not at _2i bytes.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Loading...