Skip to content

Commit b58900b

Browse files
committed
feat(*): ゼロ除算のエラー検出機能を追加
feat(*): Add error check that division by zero 異常動作の原因となるゼロ除算を検出するため、該当箇所をエラーとするオプションと動作を追加した。 * コンフィグファイルのオプションに「zero_division_error」を追加した。yesとすることで機能が有効となる。 * オプションが有効な場合、以下の動作が追加される。 ** 計算式に数値のゼロでの除算が含まれている場合、コンパイルエラーとする ** 変数を使った除算で、変数にゼロが代入され、ゼロ除算となる場合、実行時エラーとする ** ON SIZE ERROR句が記述されている場合には実行時エラーとならずハンドルされる * テストケースとして、catch-exception.atを追加した
1 parent f426214 commit b58900b

File tree

16 files changed

+278
-1
lines changed

16 files changed

+278
-1
lines changed

cobc/codegen.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,6 +3267,12 @@ output_stmt (cb_tree x)
32673267
output_line ("cob_exception_code = 0;");
32683268
}
32693269

3270+
if (cb_zero_division_error && p->name &&
3271+
((strcmp (p->name, "DIVIDE") == 0) || (strcmp (p->name, "COMPUTE") == 0)) &&
3272+
(!p->handler1 && !p->handler2)) {
3273+
output_line ("cob_error_on_exit_flag = 1;");
3274+
}
3275+
32703276
if (p->null_check) {
32713277
output_stmt (p->null_check);
32723278
}

cobc/config.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ CB_CONFIG_BOOLEAN (cb_switch_no_mnemonic, "switch-no-mnemonic")
5959
CB_CONFIG_BOOLEAN (cb_allow_is_in_sort_key_spec, "allow-is-in-sort-key-spec")
6060
CB_CONFIG_BOOLEAN (cb_allow_search_key_in_rhs, "allow-search-key-in-rhs")
6161
CB_CONFIG_BOOLEAN (cb_ignore_invalid_record_contains, "ignore-invalid-record-contains")
62+
CB_CONFIG_BOOLEAN (cb_zero_division_error, "zero_division_error")
6263
CB_CONFIG_SUPPORT (cb_author_paragraph, "author-paragraph")
6364
CB_CONFIG_SUPPORT (cb_memory_size_clause, "memory-size-clause")
6465
CB_CONFIG_SUPPORT (cb_multiple_file_tape_clause, "multiple-file-tape-clause")

cobc/tree.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,6 +2251,12 @@ cb_build_binary_op (cb_tree x, int op, cb_tree y)
22512251
if (x == cb_error_node || y == cb_error_node) {
22522252
return cb_error_node;
22532253
}
2254+
if (cb_zero_division_error && op == '/') {
2255+
y = cb_check_zero_division (y);
2256+
if (y == cb_error_node) {
2257+
return cb_error_node;
2258+
}
2259+
}
22542260
category = CB_CATEGORY_NUMERIC;
22552261
break;
22562262

cobc/tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ extern int cb_fits_long_long (cb_tree x);
260260
extern int cb_get_int (cb_tree x);
261261
extern int cb_is_digist_data (cb_tree x);
262262
extern long long cb_get_long_long (cb_tree x);
263+
extern cb_tree cb_check_zero_division(cb_tree x);
263264

264265
/*
265266
* Constants

cobc/typeck.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7717,3 +7717,22 @@ cb_build_write_advancing_page (cb_tree pos)
77177717

77187718
return cb_int (opt | COB_WRITE_PAGE);
77197719
}
7720+
7721+
cb_tree
7722+
cb_check_zero_division (cb_tree x)
7723+
{
7724+
if (x == cb_error_node) {
7725+
return cb_error_node;
7726+
}
7727+
7728+
if (! CB_NUMERIC_LITERAL_P (x)) {
7729+
return x;
7730+
}
7731+
7732+
if (cb_get_int(x) == 0) {
7733+
cb_error_x (x, _("Detected division by zero."));
7734+
return cb_error_node;
7735+
}
7736+
7737+
return x;
7738+
}

config/default-en.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,4 @@ switch-no-mnemonic: no
147147
allow-is-in-sort-key-spec: no
148148
allow-search-key-in-rhs: no
149149
ignore-invalid-record-contains: no
150+
zero_division_error: no

config/default-jp.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,4 @@ switch-no-mnemonic: no
147147
allow-is-in-sort-key-spec: no
148148
allow-search-key-in-rhs: no
149149
ignore-invalid-record-contains: no
150+
zero_division_error: yes

config/default.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,4 @@ switch-no-mnemonic: no
147147
allow-is-in-sort-key-spec: no
148148
allow-search-key-in-rhs: no
149149
ignore-invalid-record-contains: no
150+
zero_division_error: no

config/jp-compat.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ switch-no-mnemonic: yes
2222
allow-is-in-sort-key-spec: yes
2323
allow-search-key-in-rhs: yes
2424
ignore-invalid-record-contains: yes
25+
zero_division_error: yes
2526

2627
# Value: 'any', 'fatal', 'never'
2728
abort-on-io-exception: fatal

libcob/common.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ struct cob_module *cob_current_module = NULL;
169169

170170
int cob_initialized = 0;
171171
int cob_exception_code = 0;
172+
int cob_error_on_exit_flag = 0;
172173

173174
int cob_call_params = 0;
174175
int cob_save_call_params = 0;

0 commit comments

Comments
 (0)