diff options
author | Thomas Vigouroux <39092278+vigoux@users.noreply.github.com> | 2020-04-22 18:54:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-22 18:54:56 +0200 |
commit | 6c9a5743a0c296e74a48368a65783b9d49e6f702 (patch) | |
tree | ec9d5a7d20854ed4e7f0c6af4ed823b4745d2a22 | |
parent | fdedaa7226ffa411d023f37c3c5dbae984ca0e38 (diff) | |
download | rneovim-6c9a5743a0c296e74a48368a65783b9d49e6f702.tar.gz rneovim-6c9a5743a0c296e74a48368a65783b9d49e6f702.tar.bz2 rneovim-6c9a5743a0c296e74a48368a65783b9d49e6f702.zip |
treesitter: check for integer overflow (#12135)
Sometimes treesitter calls for an invalid column within a line, checking
that the column is actually valid and forcing the value avoids an
integer overflow and an infinite sequence of invalid reads.
Fixes #12131
-rw-r--r-- | src/nvim/lua/treesitter.c | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 4753df7b87..51d9549033 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -271,17 +271,22 @@ static const char *input_cb(void *payload, uint32_t byte_index, } char_u *line = ml_get_buf(bp, position.row+1, false); size_t len = STRLEN(line); - size_t tocopy = MIN(len-position.column, BUFSIZE); - - memcpy(buf, line+position.column, tocopy); - // Translate embedded \n to NUL - memchrsub(buf, '\n', '\0', tocopy); - *bytes_read = (uint32_t)tocopy; - if (tocopy < BUFSIZE) { - // now add the final \n. If it didn't fit, input_cb will be called again - // on the same line with advanced column. - buf[tocopy] = '\n'; - (*bytes_read)++; + + if (position.column > len) { + *bytes_read = 0; + } else { + size_t tocopy = MIN(len-position.column, BUFSIZE); + + memcpy(buf, line+position.column, tocopy); + // Translate embedded \n to NUL + memchrsub(buf, '\n', '\0', tocopy); + *bytes_read = (uint32_t)tocopy; + if (tocopy < BUFSIZE) { + // now add the final \n. If it didn't fit, input_cb will be called again + // on the same line with advanced column. + buf[tocopy] = '\n'; + (*bytes_read)++; + } } return buf; #undef BUFSIZE |