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
     1.1 --- a/src/c2html.c	Sun Jun 11 14:05:28 2023 +0200
     1.2 +++ b/src/c2html.c	Sun Jun 11 15:16:48 2023 +0200
     1.3 @@ -31,6 +31,7 @@
     1.4  
     1.5  #include <cx/array_list.h>
     1.6  #include <cx/printf.h>
     1.7 +#include <string.h>
     1.8  
     1.9  size_t c2html_format(
    1.10          CxList const *lines,
    1.11 @@ -92,15 +93,15 @@
    1.12      return written;
    1.13  }
    1.14  
    1.15 -size_t c2html_bformat(
    1.16 +size_t c2html_textformat(
    1.17          char const *inputbuffer,
    1.18 -        size_t inputbuflen,
    1.19          void *outbuf,
    1.20          cx_write_func wfnc,
    1.21          c2html_highlighter_func highlighter,
    1.22          int showln
    1.23  ) {
    1.24      /* a rough estimate for the number of lines */
    1.25 +    size_t inputbuflen = strlen(inputbuffer);
    1.26      size_t est_cap = 16 + inputbuflen / 40;
    1.27  
    1.28      /* create the line pointer array */
     2.1 --- a/src/c2html.h	Sun Jun 11 14:05:28 2023 +0200
     2.2 +++ b/src/c2html.h	Sun Jun 11 15:16:48 2023 +0200
     2.3 @@ -45,8 +45,7 @@
     2.4  /**
     2.5   * Writes the formatted source data to the output buffer.
     2.6   * 
     2.7 - * @param inputbuffer the source file data as string
     2.8 - * @param inputbuflen the length of the source file
     2.9 + * @param inputtext the source file data as zero-terminated string
    2.10   * @param outbuf the output buffer
    2.11   * @param wfnc a write function for the output buffer
    2.12   * @param hltr the highlighter function
    2.13 @@ -58,9 +57,9 @@
    2.14   * @see c2html_c_highlighter()
    2.15   * @see c2html_java_highlighter()
    2.16   */
    2.17 -size_t c2html_bformat(char const* inputbuffer, size_t inputbuflen,
    2.18 -                      void* outbuf, cx_write_func wfnc,
    2.19 -                      c2html_highlighter_func hltr, int showln);
    2.20 +size_t c2html_textformat(char const* inputtext,
    2.21 +                         void* outbuf, cx_write_func wfnc,
    2.22 +                         c2html_highlighter_func hltr, int showln);
    2.23  
    2.24  /**
    2.25   * Writes the formatted source data to the output buffer.
     3.1 --- a/src/frontend.c	Sun Jun 11 14:05:28 2023 +0200
     3.2 +++ b/src/frontend.c	Sun Jun 11 15:16:48 2023 +0200
     3.3 @@ -150,10 +150,10 @@
     3.4              cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND);
     3.5              cx_stream_copy(inputfile, &fbuf, (cx_read_func) fread,
     3.6                             (cx_write_func) cxBufferWrite);
     3.7 +            cxBufferPut(&fbuf, 0);
     3.8              fclose(inputfile);
     3.9 -            c2html_bformat(
    3.10 -                    fbuf.space, fbuf.size,
    3.11 -                    fout, (cx_write_func ) fwrite, hltr,
    3.12 +            c2html_textformat(
    3.13 +                    fbuf.space, fout, (cx_write_func ) fwrite, hltr,
    3.14                      settings.showlinenumbers
    3.15              );
    3.16              cxBufferDestroy(&fbuf);
     4.1 --- a/test/ctest.c	Sun Jun 11 14:05:28 2023 +0200
     4.2 +++ b/test/ctest.c	Sun Jun 11 15:16:48 2023 +0200
     4.3 @@ -388,4 +388,4 @@
     4.4          str.length = 0;
     4.5          return str;
     4.6      }
     4.7 -}
     4.8 +}
     4.9 \ No newline at end of file
     5.1 --- a/test/gs/ctest.html	Sun Jun 11 14:05:28 2023 +0200
     5.2 +++ b/test/gs/ctest.html	Sun Jun 11 15:16:48 2023 +0200
     5.3 @@ -441,8 +441,7 @@
     5.4  <a class="c2html-lineno" name="l388" href="#l388">388 </a>        str.length = <span class="c2html-macroconst">0</span>;
     5.5  <a class="c2html-lineno" name="l389" href="#l389">389 </a>        <span class="c2html-keyword">return</span> str;
     5.6  <a class="c2html-lineno" name="l390" href="#l390">390 </a>    }
     5.7 -<a class="c2html-lineno" name="l391" href="#l391">391 </a>}
     5.8 -</div>
     5.9 +<a class="c2html-lineno" name="l391" href="#l391">391 </a>}</div>
    5.10    </body>
    5.11  </html>
    5.12  

mercurial