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) {
size_t size = ftell(file);
rewind(file);
char* chars = new char[size + 1];
chars[size] = '\0';
std::unique_ptr<char> chars(new char[size + 1]);
chars.get()[size] = '\0';
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)) {
fclose(file);
return MaybeLocal<String>();
......@@ -661,8 +661,7 @@ MaybeLocal<String> ReadFile(Isolate* isolate, const string& name) {
}
fclose(file);
MaybeLocal<String> result = String::NewFromUtf8(
isolate, chars, NewStringType::kNormal, static_cast<int>(size));
delete[] chars;
isolate, chars.get(), NewStringType::kNormal, static_cast<int>(size));
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