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

Unified Diff: runtime/bin/directory.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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/directory.h ('k') | runtime/bin/directory_android.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/directory.cc
diff --git a/runtime/bin/directory.cc b/runtime/bin/directory.cc
index a28b01cd9852e6ca7a5815ff5e05b2a6fba1c664..3791197cc5ddb0d2936475c166e05ad5317da0f1 100644
--- a/runtime/bin/directory.cc
+++ b/runtime/bin/directory.cc
@@ -13,10 +13,9 @@ namespace dart {
namespace bin {
void FUNCTION_NAME(Directory_Current)(Dart_NativeArguments args) {
- char* current = Directory::Current();
+ const char* current = Directory::Current();
if (current != NULL) {
Dart_SetReturnValue(args, DartUtils::NewString(current));
- free(current);
} else {
Dart_SetReturnValue(args, DartUtils::NewDartOSError());
}
@@ -69,9 +68,8 @@ void FUNCTION_NAME(Directory_Create)(Dart_NativeArguments args) {
void FUNCTION_NAME(Directory_SystemTemp)(
Dart_NativeArguments args) {
- char* result = Directory::SystemTemp();
+ const char* result = Directory::SystemTemp();
Dart_SetReturnValue(args, DartUtils::NewString(result));
- free(result);
}
@@ -82,10 +80,10 @@ void FUNCTION_NAME(Directory_CreateTemp)(Dart_NativeArguments args) {
"Prefix argument of CreateSystemTempSync is not a String"));
return;
}
- char* result = Directory::CreateTemp(DartUtils::GetStringValue(path));
+ const char* result =
+ Directory::CreateTemp(DartUtils::GetStringValue(path));
if (result != NULL) {
Dart_SetReturnValue(args, DartUtils::NewString(result));
- free(result);
} else {
Dart_SetReturnValue(args, DartUtils::NewDartOSError());
}
@@ -187,10 +185,9 @@ CObject* Directory::ExistsRequest(const CObjectArray& request) {
CObject* Directory::CreateTempRequest(const CObjectArray& request) {
if (request.Length() == 1 && request[0]->IsString()) {
CObjectString path(request[0]);
- char* result = Directory::CreateTemp(path.CString());
+ const char* result = Directory::CreateTemp(path.CString());
if (result != NULL) {
CObject* temp_dir = new CObjectString(CObject::NewString(result));
- free(result);
return temp_dir;
} else {
return CObject::NewOSError();
@@ -243,8 +240,7 @@ CObject* Directory::ListStartRequest(const CObjectArray& request) {
CObject* Directory::ListNextRequest(const CObjectArray& request) {
- if (request.Length() == 1 &&
- request[0]->IsIntptr()) {
+ if ((request.Length() == 1) && request[0]->IsIntptr()) {
CObjectIntptr ptr(request[0]);
AsyncDirectoryListing* dir_listing =
reinterpret_cast<AsyncDirectoryListing*>(ptr.Value());
@@ -265,7 +261,7 @@ CObject* Directory::ListNextRequest(const CObjectArray& request) {
CObject* Directory::ListStopRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsIntptr()) {
+ if ((request.Length() == 1) && request[0]->IsIntptr()) {
CObjectIntptr ptr(request[0]);
AsyncDirectoryListing* dir_listing =
reinterpret_cast<AsyncDirectoryListing*>(ptr.Value());
@@ -283,7 +279,9 @@ CObject* Directory::RenameRequest(const CObjectArray& request) {
CObjectString path(request[0]);
CObjectString new_path(request[1]);
bool completed = Directory::Rename(path.CString(), new_path.CString());
- if (completed) return CObject::True();
+ if (completed) {
+ return CObject::True();
+ }
return CObject::NewOSError();
}
return CObject::IllegalArgumentError();
@@ -291,7 +289,7 @@ CObject* Directory::RenameRequest(const CObjectArray& request) {
bool AsyncDirectoryListing::AddFileSystemEntityToResponse(Response type,
- char* arg) {
+ const char* arg) {
array_->SetAt(index_++, new CObjectInt32(CObject::NewInt32(type)));
if (arg != NULL) {
array_->SetAt(index_++, new CObjectString(CObject::NewString(arg)));
@@ -302,37 +300,42 @@ bool AsyncDirectoryListing::AddFileSystemEntityToResponse(Response type,
}
-bool AsyncDirectoryListing::HandleDirectory(char* dir_name) {
+bool AsyncDirectoryListing::HandleDirectory(const char* dir_name) {
return AddFileSystemEntityToResponse(kListDirectory, dir_name);
}
-bool AsyncDirectoryListing::HandleFile(char* file_name) {
+bool AsyncDirectoryListing::HandleFile(const char* file_name) {
return AddFileSystemEntityToResponse(kListFile, file_name);
}
-bool AsyncDirectoryListing::HandleLink(char* link_name) {
+bool AsyncDirectoryListing::HandleLink(const char* link_name) {
return AddFileSystemEntityToResponse(kListLink, link_name);
}
+
void AsyncDirectoryListing::HandleDone() {
AddFileSystemEntityToResponse(kListDone, NULL);
}
-bool AsyncDirectoryListing::HandleError(const char* dir_name) {
+bool AsyncDirectoryListing::HandleError() {
CObject* err = CObject::NewOSError();
array_->SetAt(index_++, new CObjectInt32(CObject::NewInt32(kListError)));
CObjectArray* response = new CObjectArray(CObject::NewArray(3));
response->SetAt(0, new CObjectInt32(CObject::NewInt32(kListError)));
- response->SetAt(1, new CObjectString(CObject::NewString(dir_name)));
+ // Delay calling CurrentPath() until after CObject::NewOSError() in case
+ // CurrentPath() pollutes the OS error code.
+ response->SetAt(1, new CObjectString(CObject::NewString(
+ error() ? "Invalid path" : CurrentPath())));
response->SetAt(2, err);
array_->SetAt(index_++, response);
return index_ < length_;
}
-bool SyncDirectoryListing::HandleDirectory(char* dir_name) {
+
+bool SyncDirectoryListing::HandleDirectory(const char* dir_name) {
Dart_Handle dir_name_dart = DartUtils::NewString(dir_name);
Dart_Handle dir =
Dart_New(directory_type_, Dart_Null(), 1, &dir_name_dart);
@@ -340,7 +343,8 @@ bool SyncDirectoryListing::HandleDirectory(char* dir_name) {
return true;
}
-bool SyncDirectoryListing::HandleLink(char* link_name) {
+
+bool SyncDirectoryListing::HandleLink(const char* link_name) {
Dart_Handle link_name_dart = DartUtils::NewString(link_name);
Dart_Handle link =
Dart_New(link_type_, Dart_Null(), 1, &link_name_dart);
@@ -348,7 +352,8 @@ bool SyncDirectoryListing::HandleLink(char* link_name) {
return true;
}
-bool SyncDirectoryListing::HandleFile(char* file_name) {
+
+bool SyncDirectoryListing::HandleFile(const char* file_name) {
Dart_Handle file_name_dart = DartUtils::NewString(file_name);
Dart_Handle file =
Dart_New(file_type_, Dart_Null(), 1, &file_name_dart);
@@ -356,11 +361,12 @@ bool SyncDirectoryListing::HandleFile(char* file_name) {
return true;
}
-bool SyncDirectoryListing::HandleError(const char* dir_name) {
+
+bool SyncDirectoryListing::HandleError() {
Dart_Handle dart_os_error = DartUtils::NewDartOSError();
Dart_Handle args[3];
args[0] = DartUtils::NewString("Directory listing failed");
- args[1] = DartUtils::NewString(dir_name);
+ args[1] = DartUtils::NewString(error() ? "Invalid path" : CurrentPath());
args[2] = dart_os_error;
Dart_ThrowException(Dart_New(
DartUtils::GetDartType(DartUtils::kIOLibURL, "FileSystemException"),
@@ -386,7 +392,7 @@ static bool ListNext(DirectoryListing* listing) {
return listing->HandleDirectory(listing->CurrentPath());
case kListError:
- return listing->HandleError(listing->CurrentPath());
+ return listing->HandleError();
case kListDone:
listing->Pop();
@@ -403,9 +409,10 @@ static bool ListNext(DirectoryListing* listing) {
return false;
}
+
void Directory::List(DirectoryListing* listing) {
if (listing->error()) {
- listing->HandleError("Invalid path");
+ listing->HandleError();
listing->HandleDone();
} else {
while (ListNext(listing)) {}
« no previous file with comments | « runtime/bin/directory.h ('k') | runtime/bin/directory_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698