Jump to content
Sign in to follow this  
dub

Memory Corruption Of Constant String

Recommended Posts

I've seen the problem on and off on my embedded device and always though it was stack overflow related but last night nailed the problem to BoostC compiler error.

 

Consider call to following function:

 

...

...

lcd_printf("%3d m/sec", speed);

...

...

 

 

Function is defined earlier in code and here is just the starting sequence of the function (note definition of "pi" variable, We will get ot that later).

 

void lcd_printf( char *fmtptr, unsigned int val ) // JS - Accept unsigned by default

{

unsigned char pi = 0, bi, c, fill, baseOrBits, sign, mask;

unsigned char buff[ 16 ]; // max length allowed is 16

bit pad;

 

while( 1 )

{

c = fmtptr[pi++]; if ( !c ) return;

 

.............

............

 

 

Variable fmtptr (string "%3d m/sec" is placed to address 0x0073 (bold in following

memory dump.

 

0x0070 00 01 00 25 33 64 20 6D .

0x0078 2F 73 65 64 00 01 20 00

 

As we progress through the loop debugger clearly shows that

local variable "pi" is stored at the location of NUL character in fmtptr string thus

corrupting output to LCD screen and actually printing non printable character until

first NUL char is encountered.

 

0x0070 00 01 00 25 33 64 20 6D

0x0078 2F 73 65 64 04 01 20 00

 

 

Please contact me if you need assistance in reproducing the bug. It caused me quite a lot of grief over the last week or so.

 

Regards!

 

Dub Bartolec

Share this post


Link to post
Share on other sites

Dub,

 

....

As we progress through the loop debugger clearly shows that

local variable "pi" is stored at the location of NUL character in fmtptr string thus

corrupting output to LCD screen and actually printing non printable character until

first NUL char is encountered.

 

0x0070  00 01 00 25 33 64 20 6D

0x0078  2F 73 65 64 04 01 20 00

It sounds nasty :)

Please send a simple, but complete, project that can be built and run under the SourceBoost IDE debugger and we will investigate further.

support@sourceboost.com

 

Regards

Dave

Share this post


Link to post
Share on other sites

Dub,

 

Thanks for sending the project.

This problem exists in V6.40 and previous versions.

The problem is caused when a temporary string is passed in an inline function eg:

 

void foo2( char *str )
{
.....
.....
}

inline void foo()
{
foo2( "hello" );
}

The string only gets 1 byte allocated to it, so other variables then uses the same memory area causing corruption.

 

Only temporary string data seems to be affected in this way.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Dave and others,

 

Tried the patch, and I am pleased to report that it fixed the problem reported few days ago. Thanks for providing fix so quickly, and keep up the good work.

 

Regards!

 

Dub

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...