View previous topic :: View next topic |
Author |
Message |
Reak I post too much
Reputation: 0
Joined: 15 May 2007 Posts: 3496
|
Posted: Fri Apr 25, 2008 3:42 pm Post subject: [C++] Avoiding crash with atoi() |
|
|
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 |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Fri Apr 25, 2008 3:50 pm Post subject: |
|
|
Try that first see if it still continues. |
|
Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Fri Apr 25, 2008 3:53 pm Post subject: |
|
|
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 |
|
 |
Reak I post too much
Reputation: 0
Joined: 15 May 2007 Posts: 3496
|
Posted: Fri Apr 25, 2008 3:58 pm Post subject: |
|
|
noz3001 wrote: |
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 |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sat Apr 26, 2008 5:16 am Post subject: |
|
|
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 |
|
 |
decheatengine Cheater
Reputation: 0
Joined: 10 Jun 2007 Posts: 25
|
Posted: Sat Apr 26, 2008 9:07 am Post subject: |
|
|
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 |
|
 |
Reak I post too much
Reputation: 0
Joined: 15 May 2007 Posts: 3496
|
Posted: Sun Apr 27, 2008 7:46 am Post subject: |
|
|
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
I got it working already. Thanks anyway  |
|
Back to top |
|
 |
|