Skip to content

Commit 1c184c5

Browse files
committed
base: Fix File::Close() implem
1 parent 5215e10 commit 1c184c5

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

ortools/base/file.cc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ size_t File::Size() {
6363
bool File::Flush() { return fflush(f_) == 0; }
6464

6565
bool File::Close() {
66-
if (f_ != nullptr && fclose(f_) == 0) {
66+
if (f_ == nullptr) {
67+
return true;
68+
}
69+
70+
if (fclose(f_) == 0) {
6771
f_ = nullptr;
6872
return true;
6973
} else {
@@ -72,15 +76,17 @@ bool File::Close() {
7276
}
7377

7478
absl::Status File::Close(int flags) {
75-
bool ok = true;
76-
if (f_ != nullptr) {
77-
ok = (fclose(f_) == 0);
79+
if (f_ == nullptr) {
80+
return absl::Status();
81+
}
82+
83+
if (fclose(f_) == 0) {
7884
f_ = nullptr;
85+
return absl::Status();
86+
} else {
87+
const std::string msg = absl::StrCat("Could not close file '", name_, "'");
88+
return absl::Status(absl::StatusCode::kInvalidArgument, msg);
7989
}
80-
std::string msg = absl::StrCat("Could not close file '", name_, "'");
81-
delete this;
82-
return ok ? absl::Status()
83-
: absl::Status(absl::StatusCode::kInvalidArgument, msg);
8490
}
8591

8692
void File::ReadOrDie(void* buf, size_t size) {

0 commit comments

Comments
 (0)