Skip to content

Commit 9ffdca8

Browse files
Fix an error of comparing large numbers (#275)
1 parent 2a19aa5 commit 9ffdca8

File tree

6 files changed

+74
-8
lines changed

6 files changed

+74
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
2020
* Fix the process that checks MOVE statements (#266, #267)
2121
* Fix `INSPECT` statement (#268)
2222
* Fix error handlings of 0 divisions (#273)
23+
* Fix an error of comparing large numbers (#275)
2324
### Optimized
2425
* Optimize the file reading process (#257)
2526

cobj/typeck.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,9 +2501,19 @@ cb_tree cb_build_cond(cb_tree x) {
25012501

25022502
if (cb_chk_num_cond(p->x, p->y)) {
25032503
size1 = cb_field_size(p->x);
2504-
x = cb_build_method_call_3("memcmp", cb_build_cast_address(p->x),
2505-
cb_build_cast_address(p->y),
2506-
cb_int(size1));
2504+
cb_tree xx;
2505+
if (CB_LITERAL_P(p->x)) {
2506+
xx = cb_build_string0(CB_LITERAL(p->x)->data);
2507+
} else {
2508+
xx = cb_build_cast_address(p->x);
2509+
}
2510+
cb_tree yy;
2511+
if (CB_LITERAL_P(p->y)) {
2512+
yy = cb_build_string0(CB_LITERAL(p->y)->data);
2513+
} else {
2514+
yy = cb_build_cast_address(p->y);
2515+
}
2516+
x = cb_build_method_call_3("memcmp", xx, yy, cb_int(size1));
25072517
break;
25082518
}
25092519
if (CB_TREE_CLASS(p->x) == CB_CLASS_NUMERIC &&
@@ -2531,9 +2541,19 @@ cb_tree cb_build_cond(cb_tree x) {
25312541
if (size1 == 1 && size2 == 1) {
25322542
x = cb_build_funcall_2("$G", p->x, p->y);
25332543
} else if (size1 != 0 && size1 == size2) {
2534-
x = cb_build_method_call_3("memcmp", cb_build_cast_address(p->x),
2535-
cb_build_cast_address(p->y),
2536-
cb_int(size1));
2544+
cb_tree xx;
2545+
if (CB_LITERAL_P(p->x)) {
2546+
xx = cb_build_string0(CB_LITERAL(p->x)->data);
2547+
} else {
2548+
xx = cb_build_cast_address(p->x);
2549+
}
2550+
cb_tree yy;
2551+
if (CB_LITERAL_P(p->y)) {
2552+
yy = cb_build_string0(CB_LITERAL(p->y)->data);
2553+
} else {
2554+
yy = cb_build_cast_address(p->y);
2555+
}
2556+
x = cb_build_method_call_3("memcmp", xx, yy, cb_int(size1));
25372557
} else {
25382558
if (CB_TREE_CLASS(p->x) == CB_CLASS_NUMERIC && p->y == cb_zero) {
25392559
x = cb_build_optim_cond(p);

tests/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ misc_DEPENDENCIES = \
190190
misc.src/index-file-status.at \
191191
misc.src/comp-n.at \
192192
misc.src/fd-external.at \
193-
misc.src/compile-multiple-files.at
193+
misc.src/compile-multiple-files.at \
194+
misc.src/compare-large-number.at
194195

195196
EXTRA_DIST = $(srcdir)/package.m4 \
196197
$(TESTS) \

tests/Makefile.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,8 @@ misc_DEPENDENCIES = \
731731
misc.src/index-file-status.at \
732732
misc.src/comp-n.at \
733733
misc.src/fd-external.at \
734-
misc.src/compile-multiple-files.at
734+
misc.src/compile-multiple-files.at \
735+
misc.src/compare-large-number.at
735736

736737
EXTRA_DIST = $(srcdir)/package.m4 \
737738
$(TESTS) \

tests/misc.at

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ m4_include([index-file-status.at])
4141
m4_include([comp-n.at])
4242
m4_include([fd-external.at])
4343
m4_include([compile-multiple-files.at])
44+
m4_include([compare-large-number.at])
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
AT_SETUP([Compare large])
2+
3+
AT_DATA([prog.cbl], [
4+
IDENTIFICATION DIVISION.
5+
PROGRAM-ID. prog.
6+
DATA DIVISION.
7+
WORKING-STORAGE SECTION.
8+
PROCEDURE DIVISION.
9+
IF 2147483647 = 2147483647 THEN
10+
DISPLAY "OK"
11+
END-IF.
12+
13+
IF -2147483648 = 2147483648 THEN
14+
DISPLAY "OK"
15+
END-IF.
16+
17+
EVALUATE 2147483647
18+
WHEN 2147483647
19+
DISPLAY "OK"
20+
WHEN 1
21+
DISPLAY "NG"
22+
WHEN OTHER
23+
DISPLAY "NG".
24+
25+
EVALUATE -2147483648
26+
WHEN -2147483648
27+
DISPLAY "OK"
28+
WHEN 1
29+
DISPLAY "NG"
30+
WHEN OTHER
31+
DISPLAY "NG".
32+
])
33+
34+
AT_CHECK([cobj prog.cbl])
35+
AT_CHECK([java prog], [0],
36+
[OK
37+
OK
38+
OK
39+
OK
40+
])
41+
42+
AT_CLEANUP

0 commit comments

Comments
 (0)