Prechádzať zdrojové kódy

Fixed all overflow checks and made the code a bit cleaner from r13322.

git-svn-id: https://svn.code.sf.net/p/rathena/svn/trunk@13331 54d463be-8e91-2dee-dedb-b68131a5f0ec
Paradox924X 16 rokov pred
rodič
commit
0381279f21
3 zmenil súbory, kde vykonal 76 pridanie a 23 odobranie
  1. 1 0
      Changelog-Trunk.txt
  2. 36 10
      src/map/atcommand.c
  3. 39 13
      src/map/charcommand.c

+ 1 - 0
Changelog-Trunk.txt

@@ -4,6 +4,7 @@ AS OF SVN REV. 5091, WE ARE NOW USING TRUNK.  ALL UNTESTED BUGFIXES/FEATURES GO
 IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK.
 
 2008/10/27
+	* Fixed all overflow checks and made the code a bit cleaner from r13322. [Paradox924X]
 	* Removed leftovers of "bLoseSPWhenUnequip" and "bDamageWhenUnequip". [Paradox924X]
 2008/10/24
 	* Added protection from segfault by int overflow in charcommand heal and fixed a typo in atcommand heal. (Follow-up to r13321) [Paradox924X]

+ 36 - 10
src/map/atcommand.c

@@ -1505,7 +1505,7 @@ int atcommand_heal(const int fd, struct map_session_data* sd, const char* comman
 	sscanf(message, "%d %d", &hp, &sp);
 
 	if ( ( hp == 0 && sp == 0 )
-		|| ( hp > 2147483647 || sp > 2147483647 ) ) { // Prevent overflow. [Paradox924X]
+		|| ( hp > INT_MAX && sp > INT_MAX ) ) { // Prevent overflow. [Paradox924X]
 		if (!status_percent_heal(&sd->bl, 100, 100))
 			clif_displaymessage(fd, msg_txt(157)); // HP and SP have already been recovered.
 		else
@@ -1513,14 +1513,40 @@ int atcommand_heal(const int fd, struct map_session_data* sd, const char* comman
 		return 0;
 	}
 
-	if ( hp < -2147483647 || sp < -2147483647 ) { // Prevent overflow. [Paradox924X]
-		status_damage(NULL, &sd->bl, 2147483647, 2147483647, 0, 0);
-		clif_damage(&sd->bl,&sd->bl, gettick(), 0, 0, 2147483647, 0 , 4, 0);
+	// Prevent overflow. [Paradox924X]
+	if ( hp < -INT_MAX && sp < -INT_MAX ) {
+		status_damage(NULL, &sd->bl, INT_MAX, INT_MAX, 0, 0);
+		clif_damage(&sd->bl,&sd->bl, gettick(), 0, 0, INT_MAX, 0, 4, 0);
 		clif_displaymessage(fd, msg_txt(156)); // HP or/and SP modified.
 		return 0;
 	}
 
-	if(hp > 0 && sp >= 0) {
+	// Prevent overflow. [Paradox924X]
+	if ( hp > INT_MAX ) {
+		if (!status_percent_heal(&sd->bl, 100, 0))
+			clif_displaymessage(fd, msg_txt(157)); // HP and SP have already been recovered.
+		else
+			clif_displaymessage(fd, msg_txt(17)); // HP, SP recovered.
+		return 0;
+	} else if ( hp < -INT_MAX ) {
+		status_damage(NULL, &sd->bl, INT_MAX, 0, 0, 0);
+		clif_damage(&sd->bl,&sd->bl, gettick(), 0, 0, INT_MAX, 0, 4, 0);
+		clif_displaymessage(fd, msg_txt(156)); // HP or/and SP modified.
+		return 0;
+	}
+
+	// Prevent overflow. [Paradox924X]
+	if ( sp > INT_MAX ) {
+		status_heal(&sd->bl, 0, INT_MAX, 0);
+		clif_displaymessage(fd, msg_txt(156)); // HP or/and SP modified.
+		return 0;
+	} else if ( sp < -INT_MAX ) {
+		status_damage(NULL, &sd->bl, 0, INT_MAX, 0, 0);
+		clif_displaymessage(fd, msg_txt(156)); // HP or/and SP modified.
+		return 0;
+	}
+
+	if ( hp > 0 && sp >= 0 ) {
 		if(!status_heal(&sd->bl, hp, sp, 0))
 			clif_displaymessage(fd, msg_txt(157)); // HP and SP are already with the good value.
 		else
@@ -1528,24 +1554,24 @@ int atcommand_heal(const int fd, struct map_session_data* sd, const char* comman
 		return 0;
 	}
 
-	if(hp < 0 && sp <= 0) {
+	if ( hp < 0 && sp <= 0 ) {
 		status_damage(NULL, &sd->bl, -hp, -sp, 0, 0);
-		clif_damage(&sd->bl,&sd->bl, gettick(), 0, 0, -hp, 0 , 4, 0);
+		clif_damage(&sd->bl,&sd->bl, gettick(), 0, 0, -hp, 0, 4, 0);
 		clif_displaymessage(fd, msg_txt(156)); // HP or/and SP modified.
 		return 0;
 	}
 
 	//Opposing signs.
-	if (hp) {
+	if ( hp ) {
 		if (hp > 0)
 			status_heal(&sd->bl, hp, 0, 0);
 		else {
 			status_damage(NULL, &sd->bl, -hp, 0, 0, 0);
-			clif_damage(&sd->bl,&sd->bl, gettick(), 0, 0, -hp, 0 , 4, 0);
+			clif_damage(&sd->bl,&sd->bl, gettick(), 0, 0, -hp, 0, 4, 0);
 		}
 	}
 
-	if (sp) {
+	if ( sp ) {
 		if (sp > 0)
 			status_heal(&sd->bl, 0, sp, 0);
 		else

+ 39 - 13
src/map/charcommand.c

@@ -1795,7 +1795,7 @@ int charcommand_heal(const int fd, struct map_session_data* sd, const char* comm
 	}
 
 	if ( ( hp == 0 && sp == 0 )
-		|| ( hp > 2147483647 || sp > 2147483647 ) ) { // Prevent overflow. [Paradox924X]
+		|| ( hp > INT_MAX && sp > INT_MAX ) ) { // Prevent overflow. [Paradox924X]
 		if (!status_percent_heal(&pl_sd->bl, 100, 100))
 			clif_displaymessage(fd, msg_txt(157)); // HP and SP are already with the good value.
 		else
@@ -1807,37 +1807,63 @@ int charcommand_heal(const int fd, struct map_session_data* sd, const char* comm
 		return 0;
 	}
 
-	if ( hp < -2147483647 || sp < -2147483647 ) { // Prevent overflow. [Paradox924X]
-		status_damage(NULL, &pl_sd->bl, 2147483647, 2147483647, 0, 0);
-		clif_damage(&pl_sd->bl,&pl_sd->bl, gettick(), 0, 0, 2147483647, 0 , 4, 0);
+	// Prevent overflow. [Paradox924X]
+	if ( hp < -INT_MAX && sp < -INT_MAX ) {
+		status_damage(NULL, &pl_sd->bl, INT_MAX, INT_MAX, 0, 0);
+		clif_damage(&pl_sd->bl,&pl_sd->bl, gettick(), 0, 0, INT_MAX, 0 , 4, 0);
 		clif_displaymessage(fd, msg_txt(156)); // HP or/and SP modified.
 		return 0;
 	}
-	
+
+	// Prevent overflow. [Paradox924X]
+	if ( hp > INT_MAX ) {
+		if (!status_percent_heal(&pl_sd->bl, 100, 0))
+			clif_displaymessage(fd, msg_txt(157)); // HP and SP have already been recovered.
+		else
+			clif_displaymessage(fd, msg_txt(17)); // HP, SP recovered.
+		return 0;
+	} else if ( hp < -INT_MAX ) {
+		status_damage(NULL, &pl_sd->bl, INT_MAX, 0, 0, 0);
+		clif_damage(&pl_sd->bl,&pl_sd->bl, gettick(), 0, 0, INT_MAX, 0, 4, 0);
+		clif_displaymessage(fd, msg_txt(156)); // HP or/and SP modified.
+		return 0;
+	}
+
+	// Prevent overflow. [Paradox924X]
+	if ( sp > INT_MAX ) {
+		status_heal(&pl_sd->bl, 0, INT_MAX, 0);
+		clif_displaymessage(fd, msg_txt(156)); // HP or/and SP modified.
+		return 0;
+	} else if ( sp < -INT_MAX ) {
+		status_damage(NULL, &pl_sd->bl, 0, INT_MAX, 0, 0);
+		clif_displaymessage(fd, msg_txt(156)); // HP or/and SP modified.
+		return 0;
+	}
+
 	if(hp > 0 && sp >= 0) {
-		if(!status_heal(&pl_sd->bl, hp, sp, 2))
+		if( !status_heal(&pl_sd->bl, hp, sp, 2) )
 			clif_displaymessage(fd, msg_txt(157)); // HP and SP are already with the good value.
 		else
 		{
 			clif_displaymessage(pl_sd->fd, msg_txt(17)); // HP, SP recovered.
-			if (pl_sd->fd != fd)
+			if ( pl_sd->fd != fd )
 				clif_displaymessage(fd, msg_txt(17)); // HP, SP recovered.
 		}
 		return 0;
 	}
 
-	if(hp < 0 && sp <= 0) {
+	if( hp < 0 && sp <= 0 ) {
 		status_damage(NULL, &pl_sd->bl, -hp, -sp, 0, 0);
 		clif_damage(&pl_sd->bl,&pl_sd->bl, gettick(), 0, 0, -hp, 0 , 4, 0);
 		clif_displaymessage(pl_sd->fd, msg_txt(156)); // HP or/and SP modified.
-		if (pl_sd->fd != fd)
+		if ( pl_sd->fd != fd )
 			clif_displaymessage(fd, msg_txt(156)); // HP or/and SP modified.
 		return 0;
 	}
 
 	//Opposing signs.
-	if (hp) {
-		if (hp > 0)
+	if ( hp ) {
+		if ( hp > 0 )
 			status_heal(&pl_sd->bl, hp, 0, 2);
 		else {
 			status_damage(NULL, &pl_sd->bl, -hp, 0, 0, 0);
@@ -1845,8 +1871,8 @@ int charcommand_heal(const int fd, struct map_session_data* sd, const char* comm
 		}
 	}
 
-	if (sp) {
-		if (sp > 0)
+	if ( sp ) {
+		if ( sp > 0 )
 			status_heal(&pl_sd->bl, 0, sp, 2);
 		else
 			status_damage(NULL, &pl_sd->bl, 0, -sp, 0, 0);