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

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: Fix tests on Windows 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);
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);
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,
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,
(...skipping 16 matching lines...) Expand all
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);
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));
210 intptr_t process_stderr = 202 intptr_t process_stderr =
211 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 3)); 203 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 3));
212 intptr_t exit_event = 204 intptr_t exit_event =
213 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 4)); 205 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 4));
214 ProcessResult result; 206 ProcessResult result;
215 intptr_t pid; 207 intptr_t pid;
216 Process::GetProcessIdNativeField(process, &pid); 208 Process::GetProcessIdNativeField(process, &pid);
217 if (Process::Wait(pid, 209 if (Process::Wait(pid,
218 process_stdin, 210 process_stdin,
219 process_stdout, 211 process_stdout,
220 process_stderr, 212 process_stderr,
221 exit_event, 213 exit_event,
222 &result)) { 214 &result)) {
223 Dart_Handle out = result.stdout_data(); 215 Dart_Handle out = result.stdout_data();
224 if (Dart_IsError(out)) Dart_PropagateError(out); 216 if (Dart_IsError(out)) {
217 Dart_PropagateError(out);
218 }
225 Dart_Handle err = result.stderr_data(); 219 Dart_Handle err = result.stderr_data();
226 if (Dart_IsError(err)) Dart_PropagateError(err); 220 if (Dart_IsError(err)) {
221 Dart_PropagateError(err);
222 }
227 Dart_Handle list = Dart_NewList(4); 223 Dart_Handle list = Dart_NewList(4);
228 Dart_ListSetAt(list, 0, Dart_NewInteger(pid)); 224 Dart_ListSetAt(list, 0, Dart_NewInteger(pid));
229 Dart_ListSetAt(list, 1, Dart_NewInteger(result.exit_code())); 225 Dart_ListSetAt(list, 1, Dart_NewInteger(result.exit_code()));
230 Dart_ListSetAt(list, 2, out); 226 Dart_ListSetAt(list, 2, out);
231 Dart_ListSetAt(list, 3, err); 227 Dart_ListSetAt(list, 3, err);
232 Dart_SetReturnValue(args, list); 228 Dart_SetReturnValue(args, list);
233 } else { 229 } else {
234 Dart_Handle error = DartUtils::NewDartOSError(); 230 Dart_Handle error = DartUtils::NewDartOSError();
235 Process::Kill(pid, 9); 231 Process::Kill(pid, 9);
236 Dart_ThrowException(error); 232 Dart_ThrowException(error);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 Dart_Handle Process::SetProcessIdNativeField(Dart_Handle process, 312 Dart_Handle Process::SetProcessIdNativeField(Dart_Handle process,
317 intptr_t pid) { 313 intptr_t pid) {
318 return Dart_SetNativeInstanceField(process, kProcessIdNativeField, pid); 314 return Dart_SetNativeInstanceField(process, kProcessIdNativeField, pid);
319 } 315 }
320 316
321 317
322 void FUNCTION_NAME(SystemEncodingToString)(Dart_NativeArguments args) { 318 void FUNCTION_NAME(SystemEncodingToString)(Dart_NativeArguments args) {
323 Dart_Handle bytes = Dart_GetNativeArgument(args, 0); 319 Dart_Handle bytes = Dart_GetNativeArgument(args, 0);
324 intptr_t bytes_length = 0; 320 intptr_t bytes_length = 0;
325 Dart_Handle result = Dart_ListLength(bytes, &bytes_length); 321 Dart_Handle result = Dart_ListLength(bytes, &bytes_length);
326 if (Dart_IsError(result)) Dart_PropagateError(result); 322 if (Dart_IsError(result)) {
327 uint8_t* buffer = 323 Dart_PropagateError(result);
328 reinterpret_cast<uint8_t*>(Dart_ScopeAllocate(bytes_length + 1)); 324 }
325 uint8_t* buffer = Dart_ScopeAllocate(bytes_length + 1);
329 result = Dart_ListGetAsBytes(bytes, 0, buffer, bytes_length); 326 result = Dart_ListGetAsBytes(bytes, 0, buffer, bytes_length);
330 buffer[bytes_length] = '\0'; 327 buffer[bytes_length] = '\0';
331 if (Dart_IsError(result)) Dart_PropagateError(result); 328 if (Dart_IsError(result)) {
329 Dart_PropagateError(result);
330 }
332 intptr_t len; 331 intptr_t len;
333 char* str = 332 char* str = StringUtils::ConsoleStringToUtf8(
334 StringUtils::ConsoleStringToUtf8( 333 reinterpret_cast<char*>(buffer), bytes_length, &len);
335 reinterpret_cast<char*>(buffer),
336 bytes_length,
337 &len);
338 if (str == NULL) { 334 if (str == NULL) {
339 Dart_ThrowException( 335 Dart_ThrowException(
340 DartUtils::NewInternalError("SystemEncodingToString failed")); 336 DartUtils::NewInternalError("SystemEncodingToString failed"));
341 } 337 }
342 result = 338 result =
343 Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(str), len); 339 Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(str), len);
344 free(str);
345 Dart_SetReturnValue(args, result); 340 Dart_SetReturnValue(args, result);
346 } 341 }
347 342
348 343
349 void FUNCTION_NAME(StringToSystemEncoding)(Dart_NativeArguments args) { 344 void FUNCTION_NAME(StringToSystemEncoding)(Dart_NativeArguments args) {
350 Dart_Handle str = Dart_GetNativeArgument(args, 0); 345 Dart_Handle str = Dart_GetNativeArgument(args, 0);
351 char* utf8; 346 char* utf8;
352 intptr_t utf8_len; 347 intptr_t utf8_len;
353 Dart_Handle result = Dart_StringToUTF8( 348 Dart_Handle result = Dart_StringToUTF8(
354 str, reinterpret_cast<uint8_t **>(&utf8), &utf8_len); 349 str, reinterpret_cast<uint8_t **>(&utf8), &utf8_len);
355 if (Dart_IsError(result)) { 350 if (Dart_IsError(result)) {
356 Dart_PropagateError(result); 351 Dart_PropagateError(result);
357 } 352 }
358 intptr_t system_len; 353 intptr_t system_len;
359 const char* system_string = 354 const char* system_string =
360 StringUtils::Utf8ToConsoleString(utf8, utf8_len, &system_len); 355 StringUtils::Utf8ToConsoleString(utf8, utf8_len, &system_len);
361 if (system_string == NULL) { 356 if (system_string == NULL) {
362 Dart_ThrowException( 357 Dart_ThrowException(
363 DartUtils::NewInternalError("StringToSystemEncoding failed")); 358 DartUtils::NewInternalError("StringToSystemEncoding failed"));
364 } 359 }
365 uint8_t* buffer = NULL; 360 uint8_t* buffer = NULL;
366 Dart_Handle external_array = IOBuffer::Allocate(system_len, &buffer); 361 Dart_Handle external_array = IOBuffer::Allocate(system_len, &buffer);
367 if (!Dart_IsError(external_array)) { 362 if (!Dart_IsError(external_array)) {
368 memmove(buffer, system_string, system_len); 363 memmove(buffer, system_string, system_len);
369 } 364 }
370 Dart_SetReturnValue(args, external_array); 365 Dart_SetReturnValue(args, external_array);
371 free(const_cast<char*>(system_string));
372 } 366 }
373 367
374 } // namespace bin 368 } // namespace bin
375 } // namespace dart 369 } // 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