From 11f7a5438d03bbadab28305e64e9cdc17b59380e Mon Sep 17 00:00:00 2001
From: "James K. Lowden" <jklowden@symas.com>
Date: Mon, 1 Jan 2024 13:27:10 -0500
Subject: [PATCH] REDEFINing table elements have capacity divided by occurs

---
 gcc/cobol/parse_ante.h | 6 +++++-
 gcc/cobol/util.cc      | 7 +++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/gcc/cobol/parse_ante.h b/gcc/cobol/parse_ante.h
index 865dd1c11b0d..376cb289f3d5 100644
--- a/gcc/cobol/parse_ante.h
+++ b/gcc/cobol/parse_ante.h
@@ -2166,7 +2166,11 @@ valid_redefine( const cbl_field_t *field, const cbl_field_t *orig ) {
    */
   if( field->type != FldGroup && orig->type != FldGroup ) {
     if( orig->size() < field->size() ) {
-      if( orig->level > 1 || has_field_attr(orig->attr, external_e) ) 
+      if( orig->level > 1 || has_field_attr(orig->attr, external_e) )
+        if( yydebug ) {
+          yyerrorv( "size error orig:  %s", field_str(orig) );
+          yyerrorv( "size error redef: %s", field_str(field) );
+        }
         yyerrorv( "error: %s (%s size %u) larger than REDEFINES %s (%s size %u)",
                   field->name,
                   3 + cbl_field_type_str(field->type), field->size(),
diff --git a/gcc/cobol/util.cc b/gcc/cobol/util.cc
index bbc3e7824c9d..f3ec7fc036e5 100644
--- a/gcc/cobol/util.cc
+++ b/gcc/cobol/util.cc
@@ -752,6 +752,13 @@ redefine_field( cbl_field_t *field ) {
   }
 
   if( field->data.capacity == 0 ) field->data = primary->data;
+
+  if( field->occurs.ntimes() > 0 ) {
+    if( field->data.digits == field->data.capacity ) {
+      field->data.digits /= field->occurs.ntimes();
+    }
+      field->data.capacity /= field->occurs.ntimes();    
+  }
   
   if( is_numeric(field->type) && field->usage == FldDisplay ) {
     fOK = symbol_field_type_update(field, FldNumericDisplay, false);
-- 
GitLab