Explorar o código

Fixed DBMap's db_dup_key to allocate only as much as necessary (bugreport:4969).
This reduces default eA's memory usage by about 400kB.


git-svn-id: https://svn.code.sf.net/p/rathena/svn/trunk@14853 54d463be-8e91-2dee-dedb-b68131a5f0ec

ultramage %!s(int64=14) %!d(string=hai) anos
pai
achega
7861584d59
Modificáronse 2 ficheiros con 9 adicións e 8 borrados
  1. 1 0
      Changelog-Trunk.txt
  2. 8 8
      src/common/db.c

+ 1 - 0
Changelog-Trunk.txt

@@ -1,6 +1,7 @@
 Date	Added
 
 2011/06/16
+	* Fixed DBMap's db_dup_key to allocate only as much as necessary (bugreport:4969) [ultramage]
 	* Fixed char-converter not being able to compile due to both char.h being included. (caused by last commit) [FlavioJS]
 	* Merges from charmerge:
 	- Added DBMap::exists. (r14090)

+ 8 - 8
src/common/db.c

@@ -631,19 +631,19 @@ static int db_is_key_null(DBType type, DBKey key)
 static DBKey db_dup_key(DBMap_impl* db, DBKey key)
 {
 	char *str;
+	size_t len;
+	unsigned short maxlen;
 
 	DB_COUNTSTAT(db_dup_key);
 	switch (db->type) {
 		case DB_STRING:
 		case DB_ISTRING:
-			if (db->maxlen) {
-				CREATE(str, char, db->maxlen +1);
-				strncpy(str, key.str, db->maxlen);
-				str[db->maxlen] = '\0';
-				key.str = str;
-			} else {
-				key.str = (char *)aStrdup(key.str);
-			}
+			maxlen = ( db->maxlen != 0 ) ? db->maxlen : UINT16_MAX;
+			len = strnlen(key.str, maxlen);
+			str = (char*)aMalloc(len + 1);
+			memcpy(str, key.str, len);
+			str[len] = '\0';
+			key.str = str;
 			return key;
 
 		default: