Merge pull request #68 from tezc/str-fix

bounds check in sc_str to prevent misuse
This commit is contained in:
Ozan Tezcan 2021-04-08 13:43:56 +03:00 committed by GitHub
commit ac312c7192
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -18,6 +18,12 @@ void test1()
assert(sc_buf_valid(&buf) == false);
sc_buf_term(&buf);
sc_buf_init(&buf, 100);
sc_buf_put_32(&buf, 1000);
assert(sc_buf_get_str(&buf) == NULL);
assert(sc_buf_valid(&buf) == false);
sc_buf_term(&buf);
sc_buf_init(&buf, 100);
sc_buf_init(&buf2, 100);
sc_buf_put_str(&buf, "s");

View File

@ -565,6 +565,11 @@ const char *sc_buf_get_str(struct sc_buf *b)
return NULL;
}
if (b->rpos + len > b->wpos) {
b->err |= SC_BUF_CORRUPT;
return NULL;
}
str = (char *) b->mem + b->rpos;
b->rpos += len + 1;