Skip to content

Commit e9573d6

Browse files
authored
Merge pull request #40 from opensourcecobol/feature/add_Runtime_error_check
実行時エラーチェックを追加
2 parents 3a41c6d + 4be0e0a commit e9573d6

File tree

13 files changed

+230
-0
lines changed

13 files changed

+230
-0
lines changed

cobc/cobc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,6 +2327,15 @@ main (int argc, char *argv[])
23272327
/* Process command line arguments */
23282328
iargs = process_command_line (argc, argv);
23292329

2330+
/* Process config file options */
2331+
if (cb_enable_check_subscript_out_of_bounds) {
2332+
CB_EXCEPTION_ENABLE (COB_EC_BOUND_SUBSCRIPT) = 1;
2333+
}
2334+
if (cb_enable_expect_compute_string_error) {
2335+
CB_EXCEPTION_ENABLE (COB_EC_DATA_INCOMPATIBLE) = 1;
2336+
}
2337+
2338+
23302339
/* Check the filename */
23312340
if (iargs == argc) {
23322341
fprintf (stderr, "cobc: No input files\n");

cobc/config.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ 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")
6262
CB_CONFIG_BOOLEAN (cb_zero_division_error, "zero_division_error")
63+
CB_CONFIG_BOOLEAN (cb_enable_check_subscript_out_of_bounds, "enable-check-subscript-out-of-bounds")
64+
CB_CONFIG_BOOLEAN (cb_enable_expect_numeric_error, "enable-expect-numeric-error")
65+
CB_CONFIG_BOOLEAN (cb_enable_expect_compute_string_error, "enable-expect-compute-string-error")
6366
CB_CONFIG_SUPPORT (cb_author_paragraph, "author-paragraph")
6467
CB_CONFIG_SUPPORT (cb_memory_size_clause, "memory-size-clause")
6568
CB_CONFIG_SUPPORT (cb_multiple_file_tape_clause, "multiple-file-tape-clause")

cobc/typeck.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4689,6 +4689,56 @@ move_error (cb_tree src, cb_tree dst, const size_t value_flag, const int flag,
46894689
return 0;
46904690
}
46914691

4692+
static void
4693+
error_destination (cb_tree x)
4694+
{
4695+
struct cb_reference *r;
4696+
struct cb_field *f;
4697+
cb_tree loc;
4698+
4699+
r = CB_REFERENCE (x);
4700+
f = CB_FIELD (r->value);
4701+
loc = CB_TREE (f);
4702+
4703+
if (r->offset) {
4704+
return;
4705+
}
4706+
4707+
if (!strcmp (f->name, "RETURN-CODE") ||
4708+
!strcmp (f->name, "SORT-RETURN") ||
4709+
!strcmp (f->name, "NUMBER-OF-CALL-PARAMETERS")) {
4710+
cb_error (_("Internal register '%s' defined as BINARY-LONG"), f->name);
4711+
} else if (f->pic) {
4712+
cb_error_x (loc, _("'%s' defined here as PIC %s"), check_filler_name ((char *)f->name), f->pic->orig);
4713+
} else {
4714+
cb_error_x (loc, _("'%s' defined here as a group of length %d"), check_filler_name ((char *)f->name), f->size);
4715+
}
4716+
}
4717+
4718+
static int
4719+
move_error2 (cb_tree src, cb_tree dst, const size_t value_flag, const int flag,
4720+
const int src_flag, const char *msg)
4721+
{
4722+
cb_tree loc;
4723+
4724+
loc = src->source_line ? src : dst;
4725+
if (value_flag) {
4726+
/* VALUE clause */
4727+
cb_error_x (loc, msg);
4728+
} else {
4729+
/* MOVE statement */
4730+
if (flag) {
4731+
cb_error_x (loc, msg);
4732+
if (src_flag) {
4733+
error_destination (src);
4734+
}
4735+
error_destination (dst);
4736+
}
4737+
}
4738+
4739+
return 0;
4740+
}
4741+
46924742
/* count the number of free places in an alphanumeric edited field */
46934743
static int
46944744
count_pic_alphanumeric_edited (struct cb_field *field)
@@ -5335,6 +5385,11 @@ validate_move (cb_tree src, cb_tree dst, size_t is_value)
53355385
return 0;
53365386

53375387
expect_numeric:
5388+
if (cb_enable_expect_numeric_error) {
5389+
return move_error2 (src, dst, is_value, 1, 0,
5390+
_("Numeric value is expected"));
5391+
}
5392+
53385393
return move_error (src, dst, is_value, cb_warn_strict_typing, 0,
53395394
_("Numeric value is expected"));
53405395

@@ -6029,6 +6084,11 @@ cb_emit_move (cb_tree src, cb_tree dsts)
60296084
}
60306085

60316086
for (l = dsts; l; l = CB_CHAIN (l)) {
6087+
if (cb_enable_expect_numeric_error) {
6088+
if (CB_TREE_TAG (src) == CB_TAG_REFERENCE) {
6089+
cb_emit (cb_build_funcall_2 ("cob_check_mvstrnum", src, CB_VALUE (l)));
6090+
}
6091+
}
60326092
cb_emit (cb_build_move (src, CB_VALUE (l)));
60336093
}
60346094
}

