Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Why does this small bit of code cause a buffer overrun??

Author
Message
archie456
16
Years of Service
User Offline
Joined: 13th Feb 2008
Location:
Posted: 1st Jul 2008 23:36
Hi,

I've writing a small game, and as an update to it I'm learning how to use memblocks to store an image which I can then query information from.

The code attached load an image, then sets it as a memblock and also sets up a char variable which will eventially display the message.

I've stripped out all other code to narrow down the error.




The variable message is blanked on each cycle of the while loop, it is will then be filled with a text message and display on each pass. (I've stripped this bit out)

Why does this cause a buffer overflow??

Thanks for any help.
monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 2nd Jul 2008 02:40 Edited at: 2nd Jul 2008 13:00
I haven't got GDK installed at the minute so I can't test this but, you are loading an image then making a memblock from a bitmap instead of an image.

So this


Should be this:


Also, to empty the string you could just do this:

Edit: I corrected the above line to prevent any possible confusion, I originally wrote the newline deliminator instead of the null terminator.

My liver is evil, I must kill it!
SunDawg
19
Years of Service
User Offline
Joined: 21st Dec 2004
Location: Massachusetts
Posted: 2nd Jul 2008 05:11 Edited at: 2nd Jul 2008 08:01
Initialize message[200] globally, before your includes.


My site, for various stuff that I make.
monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 2nd Jul 2008 12:08
It is already initialised when it is declared, if it was a pointer to a character array then it would be different.

My liver is evil, I must kill it!
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 2nd Jul 2008 12:33 Edited at: 2nd Jul 2008 12:34
Quote: "Also, to empty the string you could just do this: "


You mean 'message[0] = "\0";' or 'message[0] = 0;'

monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 2nd Jul 2008 12:52 Edited at: 2nd Jul 2008 13:01
Indeed, another way.

message[0] = '\0';
or
message[0] = NULL; // NULL is just a defined value of zero
or
message[0] = 0;


Edit: Yep, just realised the deliberate mistake in my previous post, don't use '\n' use '\0'. Cheers Dark Coder, I haven't had any coffee this morning so the brain is only operating at 10% efficiency.

My liver is evil, I must kill it!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 2nd Jul 2008 18:30
Or, even faster

*message = 0;

Even though message itself isn't a pointer, message does represent an address. From my understanding, using array notation requires taking the base address and adding the index times the element size. Since message[0] just results in the address of the base array you can dereference the address directly.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 2nd Jul 2008 18:48
The compiler handles such things automatically.

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 2nd Jul 2008 18:50
Ah! then I'm taking some stress off the compiler.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
archie456
16
Years of Service
User Offline
Joined: 13th Feb 2008
Location:
Posted: 3rd Jul 2008 00:47
I see - thanks.

Just one question - you said that the problem was that I was making a memblock from an image rather than a bitmap.

Whats the difference - I though a bitmap is an image?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 3rd Jul 2008 00:52 Edited at: 3rd Jul 2008 00:52
Images, bitmaps and sprites can be used interchangeably for the most part(when talking about 2D graphics), but in GDK all 3 are very different, you cannot load a bitmap and use sprite commands on it and same for images and bitmaps.

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 3rd Jul 2008 01:49
Quote: "
Just one question - you said that the problem was that I was making a memblock from an image rather than a bitmap.

Whats the difference - I though a bitmap is an image?"


Wouldn't the function call cause a problem if you say to make a memblock from an image but pass it a bitmap number?

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
monotonic
18
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 3rd Jul 2008 01:57 Edited at: 3rd Jul 2008 02:00
I haven't got GDK installed to test it. However, you would think that calling dbLoadObject and passing a filename that doesn't exist would cause an error but, it doesn't, it just carries on regardless.

One of the bugs/features of GDK.

My liver is evil, I must kill it!

Login to post a reply

Server time is: 2024-09-30 01:40:29
Your offset time is: 2024-09-30 01:40:29