Skip to content

Commit

Permalink
follow up tp r4737 - strncpy crusade
Browse files Browse the repository at this point in the history
cobc
* typeck.c (cb_check_field_debug): fixed bug introduced with and don't create unnecessary long DEBUG-NAME strings
* config.c (cb_load_conf_file): check snprintf size fixing compile warning
  • Loading branch information
sf-mensch committed Oct 5, 2022
1 parent 157fa35 commit a0cacd0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 46 deletions.
13 changes: 10 additions & 3 deletions cobc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

2022-10-05 Simon Sobisch <[email protected]>

* typeck.c (cb_check_field_debug): fixed bug introduced with last change
and don't create unnecessary long DEBUG-NAME strings
* config.c (cb_load_conf_file): check snprintf size fixing compile warning

2022-10-04 Nicolas Berthier <[email protected]>

* pplex.l, parser.y: fix AREACHECK in DEFAULT SECTION of CONTROL DIVISON
Expand All @@ -14,9 +20,9 @@
* codegen.c (output_error_handler), typeck.c (cb_emit_open): use of
cb_open_mode_to_string for improved codegen (using enum values instead
of numeric constants)
* tree.c (cb_name_1): replaced strncpy calls by snprintf, calculate length
from known positions instead of strlen; limit scope of some variables,
allowing them to be defined as const
* tree.c (cb_name_1): replaced strncpy calls by checked snprintf, calculate
length from known positions instead of strlen; limit scope of some
variables, allowing them to be defined as const
* cobc.c (print_88_values, print_fields, xref_88_values, xref_fields):
drop copy of field name to temporary lcl_name as a field name has
a maximum length of COB_MAX_WORDLEN (63) and therefore is always smaller
Expand All @@ -28,6 +34,7 @@
* typeck.c (cb_build_debug_item): refactored to remove number of casts
* field.c (copy_duplicated_field_into_field): extracted
from copy_children, copy_into_field_recursive
* tree.c (build_sum_counter): fix reference to sum counter

2022-10-01 Simon Sobisch <[email protected]>