config/default-en.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,6 @@ allow-is-in-sort-key-spec: no
148148
allow-search-key-in-rhs: no
149149
ignore-invalid-record-contains: no
150150
zero_division_error: no
151+
enable-check-subscript-out-of-bounds: yes
152+
enable-expect-numeric-error: yes
153+
enable-expect-compute-string-error: yes

config/default-jp.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,6 @@ allow-is-in-sort-key-spec: no
148148
allow-search-key-in-rhs: no
149149
ignore-invalid-record-contains: no
150150
zero_division_error: yes
151+
enable-check-subscript-out-of-bounds: yes
152+
enable-expect-numeric-error: yes
153+
enable-expect-compute-string-error: yes

config/default.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,6 @@ allow-is-in-sort-key-spec: no
148148
allow-search-key-in-rhs: no
149149
ignore-invalid-record-contains: no
150150
zero_division_error: no
151+
enable-check-subscript-out-of-bounds: no
152+
enable-expect-numeric-error: no
153+
enable-expect-compute-string-error: no

config/jp-compat.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ allow-is-in-sort-key-spec: yes
2323
allow-search-key-in-rhs: yes
2424
ignore-invalid-record-contains: yes
2525
zero_division_error: yes
26+
enable-check-subscript-out-of-bounds: yes
27+
enable-expect-numeric-error: yes
28+
enable-expect-compute-string-error: yes
2629

2730
# Value: 'any', 'fatal', 'never'
2831
abort-on-io-exception: fatal

libcob/common.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,35 @@ cob_check_ref_mod (const int offset, const int length, const int size, const cha
16921692
}
16931693
}
16941694

1695+
void
1696+
cob_check_mvstrnum (cob_field *src, cob_field *dst)
1697+
{
1698+
size_t i;
1699+
1700+
switch (COB_FIELD_TYPE (src)) {
1701+
case COB_TYPE_ALPHANUMERIC:
1702+
case COB_TYPE_ALPHANUMERIC_ALL:
1703+
case COB_TYPE_ALPHANUMERIC_EDITED:
1704+
switch (COB_FIELD_TYPE (dst)) {
1705+
case COB_TYPE_NUMERIC:
1706+
/*case COB_TYPE_NUMERIC_DISPLAY:*/
1707+
case COB_TYPE_NUMERIC_BINARY:
1708+
case COB_TYPE_NUMERIC_PACKED:
1709+
case COB_TYPE_NUMERIC_FLOAT:
1710+
case COB_TYPE_NUMERIC_DOUBLE:
1711+
case COB_TYPE_NUMERIC_EDITED:
1712+
for (i=0; i<src->size; i++) {
1713+
if (! isdigit(COB_FIELD_DATA (src) [i])) {
1714+
cob_runtime_error ("Numeric value is expected");
1715+
cob_stop_run (1);
1716+
}
1717+
}
1718+
break;
1719+
}
1720+
break;
1721+
}
1722+
}
1723+
16951724
unsigned char *
16961725
cob_external_addr (const char *exname, const int exlength)
16971726
{

libcob/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,8 @@ COB_EXPIMP void cob_check_ref_mod_national (int, int, int, const char *);
502502
COB_EXPIMP int cob_check_env (const char *, const char *);
503503
COB_EXPIMP void cob_check_ref_mod (const int, const int,
504504
const int, const char *);
505+
COB_EXPIMP void cob_check_mvstrnum (cob_field *, cob_field *);
506+
505507

506508
/* Comparison functions */
507509
COB_EXPIMP int cob_numeric_cmp (cob_field *, cob_field *);

tests/atlocal.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ TEMPLATE="${abs_srcdir}/data-rep.src"
3737
FLAGS="-std=cobol2002 -debug -Wall ${COBOL_FLAGS}"
3838
FLAGS_NONE=""
3939
FLAGS_JP_COMPAT="${FLAGS} ${CONF_JP_COMPAT}"
40+
FLAGS_JP_COMPAT_NONE="${FLAGS_NONE} ${CONF_JP_COMPAT}"
4041
FLAGS_LIMIT_TEST="${FLAGS} ${CONF_LIMIT_TEST}"
4142
COMPILE="${COBC} -x ${FLAGS}"
4243
COMPILE_DEFAULT="${COBC} -x ${FLAGS_NONE}"
4344
COMPILE_JP_COMPAT="${COBC} -x ${FLAGS_JP_COMPAT}"
45+
COMPILE_JP_COMPAT_DEFAULT="${COBC} -x ${FLAGS_JP_COMPAT_NONE}"
4446
COMPILE_LIMIT_TEST="${COBC} -x ${FLAGS_LIMIT_TEST}"
4547
COMPILE_ONLY="${COBC} -fsyntax-only ${FLAGS}"
4648
COMPILE_ONLY_JP_COMPAT="${COBC} -fsyntax-only ${FLAGS_JP_COMPAT}"

0 commit comments

Comments
 (0)