C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 20th, 2011, 04:33 PM
lamb0 lamb0 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 2 lamb0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 33 sec
Reputation Power: 0
<bad ptr> please help

Before I post my syntax i'll give a brief idea of whats going on. A string is being passed in, I must take this string and make it as large as the maxStrLen variable that is being passed in. The string is generally 26 characters and the maxStrLen is generally 80 characters I must add trailing spaces to the string to make it a length of 80 characters.
------------------------------------------------------------
int edit(char *str, int row, int col, int fieldLength, int maxStrLength, bool* insertMode, int* strOffset, int* curPosition)
{
//in debugging mode on vs2010 this is <bad ptr>
char* newStr = new char[maxStrLength];

/*before this loop newStr already has a value of
newStr 0x003d3a90 "ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝύύύύ««««««««ξώξώ" char *
*/

for (int i=0; i<maxStrLength; i++){
if (i < strlen(str)){
newStr[i] = str[i];
}else
newStr[i] = ' ';
}

/*after loop I get
newStr 0x003d3a90
"abcdefghijklmnopqrstuvwxyz ύύύύ««««««««ξώξώ" char *

abcdefgh..z is the string being passed in
*/

EDIT* the new value of newStr has a bunch of spaces before ύύύύ««««««««ξώξώ
-------------------------------------------------------

Sorry if the indentation looks very weird it is hard to tell in this little post box. I would appreciate if someone could tell me why my pointer is already assigned a value.

Reply With Quote
  #2  
Old September 20th, 2011, 04:51 PM
lamb0 lamb0 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 2 lamb0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 33 sec
Reputation Power: 0
Ok I think i am on to something here. I think its buffer overflow. Before the main calls this function edit it calls my function display which does something similar with the string.

void display(const char* str, int row, int col, int fieldLen){
int stringSize = strlen(str); //length of the string passed through
int maxRow = console.getRows(); //max rows of console
int maxCol= console.getCols(); //max columns of console

console.setPosition(row, col); //set the starting position of cursor

if (fieldLen <= 0){
if (stringSize <= maxCol){
console << str;
console.setPosition(row, maxCol);
}else{
console << str;
console.setPosition(row, maxCol);
}
}else if(stringSize > fieldLen){
console << str;
console.setPosition(row, maxCol);
}else if(stringSize < fieldLen){
//right here is the memory allocation
char* newStr= new char[maxCol];

strcpy(newStr, str);
int addSpaces = (maxCol - col) - stringSize; //The amount of trailing spaces that need to be added to string.
//calculation is the max columns in console subtracted by the starting column
//subtracted by the string size


for (int i=0; i<addSpaces; i++){
strcat(newStr, " ");
}

console << newStr;
//I tryed delete [] newStr here and the program crashes

}
//I tryed delete [] newStr here and I get an error saying no variable newStr.


}

----------------------
I think im getting a buffer overflow because I never deleted newStr at the end of this function.

Reply With Quote
  #3  
Old September 23rd, 2011, 06:59 PM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Quote:
Originally Posted by lamb0
//in debugging mode on vs2010 this is <bad ptr>
char* newStr = new char[maxStrLength];
I expect you're seeing that while the arrow is pointing at that line - that'd because the allocation has not yet happened.
Quote:
/*before this loop newStr already has a value of
newStr 0x003d3a90 "..." char *
*/
I would appreciate if someone could tell me why my pointer is already assigned a value.

The allocation basically says "here's some memory can use" - but the contents of that memory is undefined - it may be dependent on whatever was last in that area of memory.

The easiest way to do this would be to
allocate the string,
initialize (fill) it with spaces - since you're already into C string functions, memset()
and copy over the from string - strcpy.

Resource management is still a potential issue though. The usual rule is to have the delete at a corresponding level as the new. Fixing this requires changing the caller, and probably the entire application. Easily circumvented by using standard library strings. Though that would also allow you to merely write s.resize(80,' ') which is perhaps too easy?

I mean, why teach how people should do it, when you can make it really hard and low-level instead?

Quote:
//I tryed delete [] newStr here and I get an error saying no variable newStr.


The variable ceases to exist at the end of the { } block it was defined in.

The crash is probably because you write outside the bounds of the allocated area, indeed. And I think the code is sound except for one thing. It's so common it's got a name, off-by-one. Remember that zero-terminated "c" strings have an extra byte at the end.

Assume col=0, maxcol=8, stringsize=4. You allocate 8 bytes, fill 4 of them. newstr[4] - byte 5 - is now 0 to tell it where the string ends. You then add a space four times, and each time the 0 is moved up, finally ending at newstr[8]. But since the valid area to write in is from newstr[0] to newstr[7] you've just invoked undefined behavior.

Simple solution is, make that allocation new char[maxcol+1]

You're lucky it crashed, alerting you to the fact that something was wrong. Undefined behavior occasionally does nothing unusual until it explodes at a later time, like when you're demoing the program in front of the class, or a multi-million dollar project is depending on it to work flawlessly... If the first byte after the string was a zero from begin with, overwriting it with a zero byte would do nothing. Until it was supposed to be something else.

Quote:
I think im getting a buffer overflow because I never deleted newStr at the end of this function.


If you don't delete, you have a "memory leak". The buffer overflow is the reason why it crashes when you do try to delete - though it may happen when allocating or deleting at a later point instead, since the "heap" corruption is already done.


It made things a lot easier that you set the context before the code.
__________________
Quote:
Programming by Coincidence
Fred types in some more code, tries it, and it still seems to work. [Then] the program suddenly stops working. [...] Fred doesn’t know why the code is failing because he didn’t know why it worked in the first place.
Undefined behavior results in: (worst to best)
-Erases your harddisk. Really.
-Appears to work - for now
-Delayed errors/crashes
-Crashes
-Compiler warning

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > <bad ptr> please help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap