From de50fd9aaf01aa3806a09e339fe85263b9ecde28 Mon Sep 17 00:00:00 2001 From: Georges Berenger Date: Sat, 18 Jan 2025 06:10:08 -0800 Subject: [PATCH] Handle invalid files more simply Summary: Let's handle RecordFileReader open issues sequentially, so it's more readable, and handling more strict. Differential Revision: D68348359 fbshipit-source-id: fe89d6a15b6fe52c726c501970a26451892fb765 --- vrs/RecordFileReader.cpp | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/vrs/RecordFileReader.cpp b/vrs/RecordFileReader.cpp index 83d4e797..0d806c5c 100644 --- a/vrs/RecordFileReader.cpp +++ b/vrs/RecordFileReader.cpp @@ -236,24 +236,22 @@ int RecordFileReader::doOpenFile( TelemetryLogger::info(context, "success"); } } - if (error != 0 || file_->getTotalSize() < static_cast(sizeof(FileFormat::FileHeader))) { - if (error != 0) { - XR_LOGE( - "Could not open the file '{}': {}", - fileSpec.getEasyPath(), - errorCodeToMessageWithCode(error)); - } else { - XR_LOGE( - "File '{}' is too small to be a valid VRS file ({} bytes).", - fileSpec.getEasyPath(), - file_->getTotalSize()); - error = NOT_A_VRS_FILE; - } - if (!file_) { - file_ = make_unique(); - } + if (error != 0) { + XR_LOGE( + "Could not open the file '{}': {}", + fileSpec.getEasyPath(), + errorCodeToMessageWithCode(error)); + closeFile(); return error; } + if (file_->getTotalSize() < static_cast(sizeof(FileFormat::FileHeader))) { + XR_LOGE( + "File '{}' is too small to be a valid VRS file ({} bytes).", + fileSpec.getEasyPath(), + file_->getTotalSize()); + closeFile(); + return NOT_A_VRS_FILE; + } TemporaryCachingStrategy temporaryCachingStrategy(file_, CachingStrategy::Passive); FileFormat::FileHeader fileHeader; LOG_PROGRESS(readFileHeader(fileSpec, fileHeader), error, [&]() { @@ -474,7 +472,8 @@ int RecordFileReader::readFileDetails( } int RecordFileReader::closeFile() { - int result = file_->close(); + int result = file_ ? file_->close() : 0; + file_ = make_unique(); if (detailsSaveThread_) { detailsSaveThread_->join(); detailsSaveThread_.reset();