Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Avoiding crash with atoi()

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Reak
I post too much
Reputation: 0

Joined: 15 May 2007
Posts: 3496

PostPosted: Fri Apr 25, 2008 3:42 pm    Post subject: [C++] Avoiding crash with atoi() Reply with quote

Hello,

I'm having a problem, I'm coding !warpplayer <username> <mapid> for TitanMS atm.
Here's the code so far:

Code:
if(strcmp(command, "warpplayer") == 0){
      if(strlen(next_token) > 0){
         printf("test2 \n");
         char* name = strtok_s(NULL, " ",&next_token);
            for (hash_map <int, Player*>::iterator iter = Players::players.begin(); iter != Players::players.end(); iter++){
               if(strcmp(iter->second->getName(), name) == 0){
                        printf("test1 \n");
                        char* mapstr = strtok_s(NULL, " ",&next_token);
                        printf("test2 \n");
                        printf("%s \n", mapstr);
                        if(strlen(mapstr) > 0 && mapstr != NULL)
                        {
                           int map = atoi(mapstr);
                           printf("test3 \n");
                           if(Maps::info.find(map) != Maps::info.end())
                              Maps::changeMap(player, map, 0);
                           printf("test4 \n");
                        }
               }
            }
         }
      }


But if someone does only enter "!warpplayer <username>" without the mapid, the server will crash.
It crashes because it tries to convert mapstr (which is NULL then) to an integer by using atoi. But why does it continue even if i check with "if(strlen(mapstr) > 0 && mapstr != NULL)" ?

Help please.
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Fri Apr 25, 2008 3:50 pm    Post subject: Reply with quote

Code:
if( mapstr )
{

}


Try that first see if it still continues.
Back to top
View user's profile Send private message MSN Messenger
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Fri Apr 25, 2008 3:53 pm    Post subject: Reply with quote

Make sure in the beggining that there are 3 tokens? if there is to little or to much have it say some error.
_________________
Back to top
View user's profile Send private message
Reak
I post too much
Reputation: 0

Joined: 15 May 2007
Posts: 3496

PostPosted: Fri Apr 25, 2008 3:58 pm    Post subject: Reply with quote

noz3001 wrote:
Code:
if( mapstr )
{

}


Try that first see if it still continues.


Works perfect, didn't know about this. Thank you!

@lurc: Also thanks to you, but it noz's tip works great.
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Sat Apr 26, 2008 5:16 am    Post subject: Reply with quote

You should not be using atoi, there's your reason :P

I suggest you to use either a stringstream or strtol:
Code:
#include <iostream>
#include <sstream>

int main(int argc, char *argv[])
{
   if( argc < 2)
      return EXIT_FAILURE;

   std::stringstream temp;
   temp << argv[1];
   long int i;
   char *flag;

   if( temp >> i )
      std::cout << "ss: " << i << std::endl;

   i = strtol( argv[1], &flag, 10 );
   if( flag != argv[1] )
      std::cout << "strtol: " << i << std::endl;

   return EXIT_SUCCESS;
}
The number is taken as an argument and it's only printed if the conversion was successful (ie. there's at least one number in the front.)
Back to top
View user's profile Send private message
decheatengine
Cheater
Reputation: 0

Joined: 10 Jun 2007
Posts: 25

PostPosted: Sat Apr 26, 2008 9:07 am    Post subject: Reply with quote

Quote:
if(strlen(mapstr) > 0 && mapstr != NULL)


It crashed because you try to find the length of the NULL. You could try

if (mapstr!=NULL && strlen(mapstr) > 0)

since I believe C will only do the test for both cases if the first test is TRUE. If the first test is FALSE, the second test will not be carry out. They called this short circuit or something. For cases that has || (or), if the first case is TRUE, the subsequent test are skipped.
Back to top
View user's profile Send private message
Reak
I post too much
Reputation: 0

Joined: 15 May 2007
Posts: 3496

PostPosted: Sun Apr 27, 2008 7:46 am    Post subject: Reply with quote

decheatengine wrote:
Quote:
if(strlen(mapstr) > 0 && mapstr != NULL)


It crashed because you try to find the length of the NULL. You could try

if (mapstr!=NULL && strlen(mapstr) > 0)

since I believe C will only do the test for both cases if the first test is TRUE. If the first test is FALSE, the second test will not be carry out. They called this short circuit or something. For cases that has || (or), if the first case is TRUE, the subsequent test are skipped.


Yea I did that, check my main post Very Happy
I got it working already. Thanks anyway Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites