Skip to content

Commit 4d541b3

Browse files
authored
fix: Output error when directories specified by -j and -o options are invalid (#754)
* fix: error on invalid file-path dir * fix: error on invalid file-path dir
1 parent 17795ce commit 4d541b3

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

cobj/cobj.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,12 +1048,28 @@ static int process_command_line(const int argc, char *argv[]) {
10481048
case 'o':
10491049
/* -o : the directory where class files are stored */
10501050
/* -class-file-dir : the directory where class files are stored */
1051+
if (optarg == NULL || *optarg == '\0') {
1052+
fprintf(stderr, "Error: Missing directory path argument\n");
1053+
exit(1);
1054+
}
1055+
if (stat(optarg, &st) != 0 || !(S_ISDIR(st.st_mode))) {
1056+
fprintf(stderr, "Error: '%s' is not a valid directory\n", optarg);
1057+
exit(1);
1058+
}
10511059
output_name = strdup(optarg);
10521060
break;
10531061

10541062
case 'j':
10551063
/* -j : the directory where java files are stored */
10561064
/* -java-source-dir : the directory where java files are stored */
1065+
if (optarg == NULL || *optarg == '\0') {
1066+
fprintf(stderr, "Error: Missing directory path argument\n");
1067+
exit(1);
1068+
}
1069+
if (stat(optarg, &st) != 0 || !(S_ISDIR(st.st_mode))) {
1070+
fprintf(stderr, "Error: '%s' is not a valid directory\n", optarg);
1071+
exit(1);
1072+
}
10571073
java_source_dir = strdup(optarg);
10581074
break;
10591075

tests/command-line-options.src/file-path.at

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,42 @@ AT_CHECK([test ! -e class_dir/prog.class])
122122
AT_CHECK([test ! -e prog.java])
123123
AT_CHECK([rm -f *.java *.class class_dir/* java_dir/*])
124124

125+
AT_CLEANUP
126+
127+
128+
AT_SETUP([file-path options with non-existent directory])
129+
130+
AT_DATA([prog.cbl], [
131+
IDENTIFICATION DIVISION.
132+
PROGRAM-ID. prog.
133+
DATA DIVISION.
134+
WORKING-STORAGE SECTION.
135+
PROCEDURE DIVISION.
136+
DISPLAY "HELLO".
137+
])
138+
139+
AT_CHECK([${COBJ} -j java_dir prog.cbl], [1], [],
140+
[Error: 'java_dir' is not a valid directory
141+
])
142+
143+
AT_CHECK([${COBJ} -java-source-dir=java_dir prog.cbl], [1], [],
144+
[Error: 'java_dir' is not a valid directory
145+
])
146+
147+
AT_CHECK([${COBJ} -java-source-dir= prog.cbl], [1], [],
148+
[Error: Missing directory path argument
149+
])
150+
151+
AT_CHECK([${COBJ} -o class_dir prog.cbl], [1], [],
152+
[Error: 'class_dir' is not a valid directory
153+
])
154+
155+
AT_CHECK([${COBJ} -class-file-dir=class_dir prog.cbl], [1], [],
156+
[Error: 'class_dir' is not a valid directory
157+
])
158+
159+
AT_CHECK([${COBJ} -class-file-dir= prog.cbl], [1], [],
160+
[Error: Missing directory path argument
161+
])
162+
125163
AT_CLEANUP

0 commit comments

Comments
 (0)