https://gcc.gnu.org/g:cee6948b4633f2518ac5089c59c76558c8ae084e

commit r17-5948-gcee6948b4633f2518ac5089c59c76558c8ae084e
Author: Robert Dubner <[email protected]>
Date:   Wed Nov 4 11:05:50 2026 -0500

    cobol: Fix bugs exposed by GDB-COBOL development.
    
    Debugging the debugger exposed some memory overrun errors in the
    compiler's expand_picture() and expand_expanded() routines; those are
    fixed here.
    
    The establish_using fix prevents the run-time addition of a small number
    to a NULL pointer on big-endian architectures.
    
    gcc/cobol/ChangeLog:
    
            * genapi.cc (establish_using): A run-time check for a NULL pointer
            prevents adding a small number to it.
            (actually_create_the_static_field): Use modified expand_expanded().
            (parser_symbol_add): Use original_without_capacity().
            * symbols.cc (expand_picture): Use std::string for retval instead of
            allocated memory.
            (expand_expanded): Return char* instead of modifying the caller.
            * symbols.h (expand_expanded): Changed the declaration.
            * util.cc (cbl_field_t::encode_numeric): Use modified
            expand_expanded().

Diff:
---
 gcc/cobol/genapi.cc  | 33 +++++++++++++++++++-----
 gcc/cobol/symbols.cc | 72 +++++++++++++++++++++++++---------------------------
 gcc/cobol/symbols.h  | 10 ++++++--
 gcc/cobol/util.cc    |  2 +-
 4 files changed, 70 insertions(+), 47 deletions(-)

