Skip to content

Commit

Permalink
[ fixed #242 ] segfault due to buffer overrun in C++ printer
Browse files Browse the repository at this point in the history
After some buffer resizings, trying to strcpy the buffer into a buffer
of the initial size will fail spectacularly.  Exposed on Mac OS X.

The copying was anyway silly if the buffer is to be filled with zeros
afterwards, thus, simply don't do it!
  • Loading branch information
andreasabel committed Nov 4, 2018
1 parent e2e7f9a commit a4ae2a4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions source/src/BNFC/Backend/CPP/PrettyPrinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,15 @@ mkHFile useStl inPackage cf groups = unlines
if useStl then render (nest 2 bufAppendString) else "",
" void inline bufReset(void)",
" {",
" cur_ = 0;",
" if (buf_) free(buf_);",
" buf_size = " ++ nsDefine inPackage "BUFFER_INITIAL" ++ ";",
" resizeBuffer();",
" buf_ = (char *) malloc(buf_size);",
" if (!buf_) {",
" fprintf(stderr, \"Error: Out of memory while allocating buffer!\\n\");",
" exit(1);",
" }",
" memset(buf_, 0, buf_size);",
" cur_ = 0;",
" }",
"",
" void inline resizeBuffer(void)",
Expand Down

0 comments on commit a4ae2a4

Please sign in to comment.