From 7a829f2870843c4f57373bf572870bb7044d58b0 Mon Sep 17 00:00:00 2001 From: Kio Watanabe Date: Fri, 28 Nov 2025 15:29:50 +0900 Subject: [PATCH 1/2] fix: error on invalid file-path dir --- cobj/cobj.c | 8 ++++++ tests/command-line-options.src/file-path.at | 30 +++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/cobj/cobj.c b/cobj/cobj.c index 48cafbad..3988adce 100644 --- a/cobj/cobj.c +++ b/cobj/cobj.c @@ -1048,12 +1048,20 @@ static int process_command_line(const int argc, char *argv[]) { case 'o': /* -o : the directory where class files are stored */ /* -class-file-dir : the directory where class files are stored */ + if (stat(optarg, &st) != 0 || !(S_ISDIR(st.st_mode))) { + fprintf(stderr, "Error - '%s' is not a valid directory\n", optarg); + exit(1); + } output_name = strdup(optarg); break; case 'j': /* -j : the directory where java files are stored */ /* -java-source-dir : the directory where java files are stored */ + if (stat(optarg, &st) != 0 || !(S_ISDIR(st.st_mode))) { + fprintf(stderr, "Error - '%s' is not a valid directory\n", optarg); + exit(1); + } java_source_dir = strdup(optarg); break; diff --git a/tests/command-line-options.src/file-path.at b/tests/command-line-options.src/file-path.at index cd9e000b..97f2a794 100644 --- a/tests/command-line-options.src/file-path.at +++ b/tests/command-line-options.src/file-path.at @@ -122,4 +122,34 @@ AT_CHECK([test ! -e class_dir/prog.class]) AT_CHECK([test ! -e prog.java]) AT_CHECK([rm -f *.java *.class class_dir/* java_dir/*]) +AT_CLEANUP + + +AT_SETUP([file-path options with non-existent directory]) + +AT_DATA([prog.cbl], [ + IDENTIFICATION DIVISION. + PROGRAM-ID. prog. + DATA DIVISION. + WORKING-STORAGE SECTION. + PROCEDURE DIVISION. + DISPLAY "HELLO". +]) + +AT_CHECK([${COBJ} -j java_dir prog.cbl], [1], [], +[Error - 'java_dir' is not a valid directory +]) + +AT_CHECK([${COBJ} -java-source-dir=java_dir prog.cbl], [1], [], +[Error - 'java_dir' is not a valid directory +]) + +AT_CHECK([${COBJ} -o class_dir prog.cbl], [1], [], +[Error - 'class_dir' is not a valid directory +]) + +AT_CHECK([${COBJ} -class-file-dir=class_dir prog.cbl], [1], [], +[Error - 'class_dir' is not a valid directory +]) + AT_CLEANUP \ No newline at end of file From 1f71bac5a42a1d863ed4bb3b6d883120fec2e0cd Mon Sep 17 00:00:00 2001 From: Kio Watanabe Date: Fri, 28 Nov 2025 16:20:50 +0900 Subject: [PATCH 2/2] fix: error on invalid file-path dir --- cobj/cobj.c | 12 ++++++++++-- tests/command-line-options.src/file-path.at | 16 ++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cobj/cobj.c b/cobj/cobj.c index 3988adce..b86d65b7 100644 --- a/cobj/cobj.c +++ b/cobj/cobj.c @@ -1048,8 +1048,12 @@ static int process_command_line(const int argc, char *argv[]) { case 'o': /* -o : the directory where class files are stored */ /* -class-file-dir : the directory where class files are stored */ + if (optarg == NULL || *optarg == '\0') { + fprintf(stderr, "Error: Missing directory path argument\n"); + exit(1); + } if (stat(optarg, &st) != 0 || !(S_ISDIR(st.st_mode))) { - fprintf(stderr, "Error - '%s' is not a valid directory\n", optarg); + fprintf(stderr, "Error: '%s' is not a valid directory\n", optarg); exit(1); } output_name = strdup(optarg); @@ -1058,8 +1062,12 @@ static int process_command_line(const int argc, char *argv[]) { case 'j': /* -j : the directory where java files are stored */ /* -java-source-dir : the directory where java files are stored */ + if (optarg == NULL || *optarg == '\0') { + fprintf(stderr, "Error: Missing directory path argument\n"); + exit(1); + } if (stat(optarg, &st) != 0 || !(S_ISDIR(st.st_mode))) { - fprintf(stderr, "Error - '%s' is not a valid directory\n", optarg); + fprintf(stderr, "Error: '%s' is not a valid directory\n", optarg); exit(1); } java_source_dir = strdup(optarg); diff --git a/tests/command-line-options.src/file-path.at b/tests/command-line-options.src/file-path.at index 97f2a794..b00039e0 100644 --- a/tests/command-line-options.src/file-path.at +++ b/tests/command-line-options.src/file-path.at @@ -137,19 +137,27 @@ AT_DATA([prog.cbl], [ ]) AT_CHECK([${COBJ} -j java_dir prog.cbl], [1], [], -[Error - 'java_dir' is not a valid directory +[Error: 'java_dir' is not a valid directory ]) AT_CHECK([${COBJ} -java-source-dir=java_dir prog.cbl], [1], [], -[Error - 'java_dir' is not a valid directory +[Error: 'java_dir' is not a valid directory +]) + +AT_CHECK([${COBJ} -java-source-dir= prog.cbl], [1], [], +[Error: Missing directory path argument ]) AT_CHECK([${COBJ} -o class_dir prog.cbl], [1], [], -[Error - 'class_dir' is not a valid directory +[Error: 'class_dir' is not a valid directory ]) AT_CHECK([${COBJ} -class-file-dir=class_dir prog.cbl], [1], [], -[Error - 'class_dir' is not a valid directory +[Error: 'class_dir' is not a valid directory +]) + +AT_CHECK([${COBJ} -class-file-dir= prog.cbl], [1], [], +[Error: Missing directory path argument ]) AT_CLEANUP \ No newline at end of file