diff --git a/gcc/cobol/genapi.cc b/gcc/cobol/genapi.cc
index 09e89c6d2bf9..9012f8e66519 100644
--- a/gcc/cobol/genapi.cc
+++ b/gcc/cobol/genapi.cc
@@ -6838,12 +6838,19 @@ establish_using(size_t nusing,
 
           // That value is probably sixteen.  Subtract the length of our target
           // value from that.
-          tree offset =
+          IF( reference,
+              ne_op,
+              gg_cast(UCHAR_P, null_pointer_node) )
+            {
+            tree offset =
             gg_subtract(gg_array_value(var_decl_call_parameter_lengths, rt_i),
                        member(args[i].refer.field->var_decl_node, "capacity"));
-          // And add that value to 'reference'
-          gg_assign(reference,
-                    gg_add(reference, offset));
+            // And add that value to 'reference'
+            gg_assign(reference,
+                      gg_add(reference, offset));
+            }
+          ELSE
+            ENDIF
           // This code was added when I encountered a function call using
           // func( N - 1 ), where the function expected a single-byte value.
           // In little-endian, no problem, because the calculated value of 3
@@ -14811,7 +14818,7 @@ actually_create_the_static_field( cbl_field_t *new_var,
   char *expanded = expand_picture(new_var->data.picture);
   if( new_var->type == FldNumericEdited )
     {
-    expand_expanded(expanded);
+    expanded = expand_expanded(expanded);
     }
   tree picture = gg_string_literal(expanded);
   free(expanded);
@@ -15182,6 +15189,20 @@ void
 parser_symbol_add(struct cbl_field_t *new_var )
   {
   Analyze();
+
+  if( new_var->type == FldFloat )
+    {
+    /* Floating-point storage uses the target's native byte order. */
+    if( cobol_target_big_endian() )
+      {
+      new_var->attr |= big_endian_e;
+      }
+    else
+      {
+      new_var->attr &= ~big_endian_e;
+      }
+    }
+
   SHOW_PARSE
     {
     char ach[1024];
@@ -15634,7 +15655,7 @@ parser_symbol_add(struct cbl_field_t *new_var )
 
     if( level_88_string )
       {
-      new_var->data.original(level_88_string);
+      new_var->data.original_without_capacity(level_88_string);
       new_initial = level_88_string;
       length_of_initial_string = level_88_string_size;
       }
diff --git a/gcc/cobol/symbols.cc b/gcc/cobol/symbols.cc
index c1ba30c0cd09..a862211c633a 100644
--- a/gcc/cobol/symbols.cc
+++ b/gcc/cobol/symbols.cc
@@ -4840,19 +4840,10 @@ char *
 expand_picture(const char *picture)
   {
   // The caller should free() the return value.
-  assert(strlen(picture) < PICTURE_MAX); // guaranteed by picset() in scanner
-  size_t retval_length = PICTURE_MAX;
-
-  // In the expand_expanded routine, we are going to tack on some additional
-  // characters in order to speed up compile-time processin.
-  static const int PICTURE_EXTRA = 6;
-  retval_length += PICTURE_EXTRA;
-
-  char *retval = static_cast<char *>(xmalloc(retval_length));
+  std::string retval;
 
   int ch;
   int prior_ch = NULLCH;
-  char *d = retval;
   const char *p = picture;
   long repeat;
   int currency_symbol = NULLCH;
@@ -4873,16 +4864,16 @@ expand_picture(const char *picture)
       p = endchar + 1;
       while(repeat--)
         {
-        *d++ = prior_ch;
+        retval += prior_ch;
         }
       }
     else
       {
       prior_ch = ch;
-      *d++ = ch;
+      retval += ch;
       }
 
-    if( ! __gg__currency_signs[ch].empty() )
+    if( !__gg__currency_signs[ch].empty() )
       {
       // We are going to be mapping ch to a string in the final result:
       prior_ch = ch;
@@ -4890,39 +4881,36 @@ expand_picture(const char *picture)
       }
     }
 
-  size_t dest_length = d-retval;
-
   // We have to take into account the possibility that the currency symbol
   // mapping might be to a string of more than one character:
 
   if( currency_symbol )
     {
     size_t sign_length = __gg__currency_signs[currency_symbol].size();
-    assert(0 < sign_length);
+    gcc_assert(0 < sign_length);
     if( --sign_length )
       {
-      char *pcurrency = strchr(retval, currency_symbol);
-      assert(pcurrency);
-      memmove(    pcurrency + sign_length,
-                  pcurrency,
-                  dest_length - (pcurrency-retval));
+      // The currency sign text is more than one character.
+      // Copy everything up to the first currency symbol:
+      size_t nfound = retval.find(currency_symbol);
+      gcc_assert(nfound != std::string::npos);
+      std::string temp = retval.substr(0, nfound);
+      // Lay down 'B' to make room for the currency sign text
       for(size_t i=0; i<sign_length; i++)
         {
-        pcurrency[i] = ascii_B;
+        temp += ascii_B;
         }
-      dest_length += sign_length;
+      // And then tack on the rest of the original
+      temp += retval.substr(nfound);
+
+      // And that becomes the retval:
+      retval = temp;
       }
     }
-  retval[dest_length] = NULLCH;
-
-  // The 'V'
-
 
   // To ease the workload on interpreting the PICTURE string at run time, we
-  // are going to convert everything we can to upper case.  We also convert
-  // V to decimal point, for the same reason.  Characters that might be
-  // currency symbols have to be left in their uppercase or lowercase original
-  // state.
+  // are going to convert everything we can to upper case.
+  size_t dest_length = retval.size();
   for(size_t i=0; i<dest_length; i++)
     {
     switch(retval[i])
@@ -4961,23 +4949,30 @@ expand_picture(const char *picture)
     }
 
   // The V character is virtual in numeric_editing, so get rid of it
-  char *pV = strchr(retval, ascii_V);
-  if( pV )
+  size_t nfound = retval.find(ascii_V);
+  if( nfound != std::string::npos )
     {
-    memmove(pV, pV+1, strlen(pV));
+    retval.erase(nfound, 1);
     }
-  return retval;
+  return xstrdup(retval.c_str());
   }
 
-void
+char *
 expand_expanded(char *expanded)
   {
   if(strlen(expanded) == 0)
     {
-    return;
+    return expanded;
     }
 
-  unsigned char *dest = reinterpret_cast<unsigned char *>(expanded);
+  // expanded is a malloced numeric-edited string.  We need to make it six
+  // characters longer:
+
+  unsigned char *dest =
+                   static_cast<unsigned char *>(xmalloc(strlen(expanded)+ 7));
+  memcpy(dest, expanded, strlen(expanded)+1);
+  free(expanded);
+  expanded = reinterpret_cast<char *>(dest);
 
   /* In order to make __gg__string_to_numeric_edited() run quickly, we are
      going to process the expanded picture string especially for it.  What we
@@ -5176,6 +5171,7 @@ expand_expanded(char *expanded)
       }
     }
   strcat(expanded, extra);
+  return expanded;
   }
 
 int
diff --git a/gcc/cobol/symbols.h b/gcc/cobol/symbols.h
index 82dbddf605df..415a4a7905d0 100644
--- a/gcc/cobol/symbols.h
+++ b/gcc/cobol/symbols.h
@@ -47,7 +47,8 @@
 #include <string>
 #include <vector>
 
-// The ISO spec says there is a
+// The ISO spec says there is a limit on the length of the PICTURE
+// string in the source code.
 #define PICTURE_MAX 64
 
 extern const char *numed_message;
@@ -550,6 +551,11 @@ public:
     this->orig = orig_t( orig, all );
     return this->orig.data;
   }
+  // Save encoded data without deriving storage capacity from its bytes.
+  const char *original_without_capacity( const char *value ) {
+    orig = orig_t(value);
+    return orig.data;
+  }
   // Set the computed cce value.  Do not impute capacity.
   void original( REAL_VALUE_TYPE value ) {
     orig = orig_t(value);
@@ -3307,7 +3313,7 @@ cbl_field_t *new_alphanumeric(const cbl_name_t 
name=nullptr,
                               cbl_encoding_t encoding=no_encoding_e );
 
 char *expand_picture(const char *picture);
-void expand_expanded(char *picture);
+char *expand_expanded(char *picture);
 
 // ENABLE_HIJACKING allows for code generation to be "hijacked" when the
 // program-id is "dubner_h" or "hijack_h".  See the mainline code in genapi.cc.
diff --git a/gcc/cobol/util.cc b/gcc/cobol/util.cc
index b1944f4d7865..ded183ee83f2 100644
--- a/gcc/cobol/util.cc
+++ b/gcc/cobol/util.cc
@@ -1966,7 +1966,7 @@ cbl_field_t::encode_numeric( const char input[], 
cbl_loc_t loc ) {
             char *expanded = expand_picture(data.picture);
             if( type == FldNumericEdited )
               {
-              expand_expanded(expanded);
+              expanded = expand_expanded(expanded);
               }
             __gg__string_to_numeric_edited( reinterpret_cast<char *>(retval),
                                             ach,

Reply via email to