[gcc r17-2733] cobol: Accept NUL in comments.

James K. Lowden jklowden@gcc.gnu.org
Mon Jul 27 19:22:24 GMT 2026


https://gcc.gnu.org/g:4eb61759ef5b2a4f67c391f4bbd31d6f9178967f

commit r17-2733-g4eb61759ef5b2a4f67c391f4bbd31d6f9178967f
Author: James K. Lowden <jklowden@cobolworx.com>
Date:   Mon Jul 27 14:51:19 2026 -0400

    cobol: Accept NUL in comments.
    
    Previously gcobol reported an error if a NUL appeared in the
    input. Now a NUL in a comment does not affect compilation.
    
    gcc/cobol/ChangeLog:
    
            * lexio.cc (cdftext::free_form_reference_format): Use new sanitize_nul function.
            * lexio.h (struct filespan_t): Define sanitize_nul().

Diff:
---
 gcc/cobol/lexio.cc |  3 +++
 gcc/cobol/lexio.h  | 30 +++++++++++++++++-------------
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/gcc/cobol/lexio.cc b/gcc/cobol/lexio.cc
index 44ca02cc8aa9..39ad4e0ceff9 100644
--- a/gcc/cobol/lexio.cc
+++ b/gcc/cobol/lexio.cc
@@ -1785,6 +1785,7 @@ cdftext::free_form_reference_format( int input,
             erase_source(p, mfile.eol);
           }
         }
+        gcc_assert( ! mfile.line_contains_nul() );
         continue;
       case SPACE:
         break;
@@ -1808,6 +1809,7 @@ cdftext::free_form_reference_format( int input,
         if( indcol < mfile.eol - 1 ) {
           erase_source(indcol, mfile.eol);
         }
+        mfile.sanitize_nul();
         continue;
       case '$':
         if( lexio_dialect_mf() ) {
@@ -1825,6 +1827,7 @@ cdftext::free_form_reference_format( int input,
         break;
       }
     }
+    mfile.sanitize_nul();
     current.line.update(mfile.cur, mfile.eol, format.top().right_margin());
     current.lineno = mfile.lineno();
   } // next line
diff --git a/gcc/cobol/lexio.h b/gcc/cobol/lexio.h
index 2e64d936fcc4..17af7bbd57d9 100644
--- a/gcc/cobol/lexio.h
+++ b/gcc/cobol/lexio.h
@@ -152,6 +152,23 @@ struct filespan_t : public bytespan_t {
    */
   bool was_quote72() const { return iline == line_quote72 + 1; }
 
+  bool line_contains_nul() const {
+    char *nul = std::find(cur, eol, '\0');
+    return nul != eol;
+  }
+
+  void sanitize_nul() {
+    char *nul = std::find(cur, eol, '\0');
+    if( nul != eol ) {
+      int icol = nul - cur; // cppcheck-suppress shadowVariable
+      fprintf(stderr, "%s:%d:%d: error: NUL character detected in input\n%*s\n",
+              cobol_filename(), int(iline), ++icol,
+              int(eol - cur)-1, cur);
+      parse_error_inc();
+      std::replace(nul, eol, '\0', SPACE);
+    }
+  }
+
   size_t next_line(bool is_reference_format) {
     // Before advancing, mark the current line as ending in a quote, if true.
     if( is_reference_format && 72 <= line_length() ) {
@@ -164,19 +181,6 @@ struct filespan_t : public bytespan_t {
 
     eol = std::find(cur, eodata, '\n');
 
-    char *nul = std::find(cur, eol, '\0');
-    if( nul != eol ) {
-      if( std::any_of( nul, eodata,
-                       []( char ch ) { return ch != '\0'; } ) ) {
-        int icol = nul - cur; // cppcheck-suppress shadowVariable
-        fprintf(stderr, "%s:%d:%d: error: NUL character detected in input\n%*s\n",
-                cobol_filename(), int(iline + 1), ++icol,
-                int(eol - cur), cur);
-        parse_error_inc();
-        std::replace(nul, eol, '\0', SPACE);
-      }
-    }
-
     if( eol < eodata ) {
       ++eol;
       ++iline;


More information about the Gcc-cvs mailing list