@@ -1744,53 +1744,67 @@ bool ImageConverter::apply_crop(
17441744bool ImageConverter::make_output_path (
17451745 std::string &path, const std::string &suffix )
17461746{
1747- std::filesystem::path temp_path ( path );
1747+ // Validate input path
1748+ if ( path.empty () )
1749+ {
1750+ std::cerr << " ERROR: Empty input path provided." << std::endl;
1751+ return false ;
1752+ }
1753+ try
1754+ {
1755+ std::filesystem::path temp_path ( path );
17481756
1749- temp_path.replace_extension ();
1750- temp_path += suffix + " .exr" ;
1757+ temp_path.replace_extension ();
1758+ temp_path += suffix + " .exr" ;
17511759
1752- if ( !settings.output_dir .empty () )
1753- {
1754- auto new_directory = std::filesystem::path ( settings.output_dir );
1760+ if ( !settings.output_dir .empty () )
1761+ {
1762+ auto new_directory = std::filesystem::path ( settings.output_dir );
17551763
1756- auto filename = temp_path.filename ();
1757- auto old_directory = temp_path.remove_filename ();
1764+ auto filename = temp_path.filename ();
1765+ auto old_directory = temp_path.remove_filename ();
17581766
1759- new_directory = old_directory / new_directory;
1767+ new_directory = old_directory / new_directory;
17601768
1761- if ( !std::filesystem::exists ( new_directory ) )
1762- {
1763- if ( settings.create_dirs )
1769+ if ( !std::filesystem::exists ( new_directory ) )
17641770 {
1765- if ( !std::filesystem::create_directory ( new_directory ) )
1771+ if ( settings.create_dirs )
1772+ {
1773+ if ( !std::filesystem::create_directory ( new_directory ) )
1774+ {
1775+ std::cerr << " ERROR: Failed to create directory "
1776+ << new_directory << " ." << std::endl;
1777+ return false ;
1778+ }
1779+ }
1780+ else
17661781 {
1767- std::cerr << " ERROR: Failed to create directory "
1768- << new_directory << " ." << std::endl;
1782+ std::cerr << " ERROR: The output directory " << new_directory
1783+ << " does not exist ." << std::endl;
17691784 return false ;
17701785 }
17711786 }
1772- else
1773- {
1774- std::cerr << " ERROR: The output directory " << new_directory
1775- << " does not exist." << std::endl;
1776- return false ;
1777- }
1787+ temp_path = std::filesystem::absolute ( new_directory / filename );
17781788 }
17791789
1780- temp_path = std::filesystem::absolute ( new_directory / filename );
1781- }
1790+ if ( !settings.overwrite && std::filesystem::exists ( temp_path ) )
1791+ {
1792+ std::cerr
1793+ << " ERROR: file " << temp_path << " already exists. Use "
1794+ << " --overwrite to allow overwriting existing files. Skipping "
1795+ << " this file." << std::endl;
1796+ return false ;
1797+ }
17821798
1783- if ( !settings.overwrite && std::filesystem::exists ( temp_path ) )
1799+ path = temp_path.string ();
1800+ return true ;
1801+ }
1802+ catch ( const std::exception &e )
17841803 {
1785- std::cerr
1786- << " ERROR: file " << temp_path << " already exists. Use "
1787- << " --overwrite to allow overwriting existing files. Skipping "
1788- << " this file." << std::endl;
1804+ std::cerr << " ERROR: Invalid path format '" << path << " ': " << e.what ()
1805+ << std::endl;
17891806 return false ;
17901807 }
1791-
1792- path = temp_path.string ();
1793- return true ;
17941808}
17951809
17961810bool ImageConverter::save_image (
@@ -1824,6 +1838,40 @@ bool ImageConverter::save_image(
18241838
18251839bool ImageConverter::process_image ( const std::string &input_filename )
18261840{
1841+ // Early validation: check if input file exists and is valid
1842+ if ( input_filename.empty () )
1843+ {
1844+ if ( settings.verbosity > 0 )
1845+ {
1846+ std::cerr << " ERROR: Empty input filename provided." << std::endl;
1847+ }
1848+ return false ;
1849+ }
1850+
1851+ // Validate input file exists
1852+ // Wrap in try-catch to handle filesystem exceptions on Windows
1853+ try
1854+ {
1855+ if ( !std::filesystem::exists ( input_filename ) )
1856+ {
1857+ if ( settings.verbosity > 0 )
1858+ {
1859+ std::cerr << " ERROR: Input file does not exist: "
1860+ << input_filename << std::endl;
1861+ }
1862+ return false ;
1863+ }
1864+ }
1865+ catch ( const std::filesystem::filesystem_error &e )
1866+ {
1867+ if ( settings.verbosity > 0 )
1868+ {
1869+ std::cerr << " ERROR: Filesystem error while checking input file '"
1870+ << input_filename << " ': " << e.what () << std::endl;
1871+ }
1872+ return false ;
1873+ }
1874+
18271875 std::string output_filename = input_filename;
18281876 if ( !make_output_path ( output_filename ) )
18291877 {
0 commit comments