Expand Down
2 changes: 1 addition & 1 deletion cobc/cobc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ cobc_elided_strcpy (char *dest, const char* src,
memcpy (dest + max_size - 3, "...", 3 + 1);
} else {
memcpy (dest, "...", 3);
memcpy (dest + 3, src + size - size_to_copy, size_to_copy + 1);
memcpy (dest + 3, src + size - size_to_copy, size_to_copy);
}
}
return dest;
Expand Down
7 changes: 4 additions & 3 deletions cobc/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,10 @@ cb_load_conf_file (const char *conf_file, const enum cb_include_type include_typ
}
if (filename[0] == 0) {
/* check for COB_CONFIG_DIR (use default if not in environment) */
snprintf (filename, (size_t)COB_NORMAL_MAX, "%s%c%s", cob_config_dir, SLASH_CHAR, conf_file);
filename[COB_NORMAL_MAX] = 0;
if (access (filename, F_OK) == 0) { /* and prefixed file exist */
const int size = snprintf (filename, (size_t)COB_NORMAL_MAX,
"%s%c%s", cob_config_dir, SLASH_CHAR, conf_file);
if (size < COB_NORMAL_MAX /* no overflow - full name available */
&& access (filename, F_OK) == 0) { /* and prefixed file exist */
conf_file = filename; /* Prefix COB_CONFIG_DIR */
}
}
Expand Down
23 changes: 10 additions & 13 deletions cobc/typeck.c
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ cb_check_field_debug (cb_tree fld)
cb_tree z;
size_t size;
size_t found;
char buff[COB_MINI_BUFF];
char buff[COB_MINI_BUFF]; /* at least DEBUG-NAME + 4 + COB_MAX_NAMELEN...*/

/* Basic reference check */
if (CB_WORD_COUNT (fld) > 0) {
Expand Down Expand Up @@ -1264,19 +1264,16 @@ cb_check_field_debug (cb_tree fld)

/* Set up debug info */
size = sprintf (buff, "%s", CB_FIELD (x)->name);
l = CB_REFERENCE (fld)->chain;
if (l) {
for (; l; l = CB_REFERENCE (l)->chain) {
z = cb_ref (l);
if (z != cb_error_node) {
const size_t size_new = strlen (CB_FIELD (z)->name) + 1;
if (size + 4 + size_new >= sizeof(buff)) {
break;
}
memcpy (buff + size, " OF ", 4);
size += 4;
memcpy (buff + size, CB_FIELD (z)->name, size_new);
/* DEBUG-NAME's max is fixed-length 30, so no use in writing more */
for (l = CB_REFERENCE (fld)->chain; l && size < 30; l = CB_REFERENCE (l)->chain) {
z = cb_ref (l);
if (z != cb_error_node) {
const char *name = CB_FIELD (z)->name;
const size_t sname = strlen (name) + 1;
if (size + 4 + sname >= sizeof(buff)) {
break;
}
size += sprintf (buff + size, " OF %s", name);
}
}
current_statement->debug_nodups =
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite.src/listings.at
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@ prog-2.cob:16: warning: unreachable statement 'ACCEPT'
# Check once with $COMPILE and once with $COMPILE_ONLY.
# This tests whether codegen affects the listing.

AT_CHECK([$COMPILE -x -Wunreachable -t prog.lst -Xref -tsymbols prog-1.cob prog-2.cob], [0], [], [ignore])
AT_CHECK([$COMPILE -Wunreachable -t prog.lst -Xref -tsymbols prog-1.cob prog-2.cob], [0], [], [ignore])
AT_CHECK([$UNIFY_LISTING prog.lst prog.lis], [0], [], [])
AT_CHECK([diff expected.lst prog.lis], [0], [], [])

Expand Down
62 changes: 37 additions & 25 deletions tests/testsuite.src/run_fundamental.at
Original file line number Diff line number Diff line change
Expand Up @@ -5003,14 +5003,17 @@ AT_DATA([prog.cob], [
WORKING-STORAGE SECTION.
01 MY-DATA-FIELDS.
02 MY-DATA-FIELD-1 PIC 9 VALUE 1.
02 MY-DATA-FIELD-2 PIC 9 VALUE 4.
02 MDF.
03 MDF-TOO-LONG-NAMES.
05 MY-DATA-FIELD-2 PIC 9 VALUE 4.
05 MY-DATA-FIELD-3 REDEFINES MY-DATA-FIELD-2 PIC X.
01 MY-DATA-FIELD-B PIC X(40) VALUE "ABCD".
PROCEDURE DIVISION.
DECLARATIVES.
TEST-DEBUG SECTION.
USE FOR DEBUGGING ON ALL REFERENCES OF MY-DATA-FIELD-1
ALL MY-DATA-FIELD-2
MY-DATA-FIELD-B.
MY-DATA-FIELD-3 MY-DATA-FIELD-B.
DISPLAY DEBUG-ITEM "|" END-DISPLAY.
END DECLARATIVES.
INIT-PAR.
Expand All @@ -5023,36 +5026,45 @@ AT_DATA([prog.cob], [
END-PAR.
MOVE "99" TO MY-DATA-FIELD-B.
MOVE MY-DATA-FIELD-B TO MY-DATA-FIELDS.
MOVE "X" TO MY-DATA-FIELD-3
OF MDF
OF MY-DATA-FIELDS.
MOVE "-" TO MY-DATA-FIELD-3
OF MDF-TOO-LONG-NAMES
OF MDF
OF MY-DATA-FIELDS.
STOP RUN.
])

AT_CHECK([$COMPILE -fmissing-statement=ok prog.cob], [0], [], [])
# TODO: validate against other compilers, especially the line 30;
# likely the second line should be 25 instead of 24:
AT_CHECK([COB_SET_DEBUG=1 $COBCRUN_DIRECT ./prog], [0],
[ 22 MY-DATA-FIELD-2 6 |
24 MY-DATA-FIELD-1 1 |
24 MY-DATA-FIELD-1 1 |
24 MY-DATA-FIELD-2 6 |
24 MY-DATA-FIELD-1 2 |
24 MY-DATA-FIELD-1 2 |
24 MY-DATA-FIELD-2 6 |
24 MY-DATA-FIELD-1 3 |
24 MY-DATA-FIELD-1 3 |
24 MY-DATA-FIELD-2 6 |
24 MY-DATA-FIELD-1 4 |
24 MY-DATA-FIELD-1 4 |
24 MY-DATA-FIELD-2 6 |
24 MY-DATA-FIELD-1 5 |
24 MY-DATA-FIELD-1 5 |
24 MY-DATA-FIELD-2 6 |
24 MY-DATA-FIELD-1 6 |
24 MY-DATA-FIELD-1 6 |
24 MY-DATA-FIELD-2 6 |
24 MY-DATA-FIELD-1 7 |
24 MY-DATA-FIELD-1 7 |
24 MY-DATA-FIELD-2 6 |
29 MY-DATA-FIELD-B 99 |
[ 25 MY-DATA-FIELD-2 6 |
27 MY-DATA-FIELD-1 1 |
27 MY-DATA-FIELD-1 1 |
27 MY-DATA-FIELD-2 6 |
27 MY-DATA-FIELD-1 2 |
27 MY-DATA-FIELD-1 2 |
27 MY-DATA-FIELD-2 6 |
27 MY-DATA-FIELD-1 3 |
27 MY-DATA-FIELD-1 3 |
27 MY-DATA-FIELD-2 6 |
27 MY-DATA-FIELD-1 4 |
27 MY-DATA-FIELD-1 4 |
27 MY-DATA-FIELD-2 6 |
27 MY-DATA-FIELD-1 5 |
27 MY-DATA-FIELD-1 5 |
27 MY-DATA-FIELD-2 6 |
27 MY-DATA-FIELD-1 6 |
27 MY-DATA-FIELD-1 6 |
27 MY-DATA-FIELD-2 6 |
27 MY-DATA-FIELD-1 7 |
27 MY-DATA-FIELD-1 7 |
27 MY-DATA-FIELD-2 6 |
32 MY-DATA-FIELD-B 99 |
34 MY-DATA-FIELD-3 OF MDF OF MY-D X |
37 MY-DATA-FIELD-3 OF MDF-TOO-LON - |
], [])

AT_CLEANUP
Expand Down

0 comments on commit a0cacd0

Please sign in to comment.