[Scummvm-git-logs] scummvm master -> 8ac0012fdde8fe0c380ecbff649bc32cff7a2a28

criezy criezy at scummvm.org
Fri Nov 8 21:43:34 CET 2019


This automated email contains information about 2 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
8b586ed418 WIN32: Fix free being used on arrays allocated with new
8ac0012fdd COMMON: Clarify documentation for OSystem::convertEncoding


Commit: 8b586ed4189ed58e43d77f7307d3f2b8f30ea20a
    https://github.com/scummvm/scummvm/commit/8b586ed4189ed58e43d77f7307d3f2b8f30ea20a
Author: Thierry Crozat (criezy at scummvm.org)
Date: 2019-11-08T20:43:25Z

Commit Message:
WIN32: Fix free being used on arrays allocated with new

Changed paths:
    backends/platform/sdl/win32/win32.cpp


diff --git a/backends/platform/sdl/win32/win32.cpp b/backends/platform/sdl/win32/win32.cpp
index e7dc2af..e52525e 100644
--- a/backends/platform/sdl/win32/win32.cpp
+++ b/backends/platform/sdl/win32/win32.cpp
@@ -459,9 +459,6 @@ char *OSystem_Win32::convertEncoding(const char* to, const char *from, const cha
 	}
 
 	// Add ending zeros
-	char *wString = (char *) calloc(sizeof(char), length + 2);
-	memcpy(wString, string, length);
-
 	WCHAR *tmpStr;
 	if (Common::String(from).hasPrefixIgnoreCase("utf-16")) {
 		// Allocate space for string and 2 ending zeros
@@ -474,11 +471,20 @@ char *OSystem_Win32::convertEncoding(const char* to, const char *from, const cha
 		}
 		memcpy(tmpStr, string, length);
 	} else {
-		tmpStr = Win32::ansiToUnicode(string, Win32::getCodePageId(from));
+		// Win32::ansiToUnicode uses new to allocate the memory. We need to copy it into an array
+		// allocated with malloc as it is going to be freed using free.
+		WCHAR *tmpStr2 = Win32::ansiToUnicode(string, Win32::getCodePageId(from));
+		if (!tmpStr2) {
+			if (newString != nullptr)
+				free(newString);
+			return nullptr;
+		}
+		size_t size = wcslen(tmpStr2) + 1; // +1 for the terminating null wchar
+		tmpStr = (WCHAR *) malloc(sizeof(WCHAR) * size);
+		memcpy(tmpStr, tmpStr2, sizeof(WCHAR) * size);
+		delete[] tmpStr2;
 	}
 
-	free(wString);
-
 	if (newString != nullptr)
 		free(newString);
 
@@ -492,7 +498,15 @@ char *OSystem_Win32::convertEncoding(const char* to, const char *from, const cha
 	} else {
 		result = Win32::unicodeToAnsi(tmpStr, Win32::getCodePageId(to));
 		free(tmpStr);
-		return result;
+		if (!result)
+			return nullptr;
+		// Win32::unicodeToAnsi uses new to allocate the memory. We need to copy it into an array
+		// allocated with malloc as it is going to be freed using free.
+		size_t size = strlen(result) + 1;
+		char *resultCopy = (char *) malloc(sizeof(char) * size);
+		memcpy(resultCopy, result, sizeof(char) * size);
+		delete[] result;
+		return resultCopy;
 	}
 }
 


Commit: 8ac0012fdde8fe0c380ecbff649bc32cff7a2a28
    https://github.com/scummvm/scummvm/commit/8ac0012fdde8fe0c380ecbff649bc32cff7a2a28
Author: Thierry Crozat (criezy at scummvm.org)
Date: 2019-11-08T20:43:25Z

Commit Message:
COMMON: Clarify documentation for OSystem::convertEncoding

Changed paths:
    common/system.h


diff --git a/common/system.h b/common/system.h
index 30c2ea9..0b75d90 100644
--- a/common/system.h
+++ b/common/system.h
@@ -1541,8 +1541,8 @@ protected:
 	 * @param string The string that should be converted
 	 * @param length Size of the string in bytes
 	 *
-	 * @return Converted string, which must be freed, or nullptr if the conversion
-	 * isn't possible.
+	 * @return Converted string, which must be freed by the caller (using free()
+	 * and not delete[]), or nullptr if the conversion isn't possible.
 	 */
 	virtual char *convertEncoding(const char *to, const char *from, const char *string, size_t length) { return nullptr; }
 };





More information about the Scummvm-git-logs mailing list