Commit b64afff3 authored by Yang Guo's avatar Yang Guo Committed by Commit Bot

[sample] fix potential memory leak.

R=petermarshall@chromium.org

Bug: v8:7433
Change-Id: Ibeef2c6855b8ffb6076be889a859da513877171f
Reviewed-on: https://chromium-review.googlesource.com/928563
Commit-Queue: Yang Guo <yangguo@chromium.org>
Reviewed-by: 's avatarPeter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51429}
parent a7c78631
...@@ -650,10 +650,10 @@ MaybeLocal<String> ReadFile(Isolate* isolate, const string& name) { ...@@ -650,10 +650,10 @@ MaybeLocal<String> ReadFile(Isolate* isolate, const string& name) {
size_t size = ftell(file); size_t size = ftell(file);
rewind(file); rewind(file);
char* chars = new char[size + 1]; std::unique_ptr<char> chars(new char[size + 1]);
chars[size] = '\0'; chars.get()[size] = '\0';
for (size_t i = 0; i < size;) { for (size_t i = 0; i < size;) {
i += fread(&chars[i], 1, size - i, file); i += fread(&chars.get()[i], 1, size - i, file);
if (ferror(file)) { if (ferror(file)) {
fclose(file); fclose(file);
return MaybeLocal<String>(); return MaybeLocal<String>();
...@@ -661,8 +661,7 @@ MaybeLocal<String> ReadFile(Isolate* isolate, const string& name) { ...@@ -661,8 +661,7 @@ MaybeLocal<String> ReadFile(Isolate* isolate, const string& name) {
} }
fclose(file); fclose(file);
MaybeLocal<String> result = String::NewFromUtf8( MaybeLocal<String> result = String::NewFromUtf8(
isolate, chars, NewStringType::kNormal, static_cast<int>(size)); isolate, chars.get(), NewStringType::kNormal, static_cast<int>(size));
delete[] chars;
return result; return result;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment