Skip to content

Potential file descriptor leak at src/writer.cc #571

@viennadd

Description

@viennadd

Hi,

Our code scanner has reported a file descriptor leak at src/writer.cc#L33,
the file pointer is allocated here: FILE* file = fopen(filename_str.c_str(), "wb");

for any early return, would skip the fclose(file) statement and leak this file descriptor, could any one have a look?

Result OutputBuffer::WriteToFile(string_view filename) const {
  std::string filename_str = filename.to_string();
  FILE* file = fopen(filename_str.c_str(), "wb");
  if (!file) {
    ERROR("unable to open %s for writing\n", filename_str.c_str());
    return Result::Error;
  }

  if (data.empty()) {
    return Result::Ok;     //  <== early return
  }

  ssize_t bytes = fwrite(data.data(), 1, data.size(), file);
  if (bytes < 0 || static_cast<size_t>(bytes) != data.size()) {
    ERROR("failed to write %" PRIzd " bytes to %s\n", data.size(),
          filename_str.c_str());
    return Result::Error;    // <== early return
  }

  fclose(file);
  return Result::Ok;
}

Regards,
SourceBrella Inc., Alex

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions