mirror of
https://github.com/tezc/sc.git
synced 2025-01-14 06:43:04 +08:00
sc_buf set_rpos and set_wpos checks. Don't compact on reserve. (#65)
* sc_buf set_rpos and set_wpos checks. Don't compact on reserve.
This commit is contained in:
parent
6653078fd2
commit
ded2b9429c
@ -10,6 +10,15 @@ void test1()
|
||||
const char* xstr;
|
||||
struct sc_buf buf, buf2;
|
||||
|
||||
sc_buf_init(&buf, 100);
|
||||
sc_buf_set_rpos(&buf, 1);
|
||||
assert(sc_buf_valid(&buf) == false);
|
||||
sc_buf_clear(&buf);
|
||||
assert(sc_buf_valid(&buf) == true);
|
||||
sc_buf_set_wpos(&buf, 101);
|
||||
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");
|
||||
|
@ -93,30 +93,28 @@ bool sc_buf_reserve(struct sc_buf *buf, uint32_t len)
|
||||
|
||||
if (buf->wpos + len > buf->cap) {
|
||||
if (buf->ref) {
|
||||
return false;
|
||||
goto error;
|
||||
}
|
||||
|
||||
sc_buf_compact(buf);
|
||||
|
||||
if (buf->wpos + len > buf->cap) {
|
||||
size = ((buf->cap + len + 4095) / 4096) * 4096;
|
||||
if (size > buf->limit || buf->cap >= SC_BUF_SIZE_MAX - 4096) {
|
||||
buf->error |= SC_BUF_OOM;
|
||||
return false;
|
||||
}
|
||||
|
||||
tmp = sc_buf_realloc(buf->mem, size);
|
||||
if (tmp == NULL) {
|
||||
buf->error |= SC_BUF_OOM;
|
||||
return false;
|
||||
}
|
||||
|
||||
buf->cap = size;
|
||||
buf->mem = tmp;
|
||||
size = ((buf->cap + len + 4095) / 4096) * 4096;
|
||||
if (size > buf->limit || buf->cap >= SC_BUF_SIZE_MAX - 4096) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
tmp = sc_buf_realloc(buf->mem, size);
|
||||
if (tmp == NULL) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
buf->cap = size;
|
||||
buf->mem = tmp;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
error:
|
||||
buf->error |= SC_BUF_OOM;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool sc_buf_shrink(struct sc_buf *buf, uint32_t len)
|
||||
@ -182,13 +180,19 @@ uint32_t sc_buf_rpos(struct sc_buf *buf)
|
||||
|
||||
void sc_buf_set_rpos(struct sc_buf *buf, uint32_t pos)
|
||||
{
|
||||
assert(buf->wpos >= pos);
|
||||
if (pos > buf->wpos) {
|
||||
buf->error |= SC_BUF_CORRUPT;
|
||||
}
|
||||
|
||||
buf->rpos = pos;
|
||||
}
|
||||
|
||||
void sc_buf_set_wpos(struct sc_buf *buf, uint32_t pos)
|
||||
{
|
||||
assert(pos <= buf->cap);
|
||||
if (pos > buf->cap) {
|
||||
buf->error |= SC_BUF_CORRUPT;
|
||||
}
|
||||
|
||||
buf->wpos = pos;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user