fix illegal memory access when input file does not end with line break

Sun, 11 Jun 2023 15:16:48 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 11 Jun 2023 15:16:48 +0200
changeset 70
60cecca5e484
parent 69
ff56b28e2cdd
child 71
d5af9261231d

fix illegal memory access when input file does not end with line break

src/c2html.c file | annotate | diff | comparison | revisions
src/c2html.h file | annotate | diff | comparison | revisions
src/frontend.c file | annotate | diff | comparison | revisions
test/ctest.c file | annotate | diff | comparison | revisions
test/gs/ctest.html file | annotate | diff | comparison | revisions
--- a/src/c2html.c	Sun Jun 11 14:05:28 2023 +0200
+++ b/src/c2html.c	Sun Jun 11 15:16:48 2023 +0200
@@ -31,6 +31,7 @@
 
 #include <cx/array_list.h>
 #include <cx/printf.h>
+#include <string.h>
 
 size_t c2html_format(
         CxList const *lines,
@@ -92,15 +93,15 @@
     return written;
 }
 
-size_t c2html_bformat(
+size_t c2html_textformat(
         char const *inputbuffer,
-        size_t inputbuflen,
         void *outbuf,
         cx_write_func wfnc,
         c2html_highlighter_func highlighter,
         int showln
 ) {
     /* a rough estimate for the number of lines */
+    size_t inputbuflen = strlen(inputbuffer);
     size_t est_cap = 16 + inputbuflen / 40;
 
     /* create the line pointer array */
--- a/src/c2html.h	Sun Jun 11 14:05:28 2023 +0200
+++ b/src/c2html.h	Sun Jun 11 15:16:48 2023 +0200
@@ -45,8 +45,7 @@
 /**
  * Writes the formatted source data to the output buffer.
  * 
- * @param inputbuffer the source file data as string
- * @param inputbuflen the length of the source file
+ * @param inputtext the source file data as zero-terminated string
  * @param outbuf the output buffer
  * @param wfnc a write function for the output buffer
  * @param hltr the highlighter function
@@ -58,9 +57,9 @@
  * @see c2html_c_highlighter()
  * @see c2html_java_highlighter()
  */
-size_t c2html_bformat(char const* inputbuffer, size_t inputbuflen,
-                      void* outbuf, cx_write_func wfnc,
-                      c2html_highlighter_func hltr, int showln);
+size_t c2html_textformat(char const* inputtext,
+                         void* outbuf, cx_write_func wfnc,
+                         c2html_highlighter_func hltr, int showln);
 
 /**
  * Writes the formatted source data to the output buffer.
--- a/src/frontend.c	Sun Jun 11 14:05:28 2023 +0200
+++ b/src/frontend.c	Sun Jun 11 15:16:48 2023 +0200
@@ -150,10 +150,10 @@
             cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND);
             cx_stream_copy(inputfile, &fbuf, (cx_read_func) fread,
                            (cx_write_func) cxBufferWrite);
+            cxBufferPut(&fbuf, 0);
             fclose(inputfile);
-            c2html_bformat(
-                    fbuf.space, fbuf.size,
-                    fout, (cx_write_func ) fwrite, hltr,
+            c2html_textformat(
+                    fbuf.space, fout, (cx_write_func ) fwrite, hltr,
                     settings.showlinenumbers
             );
             cxBufferDestroy(&fbuf);
--- a/test/ctest.c	Sun Jun 11 14:05:28 2023 +0200
+++ b/test/ctest.c	Sun Jun 11 15:16:48 2023 +0200
@@ -388,4 +388,4 @@
         str.length = 0;
         return str;
     }
-}
+}
\ No newline at end of file
--- a/test/gs/ctest.html	Sun Jun 11 14:05:28 2023 +0200
+++ b/test/gs/ctest.html	Sun Jun 11 15:16:48 2023 +0200
@@ -441,8 +441,7 @@
 <a class="c2html-lineno" name="l388" href="#l388">388 </a>        str.length = <span class="c2html-macroconst">0</span>;
 <a class="c2html-lineno" name="l389" href="#l389">389 </a>        <span class="c2html-keyword">return</span> str;
 <a class="c2html-lineno" name="l390" href="#l390">390 </a>    }
-<a class="c2html-lineno" name="l391" href="#l391">391 </a>}
-</div>
+<a class="c2html-lineno" name="l391" href="#l391">391 </a>}</div>
   </body>
 </html>
 

mercurial