@@ -1743,53 +1743,67 @@ bool ImageConverter::apply_crop(
17431743bool ImageConverter::make_output_path (
17441744 std::string &path, const std::string &suffix )
17451745{
1746- std::filesystem::path temp_path ( path );
1746+ // Validate input path
1747+ if ( path.empty () )
1748+ {
1749+ std::cerr << " ERROR: Empty input path provided." << std::endl;
1750+ return false ;
1751+ }
1752+ try
1753+ {
1754+ std::filesystem::path temp_path ( path );
17471755
1748- temp_path.replace_extension ();
1749- temp_path += suffix + " .exr" ;
1756+ temp_path.replace_extension ();
1757+ temp_path += suffix + " .exr" ;
17501758
1751- if ( !settings.output_dir .empty () )
1752- {
1753- auto new_directory = std::filesystem::path ( settings.output_dir );
1759+ if ( !settings.output_dir .empty () )
1760+ {
1761+ auto new_directory = std::filesystem::path ( settings.output_dir );
17541762
1755- auto filename = temp_path.filename ();
1756- auto old_directory = temp_path.remove_filename ();
1763+ auto filename = temp_path.filename ();
1764+ auto old_directory = temp_path.remove_filename ();
17571765
1758- new_directory = old_directory / new_directory;
1766+ new_directory = old_directory / new_directory;
17591767
1760- if ( !std::filesystem::exists ( new_directory ) )
1761- {
1762- if ( settings.create_dirs )
1768+ if ( !std::filesystem::exists ( new_directory ) )
17631769 {
1764- if ( !std::filesystem::create_directory ( new_directory ) )
1770+ if ( settings.create_dirs )
1771+ {
1772+ if ( !std::filesystem::create_directory ( new_directory ) )
1773+ {
1774+ std::cerr << " ERROR: Failed to create directory "
1775+ << new_directory << " ." << std::endl;
1776+ return false ;
1777+ }
1778+ }
1779+ else
17651780 {
1766- std::cerr << " ERROR: Failed to create directory "
1767- << new_directory << " ." << std::endl;
1781+ std::cerr << " ERROR: The output directory " << new_directory
1782+ << " does not exist ." << std::endl;
17681783 return false ;
17691784 }
17701785 }
1771- else
1772- {
1773- std::cerr << " ERROR: The output directory " << new_directory
1774- << " does not exist." << std::endl;
1775- return false ;
1776- }
1786+ temp_path = std::filesystem::absolute ( new_directory / filename );
17771787 }
17781788
1779- temp_path = std::filesystem::absolute ( new_directory / filename );
1780- }
1789+ if ( !settings.overwrite && std::filesystem::exists ( temp_path ) )
1790+ {
1791+ std::cerr
1792+ << " ERROR: file " << temp_path << " already exists. Use "
1793+ << " --overwrite to allow overwriting existing files. Skipping "
1794+ << " this file." << std::endl;
1795+ return false ;
1796+ }
17811797
1782- if ( !settings.overwrite && std::filesystem::exists ( temp_path ) )
1798+ path = temp_path.string ();
1799+ return true ;
1800+ }
1801+ catch ( const std::exception &e )
17831802 {
1784- std::cerr
1785- << " ERROR: file " << temp_path << " already exists. Use "
1786- << " --overwrite to allow overwriting existing files. Skipping "
1787- << " this file." << std::endl;
1803+ std::cerr << " ERROR: Invalid path format '" << path << " ': " << e.what ()
1804+ << std::endl;
17881805 return false ;
17891806 }
1790-
1791- path = temp_path.string ();
1792- return true ;
17931807}
17941808
17951809bool ImageConverter::save_image (
@@ -1823,6 +1837,40 @@ bool ImageConverter::save_image(
18231837
18241838bool ImageConverter::process_image ( const std::string &input_filename )
18251839{
1840+ // Early validation: check if input file exists and is valid
1841+ if ( input_filename.empty () )
1842+ {
1843+ if ( settings.verbosity > 0 )
1844+ {
1845+ std::cerr << " ERROR: Empty input filename provided." << std::endl;
1846+ }
1847+ return false ;
1848+ }
1849+
1850+ // Validate input file exists
1851+ // Wrap in try-catch to handle filesystem exceptions on Windows
1852+ try
1853+ {
1854+ if ( !std::filesystem::exists ( input_filename ) )
1855+ {
1856+ if ( settings.verbosity > 0 )
1857+ {
1858+ std::cerr << " ERROR: Input file does not exist: "
1859+ << input_filename << std::endl;
1860+ }
1861+ return false ;
1862+ }
1863+ }
1864+ catch ( const std::filesystem::filesystem_error &e )
1865+ {
1866+ if ( settings.verbosity > 0 )
1867+ {
1868+ std::cerr << " ERROR: Filesystem error while checking input file '"
1869+ << input_filename << " ': " << e.what () << std::endl;
1870+ }
1871+ return false ;
1872+ }
1873+
18261874 std::string output_filename = input_filename;
18271875 if ( !make_output_path ( output_filename ) )
18281876 {
0 commit comments