Skip to content

Commit 5f4246e

Browse files
CyanoxygenKexyBiscuit
authored andcommitted
Fix memory leak and other optimization
1 parent c57c2ce commit 5f4246e

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

AOSCOSLauncher/AOSCOSLauncher.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#define ARG_INSTALL_ROOT L"--root"
1313
#define ARG_RUN L"run"
1414
#define ARG_RUN_C L"-c"
15-
#define CMD_BUF_MAX_LENGTH (LOCALE_NAME_MAX_LENGTH * 4)
15+
#define CMD_BUF_MAX_LENGTH (LOCALE_NAME_MAX_LENGTH + 38)
1616
#define CMD_AOSC_SET_LOCALE L"echo \"LANG=%s.UTF-8\" > /etc/locale.conf"
1717

1818
// Helper class for calling WSL Functions:
@@ -52,7 +52,7 @@ HRESULT InstallDistribution(bool createUser)
5252
}
5353

5454
// Set container locale according to Windows system locale
55-
LPWSTR command = (LPWSTR)malloc(CMD_BUF_MAX_LENGTH);
55+
wchar_t command[CMD_BUF_MAX_LENGTH];
5656
if (command == nullptr) {
5757
return E_FAIL;
5858
}
@@ -111,11 +111,7 @@ static HRESULT GenerateLocaleCommand(LPWSTR command_buf)
111111
HRESULT hr = ERROR_SUCCESS;
112112

113113
// Get a buffer which stores locale information.
114-
LPWSTR locale = (LPWSTR)malloc(LOCALE_NAME_MAX_LENGTH * 2);
115-
if (locale == nullptr) {
116-
// Failed to get a buffer, we have nothing to do but bailing out.
117-
return CO_E_INIT_MEMORY_ALLOCATOR;
118-
}
114+
wchar_t locale[LOCALE_NAME_MAX_LENGTH];
119115
result = GetUserDefaultLocaleName(locale, LOCALE_NAME_MAX_LENGTH);
120116
if (result <= 0) {
121117
// GetUserDefaultLocaleName() returns the string length of the locale name.
@@ -126,12 +122,10 @@ static HRESULT GenerateLocaleCommand(LPWSTR command_buf)
126122
}
127123
Helpers::PrintMessage(MSG_LOCALE_ACQUIRSION_FAILURE);
128124
}
129-
int i = 0;
130-
while (locale[i] != L'\0' || i < CMD_BUF_MAX_LENGTH) {
125+
for (int i = 0; locale[i] != L'\0' || i < LOCALE_NAME_MAX_LENGTH; i++) {
131126
if (locale[i] == L'-') {
132127
locale[i] = L'_';
133128
}
134-
i += 1;
135129
}
136130
hr = StringCchPrintfW(command_buf, CMD_BUF_MAX_LENGTH, CMD_AOSC_SET_LOCALE, locale);
137131
if (FAILED(hr)) {

0 commit comments

Comments
 (0)