Skip to content
Permalink
Browse files

fixes intermittent glitch in esl_buffer_utest detected by valgrind

esl_buffer.c::buffer_counttok line ~1859 checks for \r\n newline,
because it may need to back nc up by one. However, if input ends with
token, [bf->pos+nc] is off the end of the input, so the test for \n
there needs some protection. Add check for bf->pos+nc < bf->n.
  • Loading branch information...
cryptogenomicon committed Jun 4, 2018
1 parent f63ef75 commit b166d24e175fb5e5637959d8bdd89d52c6d0d08c
Showing with 3 additions and 2 deletions.
  1. +3 −2 esl_buffer.c
@@ -1855,9 +1855,10 @@ buffer_counttok(ESL_BUFFER *bf, const char *sep, esl_pos_t *ret_nc)
if ( (status = buffer_refill(bf, nc)) != eslOK && status != eslEOF) goto ERROR;
} while (bf->n - bf->pos > nc);

if (bf->mem[bf->pos+nc] == '\n' && bf->mem[bf->pos+nc-1] == '\r') { nc--; }
// check for \r\n newline, but beware, bf->pos+nc can be off edge of bf->mem, if input ends with token.
if (bf->pos+nc < bf->n && bf->mem[bf->pos+nc] == '\n' && bf->mem[bf->pos+nc-1] == '\r') { nc--; }

/* bf->mem[bf->pos+nc] now sitting on the first char that's in sep, or a newline char */
/* if still in input, bf->mem[bf->pos+nc] now sitting on the first char that's in sep, or a newline char */
*ret_nc = nc;
return eslOK;

0 comments on commit b166d24

Please sign in to comment.
You can’t perform that action at this time.