Skip to content

Commit efda7a7

Browse files
Improve the error handling for opening invalid formatted indexed files (#676)
* fix: check formats of indexed files to open * test: remove unnecessary checks * fix: change constants to avoid overlaps * fix: add a file existing check * test: fix the test of opening an invalid indexed file * test: fix load.at not to call default error handlers * chore: end a transaction * refactor: CobolIndexedFile.java * test: prevent infinite loops
1 parent cb1da8d commit efda7a7

File tree

4 files changed

+76
-15
lines changed

4 files changed

+76
-15
lines changed

libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolFile.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,44 +285,50 @@ public class CobolFile {
285285
/** TODO: 準備中 */
286286
protected static final int COB_STATUS_91_NOT_AVAILABLE = 91;
287287

288+
// ==============================================
289+
// The following constants must not be equal
290+
// to any of the above constants `COB_STATUS_*`
291+
288292
/** TODO: 準備中 */
289-
protected static final int COB_LINAGE_INVALID = 16384;
293+
protected static final int ENOENT = 1002;
290294

291295
/** TODO: 準備中 */
292-
protected static final int COB_NOT_CONFIGURED = 32768;
296+
protected static final int EBADF = 1009;
293297

294298
/** TODO: 準備中 */
295-
public static final int COB_SELECT_FILE_STATUS = 0x01;
299+
protected static final int EACCESS = 1013;
296300

297301
/** TODO: 準備中 */
298-
public static final int COB_SELECT_EXTERNAL = 0x02;
302+
protected static final int EISDIR = 1021;
299303

300304
/** TODO: 準備中 */
301-
public static final int COB_SELECT_LINAGE = 0x04;
305+
protected static final int EROFS = 1030;
302306

303307
/** TODO: 準備中 */
304-
public static final int COB_SELECT_SPLITKEY = 0x08;
308+
protected static final int EAGAIN = 1011;
309+
310+
// ==============================================
305311

306312
/** TODO: 準備中 */
307-
protected static final int FNSTATUSSIZE = 3;
313+
protected static final int COB_LINAGE_INVALID = 16384;
308314

309315
/** TODO: 準備中 */
310-
protected static final int ENOENT = 2;
316+
protected static final int COB_NOT_CONFIGURED = 32768;
311317

312318
/** TODO: 準備中 */
313-
protected static final int EBADF = 9;
319+
public static final int COB_SELECT_FILE_STATUS = 0x01;
314320

315321
/** TODO: 準備中 */
316-
protected static final int EACCESS = 13;
322+
public static final int COB_SELECT_EXTERNAL = 0x02;
317323

318324
/** TODO: 準備中 */
319-
protected static final int EISDIR = 21;
325+
public static final int COB_SELECT_LINAGE = 0x04;
320326

321327
/** TODO: 準備中 */
322-
protected static final int EROFS = 30;
328+
public static final int COB_SELECT_SPLITKEY = 0x08;
323329

324330
/** TODO: 準備中 */
325-
protected static final int EAGAIN = 11;
331+
protected static final int FNSTATUSSIZE = 3;
326332

327333
/** TODO: 準備中 */
328334
public static CobolFile errorFile;

libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/file/CobolIndexedFile.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,27 @@ public int open_(String filename, int mode, int sharing) {
208208

209209
boolean fileExists = new java.io.File(filename).exists();
210210

211+
if (mode == COB_OPEN_INPUT && !fileExists) {
212+
return ENOENT;
213+
}
214+
211215
p.connection = null;
212216
try {
213217
p.connection =
214218
DriverManager.getConnection("jdbc:sqlite:" + filename, config.toProperties());
215219
p.connection.setAutoCommit(false);
220+
221+
// Check if the file is accessible
222+
try (Statement st = p.connection.createStatement()) {
223+
st.execute("select 1");
224+
}
225+
p.connection.commit();
216226
} catch (SQLException e) {
217227
int errorCode = e.getErrorCode();
218228
if (errorCode == SQLiteErrorCode.SQLITE_BUSY.code) {
219229
return COB_STATUS_61_FILE_SHARING;
220230
} else {
221-
return ENOENT;
231+
return COB_STATUS_30_PERMANENT_ERROR;
222232
}
223233
} catch (Exception e) {
224234
return COB_STATUS_30_PERMANENT_ERROR;

tests/cobj-idx.src/load.at

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ AT_DATA([load.cbl],
1313
alternate record key is alt-key-1
1414
alternate record key is alt-key-2
1515
alternate record key is alt-key-dup-1 with duplicates
16-
alternate record key is alt-key-dup-2 with duplicates.
16+
alternate record key is alt-key-dup-2 with duplicates
17+
file status is file-status.
1718
data division.
1819
file section.
1920
fd f.
@@ -25,9 +26,13 @@ AT_DATA([load.cbl],
2526
03 alt-key-dup-2 pic x(5).
2627
03 rec-value pic x(5).
2728
working-storage section.
29+
01 file-status pic xx.
2830
procedure division.
2931
main-proc.
3032
open input f.
33+
if file-status not = '00'
34+
stop run
35+
end-if.
3136
perform forever
3237
read f next
3338
at end

tests/run.src/miscellaneous.at

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,3 +2095,43 @@ AT_CHECK([java prog], [1], [],
20952095
])
20962096

20972097
AT_CLEANUP
2098+
2099+
AT_SETUP([Open an invalid formatted indexed file])
2100+
2101+
AT_CHECK([echo invalid-data > invalid-formatted-file])
2102+
2103+
AT_DATA([prog.cbl],
2104+
[
2105+
IDENTIFICATION DIVISION.
2106+
PROGRAM-ID. prog.
2107+
2108+
ENVIRONMENT DIVISION.
2109+
INPUT-OUTPUT SECTION.
2110+
FILE-CONTROL.
2111+
SELECT F ASSIGN TO "invalid-formatted-file"
2112+
ORGANIZATION IS INDEXED
2113+
ACCESS MODE IS DYNAMIC
2114+
RECORD KEY IS REC-KEY
2115+
FILE STATUS IS FILE-STATUS.
2116+
2117+
DATA DIVISION.
2118+
FILE SECTION.
2119+
FD f.
2120+
01 REC.
2121+
05 REC-KEY PIC X(5).
2122+
2123+
WORKING-STORAGE SECTION.
2124+
01 FILE-STATUS PIC XX.
2125+
PROCEDURE DIVISION.
2126+
MAIN-PROCEDURE.
2127+
2128+
OPEN INPUT f.
2129+
DISPLAY FILE-STATUS.
2130+
CLOSE f.
2131+
])
2132+
2133+
AT_CHECK([${COMPILE} prog.cbl])
2134+
AT_CHECK([java prog], [0],
2135+
[30
2136+
])
2137+
AT_CLEANUP

0 commit comments

Comments
 (0)