Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Side by Side Diff: runtime/bin/process.cc

Issue 1781883002: Fixes some memory leaks in //runtime/bin (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Comments. Fix leak in Process_Start Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/platform_win.cc ('k') | runtime/bin/process_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "bin/dartutils.h" 5 #include "bin/dartutils.h"
6 #include "bin/io_buffer.h" 6 #include "bin/io_buffer.h"
7 #include "bin/log.h" 7 #include "bin/log.h"
8 #include "bin/platform.h" 8 #include "bin/platform.h"
9 #include "bin/process.h" 9 #include "bin/process.h"
10 #include "bin/socket.h" 10 #include "bin/socket.h"
(...skipping 29 matching lines...) Expand all
40 Dart_PropagateError(result); 40 Dart_PropagateError(result);
41 } 41 }
42 result = DartUtils::SetStringField( 42 result = DartUtils::SetStringField(
43 status_handle, "_errorMessage", "Max argument list length exceeded"); 43 status_handle, "_errorMessage", "Max argument list length exceeded");
44 if (Dart_IsError(result)) { 44 if (Dart_IsError(result)) {
45 Dart_PropagateError(result); 45 Dart_PropagateError(result);
46 } 46 }
47 return NULL; 47 return NULL;
48 } 48 }
49 *length = len; 49 *length = len;
50 char** string_args = new char*[len]; 50 char** string_args;
51 string_args = reinterpret_cast<char**>(
52 Dart_ScopeAllocate(len * sizeof(*string_args)));
51 for (int i = 0; i < len; i++) { 53 for (int i = 0; i < len; i++) {
52 Dart_Handle arg = Dart_ListGetAt(strings, i); 54 Dart_Handle arg = Dart_ListGetAt(strings, i);
53 if (Dart_IsError(arg)) { 55 if (Dart_IsError(arg)) {
54 delete[] string_args;
55 Dart_PropagateError(arg); 56 Dart_PropagateError(arg);
56 } 57 }
57 if (!Dart_IsString(arg)) { 58 if (!Dart_IsString(arg)) {
58 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); 59 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0);
59 if (Dart_IsError(result)) { 60 if (Dart_IsError(result)) {
60 Dart_PropagateError(result); 61 Dart_PropagateError(result);
zra 2016/03/11 23:55:51 leaks string_args.
61 } 62 }
62 result = DartUtils::SetStringField( 63 result = DartUtils::SetStringField(
63 status_handle, "_errorMessage", error_msg); 64 status_handle, "_errorMessage", error_msg);
64 if (Dart_IsError(result)) { 65 if (Dart_IsError(result)) {
65 Dart_PropagateError(result); 66 Dart_PropagateError(result);
zra 2016/03/11 23:55:51 ditto
66 } 67 }
67 delete[] string_args;
68 return NULL; 68 return NULL;
69 } 69 }
70 string_args[i] = const_cast<char *>(DartUtils::GetStringValue(arg)); 70 string_args[i] = const_cast<char *>(DartUtils::GetStringValue(arg));
71 } 71 }
72 return string_args; 72 return string_args;
73 } 73 }
74 74
75 void FUNCTION_NAME(Process_Start)(Dart_NativeArguments args) { 75 void FUNCTION_NAME(Process_Start)(Dart_NativeArguments args) {
76 Dart_Handle process = Dart_GetNativeArgument(args, 0); 76 Dart_Handle process = Dart_GetNativeArgument(args, 0);
77 intptr_t process_stdin; 77 intptr_t process_stdin;
(...skipping 30 matching lines...) Expand all
108 if (string_args == NULL) { 108 if (string_args == NULL) {
109 Dart_SetReturnValue(args, Dart_NewBoolean(false)); 109 Dart_SetReturnValue(args, Dart_NewBoolean(false));
110 return; 110 return;
111 } 111 }
112 Dart_Handle working_directory_handle = Dart_GetNativeArgument(args, 3); 112 Dart_Handle working_directory_handle = Dart_GetNativeArgument(args, 3);
113 // Defaults to the current working directoy. 113 // Defaults to the current working directoy.
114 const char* working_directory = NULL; 114 const char* working_directory = NULL;
115 if (Dart_IsString(working_directory_handle)) { 115 if (Dart_IsString(working_directory_handle)) {
116 working_directory = DartUtils::GetStringValue(working_directory_handle); 116 working_directory = DartUtils::GetStringValue(working_directory_handle);
117 } else if (!Dart_IsNull(working_directory_handle)) { 117 } else if (!Dart_IsNull(working_directory_handle)) {
118 delete[] string_args;
119 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); 118 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0);
120 if (Dart_IsError(result)) { 119 if (Dart_IsError(result)) {
121 Dart_PropagateError(result); 120 Dart_PropagateError(result);
122 } 121 }
123 result = DartUtils::SetStringField( 122 result = DartUtils::SetStringField(
124 status_handle, "_errorMessage", 123 status_handle, "_errorMessage",
125 "WorkingDirectory must be a builtin string"); 124 "WorkingDirectory must be a builtin string");
126 if (Dart_IsError(result)) { 125 if (Dart_IsError(result)) {
127 Dart_PropagateError(result); 126 Dart_PropagateError(result);
128 } 127 }
129 Dart_SetReturnValue(args, Dart_NewBoolean(false)); 128 Dart_SetReturnValue(args, Dart_NewBoolean(false));
130 return; 129 return;
131 } 130 }
132 Dart_Handle environment = Dart_GetNativeArgument(args, 4); 131 Dart_Handle environment = Dart_GetNativeArgument(args, 4);
133 intptr_t environment_length = 0; 132 intptr_t environment_length = 0;
134 char** string_environment = NULL; 133 char** string_environment = NULL;
135 if (!Dart_IsNull(environment)) { 134 if (!Dart_IsNull(environment)) {
136 string_environment = 135 string_environment =
137 ExtractCStringList(environment, 136 ExtractCStringList(environment,
zra 2016/03/11 23:55:51 leaks string_args if it calls Dart_PropagateError.
138 status_handle, 137 status_handle,
139 "Environment values must be builtin strings", 138 "Environment values must be builtin strings",
140 &environment_length); 139 &environment_length);
141 if (string_environment == NULL) { 140 if (string_environment == NULL) {
142 delete[] string_args;
143 Dart_SetReturnValue(args, Dart_NewBoolean(false)); 141 Dart_SetReturnValue(args, Dart_NewBoolean(false));
144 return; 142 return;
145 } 143 }
146 } 144 }
147 int64_t mode = 145 int64_t mode =
148 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 5), 0, 2); 146 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 5), 0, 2);
149 Dart_Handle stdin_handle = Dart_GetNativeArgument(args, 6); 147 Dart_Handle stdin_handle = Dart_GetNativeArgument(args, 6);
150 Dart_Handle stdout_handle = Dart_GetNativeArgument(args, 7); 148 Dart_Handle stdout_handle = Dart_GetNativeArgument(args, 7);
151 Dart_Handle stderr_handle = Dart_GetNativeArgument(args, 8); 149 Dart_Handle stderr_handle = Dart_GetNativeArgument(args, 8);
152 Dart_Handle exit_handle = Dart_GetNativeArgument(args, 9); 150 Dart_Handle exit_handle = Dart_GetNativeArgument(args, 9);
153 intptr_t pid = -1; 151 intptr_t pid = -1;
154 char* os_error_message = NULL; 152 char* os_error_message = NULL; // Scope allocated by Process::Start.
155 153
156 int error_code = Process::Start(path, 154 int error_code = Process::Start(path,
157 string_args, 155 string_args,
158 args_length, 156 args_length,
159 working_directory, 157 working_directory,
160 string_environment, 158 string_environment,
161 environment_length, 159 environment_length,
162 static_cast<ProcessStartMode>(mode), 160 static_cast<ProcessStartMode>(mode),
163 &process_stdout, 161 &process_stdout,
164 &process_stdin, 162 &process_stdin,
165 &process_stderr, 163 &process_stderr,
166 &pid, 164 &pid,
167 &exit_event, 165 &exit_event,
168 &os_error_message); 166 &os_error_message);
169 if (error_code == 0) { 167 if (error_code == 0) {
170 if (mode != kDetached) { 168 if (mode != kDetached) {
171 Socket::SetSocketIdNativeField(stdin_handle, process_stdin); 169 Socket::SetSocketIdNativeField(stdin_handle, process_stdin);
zra 2016/03/11 23:55:51 These may call Dart_PropagateError. If they do the
172 Socket::SetSocketIdNativeField(stdout_handle, process_stdout); 170 Socket::SetSocketIdNativeField(stdout_handle, process_stdout);
173 Socket::SetSocketIdNativeField(stderr_handle, process_stderr); 171 Socket::SetSocketIdNativeField(stderr_handle, process_stderr);
174 } 172 }
175 if (mode == kNormal) { 173 if (mode == kNormal) {
176 Socket::SetSocketIdNativeField(exit_handle, exit_event); 174 Socket::SetSocketIdNativeField(exit_handle, exit_event);
177 } 175 }
178 Process::SetProcessIdNativeField(process, pid); 176 Process::SetProcessIdNativeField(process, pid);
179 } else { 177 } else {
180 result = DartUtils::SetIntegerField( 178 result = DartUtils::SetIntegerField(
181 status_handle, "_errorCode", error_code); 179 status_handle, "_errorCode", error_code);
182 if (Dart_IsError(result)) { 180 if (Dart_IsError(result)) {
183 Dart_PropagateError(result); 181 Dart_PropagateError(result);
zra 2016/03/11 23:55:51 leaks string_args, string_environment, and os_erro
184 } 182 }
185 result = DartUtils::SetStringField( 183 result = DartUtils::SetStringField(
186 status_handle, 184 status_handle,
187 "_errorMessage", 185 "_errorMessage",
188 os_error_message != NULL ? os_error_message 186 os_error_message != NULL ? os_error_message
189 : "Cannot get error message"); 187 : "Cannot get error message");
190 if (Dart_IsError(result)) { 188 if (Dart_IsError(result)) {
191 delete[] string_args;
192 delete[] string_environment;
193 free(os_error_message);
194 Dart_PropagateError(result); 189 Dart_PropagateError(result);
195 } 190 }
196 } 191 }
197 delete[] string_args;
198 delete[] string_environment;
199 free(os_error_message);
200 Dart_SetReturnValue(args, Dart_NewBoolean(error_code == 0)); 192 Dart_SetReturnValue(args, Dart_NewBoolean(error_code == 0));
201 } 193 }
202 194
203 195
204 void FUNCTION_NAME(Process_Wait)(Dart_NativeArguments args) { 196 void FUNCTION_NAME(Process_Wait)(Dart_NativeArguments args) {
205 Dart_Handle process = Dart_GetNativeArgument(args, 0); 197 Dart_Handle process = Dart_GetNativeArgument(args, 0);
206 intptr_t process_stdin = 198 intptr_t process_stdin =
207 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 1)); 199 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 1));
208 intptr_t process_stdout = 200 intptr_t process_stdout =
209 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 2)); 201 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 2));
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 Dart_Handle Process::SetProcessIdNativeField(Dart_Handle process, 308 Dart_Handle Process::SetProcessIdNativeField(Dart_Handle process,
317 intptr_t pid) { 309 intptr_t pid) {
318 return Dart_SetNativeInstanceField(process, kProcessIdNativeField, pid); 310 return Dart_SetNativeInstanceField(process, kProcessIdNativeField, pid);
319 } 311 }
320 312
321 313
322 void FUNCTION_NAME(SystemEncodingToString)(Dart_NativeArguments args) { 314 void FUNCTION_NAME(SystemEncodingToString)(Dart_NativeArguments args) {
323 Dart_Handle bytes = Dart_GetNativeArgument(args, 0); 315 Dart_Handle bytes = Dart_GetNativeArgument(args, 0);
324 intptr_t bytes_length = 0; 316 intptr_t bytes_length = 0;
325 Dart_Handle result = Dart_ListLength(bytes, &bytes_length); 317 Dart_Handle result = Dart_ListLength(bytes, &bytes_length);
326 if (Dart_IsError(result)) Dart_PropagateError(result); 318 if (Dart_IsError(result)) {
327 uint8_t* buffer = 319 Dart_PropagateError(result);
328 reinterpret_cast<uint8_t*>(Dart_ScopeAllocate(bytes_length + 1)); 320 }
321 uint8_t* buffer = Dart_ScopeAllocate(bytes_length + 1);
329 result = Dart_ListGetAsBytes(bytes, 0, buffer, bytes_length); 322 result = Dart_ListGetAsBytes(bytes, 0, buffer, bytes_length);
330 buffer[bytes_length] = '\0'; 323 buffer[bytes_length] = '\0';
331 if (Dart_IsError(result)) Dart_PropagateError(result); 324 if (Dart_IsError(result)) {
325 Dart_PropagateError(result);
326 }
332 intptr_t len; 327 intptr_t len;
333 char* str = 328 char* str = StringUtils::ConsoleStringToUtf8(
334 StringUtils::ConsoleStringToUtf8( 329 reinterpret_cast<char*>(buffer), bytes_length, &len);
335 reinterpret_cast<char*>(buffer),
336 bytes_length,
337 &len);
338 if (str == NULL) { 330 if (str == NULL) {
339 Dart_ThrowException( 331 Dart_ThrowException(
340 DartUtils::NewInternalError("SystemEncodingToString failed")); 332 DartUtils::NewInternalError("SystemEncodingToString failed"));
341 } 333 }
342 result = 334 result =
343 Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(str), len); 335 Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(str), len);
344 free(str);
345 Dart_SetReturnValue(args, result); 336 Dart_SetReturnValue(args, result);
346 } 337 }
347 338
348 339
349 void FUNCTION_NAME(StringToSystemEncoding)(Dart_NativeArguments args) { 340 void FUNCTION_NAME(StringToSystemEncoding)(Dart_NativeArguments args) {
350 Dart_Handle str = Dart_GetNativeArgument(args, 0); 341 Dart_Handle str = Dart_GetNativeArgument(args, 0);
351 char* utf8; 342 char* utf8;
352 intptr_t utf8_len; 343 intptr_t utf8_len;
353 Dart_Handle result = Dart_StringToUTF8( 344 Dart_Handle result = Dart_StringToUTF8(
354 str, reinterpret_cast<uint8_t **>(&utf8), &utf8_len); 345 str, reinterpret_cast<uint8_t **>(&utf8), &utf8_len);
355 if (Dart_IsError(result)) { 346 if (Dart_IsError(result)) {
356 Dart_PropagateError(result); 347 Dart_PropagateError(result);
357 } 348 }
358 intptr_t system_len; 349 intptr_t system_len;
359 const char* system_string = 350 const char* system_string =
360 StringUtils::Utf8ToConsoleString(utf8, utf8_len, &system_len); 351 StringUtils::Utf8ToConsoleString(utf8, utf8_len, &system_len);
361 if (system_string == NULL) { 352 if (system_string == NULL) {
362 Dart_ThrowException( 353 Dart_ThrowException(
363 DartUtils::NewInternalError("StringToSystemEncoding failed")); 354 DartUtils::NewInternalError("StringToSystemEncoding failed"));
364 } 355 }
365 uint8_t* buffer = NULL; 356 uint8_t* buffer = NULL;
366 Dart_Handle external_array = IOBuffer::Allocate(system_len, &buffer); 357 Dart_Handle external_array = IOBuffer::Allocate(system_len, &buffer);
367 if (!Dart_IsError(external_array)) { 358 if (!Dart_IsError(external_array)) {
368 memmove(buffer, system_string, system_len); 359 memmove(buffer, system_string, system_len);
369 } 360 }
370 Dart_SetReturnValue(args, external_array); 361 Dart_SetReturnValue(args, external_array);
371 free(const_cast<char*>(system_string));
372 } 362 }
373 363
374 } // namespace bin 364 } // namespace bin
375 } // namespace dart 365 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/platform_win.cc ('k') | runtime/bin/process_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698