From b3fdbe2324e98a198cdf633bef038f9bbfa13699 Mon Sep 17 00:00:00 2001 From: Oz Date: Sun, 1 Dec 2024 12:20:46 +0100 Subject: [PATCH] Fix error reporting in MSVC 2015 --- src/bit7zlibrary.cpp | 9 ++++++--- src/internal/cfileinstream.cpp | 4 ++-- src/internal/cfileoutstream.cpp | 4 ++-- src/internal/fsitem.cpp | 3 ++- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/bit7zlibrary.cpp b/src/bit7zlibrary.cpp index 0e14757c..0c6fcdf8 100644 --- a/src/bit7zlibrary.cpp +++ b/src/bit7zlibrary.cpp @@ -35,14 +35,16 @@ using namespace bit7z; Bit7zLibrary::Bit7zLibrary( const tstring& libraryPath ) : mLibrary( Bit7zLoadLibrary( libraryPath ) ) { if ( mLibrary == nullptr ) { - throw BitException( "Failed to load the 7-zip library", ERROR_CODE( std::errc::bad_file_descriptor ) ); + const auto error = ERROR_CODE( std::errc::bad_file_descriptor ); + throw BitException( "Failed to load the 7-zip library", error ); } mCreateObjectFunc = GetProcAddress( mLibrary, "CreateObject" ); if ( mCreateObjectFunc == nullptr ) { FreeLibrary( mLibrary ); - throw BitException( "Failed to get CreateObject function", ERROR_CODE( std::errc::invalid_seek ) ); + const auto error = ERROR_CODE( std::errc::invalid_seek ); + throw BitException( "Failed to get CreateObject function", error ); } } @@ -56,7 +58,8 @@ void Bit7zLibrary::setLargePageMode() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) auto pSetLargePageMode = reinterpret_cast< SetLargePageMode >( GetProcAddress( mLibrary, "SetLargePageMode" ) ); if ( pSetLargePageMode == nullptr ) { - throw BitException( "Failed to get SetLargePageMode function", ERROR_CODE( std::errc::invalid_seek ) ); + const auto error = ERROR_CODE( std::errc::invalid_seek ); + throw BitException( "Failed to get SetLargePageMode function", error ); } const HRESULT res = pSetLargePageMode(); if ( res != S_OK ) { diff --git a/src/internal/cfileinstream.cpp b/src/internal/cfileinstream.cpp index 431448e5..374e1d27 100644 --- a/src/internal/cfileinstream.cpp +++ b/src/internal/cfileinstream.cpp @@ -33,10 +33,10 @@ void CFileInStream::openFile( const fs::path& filePath ) { if ( mFileStream.fail() ) { #if defined( __MINGW32__ ) || defined( __MINGW64__ ) std::error_code error{ errno, std::generic_category() }; - throw BitException( "Failed to open the archive file", error, path_to_tstring( filePath ) ); #else - throw BitException( "Failed to open the archive file", last_error_code(), path_to_tstring( filePath ) ); + const auto error = last_error_code(); #endif + throw BitException( "Failed to open the archive file", error, path_to_tstring( filePath ) ); } } diff --git a/src/internal/cfileoutstream.cpp b/src/internal/cfileoutstream.cpp index f9c3ff65..48d9340e 100644 --- a/src/internal/cfileoutstream.cpp +++ b/src/internal/cfileoutstream.cpp @@ -37,10 +37,10 @@ CFileOutStream::CFileOutStream( fs::path filePath, bool createAlways ) if ( mFileStream.fail() ) { #if defined( __MINGW32__ ) || defined( __MINGW64__ ) error = std::error_code{ errno, std::generic_category() }; - throw BitException( "Failed to open the output file", error, path_to_tstring( mFilePath ) ); #else - throw BitException( "Failed to open the output file", last_error_code(), path_to_tstring( mFilePath ) ); + error = last_error_code(); #endif + throw BitException( "Failed to open the output file", error, path_to_tstring( mFilePath ) ); } // Unbuffered streams are slow for Visual Studio 2015 #if !defined(_MSC_VER) || _MSC_VER != 1900 diff --git a/src/internal/fsitem.cpp b/src/internal/fsitem.cpp index d7fa9cd7..094f8d7c 100644 --- a/src/internal/fsitem.cpp +++ b/src/internal/fsitem.cpp @@ -62,7 +62,8 @@ FilesystemItem::FilesystemItem( fs::directory_entry entry, const fs::path& searc void FilesystemItem::initAttributes( const fs::path& itemPath ) { if ( !fsutil::get_file_attributes_ex( itemPath.c_str(), mSymlinkPolicy, mFileAttributeData ) ) { //should not happen, but anyway... - throw BitException( "Could not retrieve file attributes", last_error_code(), path_to_tstring( itemPath ) ); + const auto error = last_error_code(); + throw BitException( "Could not retrieve file attributes", error, path_to_tstring( itemPath ) ); } }