This commit is contained in:
tezc 2021-02-24 09:53:27 +03:00
parent ab8abe49f8
commit 491e7800a3
3 changed files with 17 additions and 3 deletions

View File

@ -29,7 +29,7 @@ if(SC_BUILD_TEST)
add_executable(${PROJECT_NAME}_test buf_test.c sc_buf.c)
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_SIZE_MAX=140000ul)
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_BUF_SIZE_MAX=1400000ul)
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR

View File

@ -174,6 +174,16 @@ void test1()
assert(sc_buf_valid(&buf));
sc_buf_term(&buf);
sc_buf_init(&buf, 100);
sc_buf_limit(&buf, 100000000);
for (int i = 0; i < 1000000; i++) {
sc_buf_put_str(&buf, "teesssssssssssssssssssssssssssssssssssssssssss");
}
assert(sc_buf_valid(&buf) == false);
sc_buf_term(&buf);
}
void test2()

View File

@ -27,6 +27,10 @@
#include <assert.h>
#include <stdio.h>
#ifndef SC_BUF_SIZE_MAX
#define SC_BUF_SIZE_MAX UINT32_MAX
#endif
#define sc_buf_min(a, b) ((a) > (b) ? (b) : (a))
bool sc_buf_init(struct sc_buf *buf, uint32_t cap)
@ -51,7 +55,7 @@ struct sc_buf sc_buf_wrap(void *data, uint32_t len, int flags)
{
struct sc_buf buf = {.mem = data,
.cap = len,
.limit = flags & SC_BUF_REF ? len : UINT32_MAX,
.limit = flags & SC_BUF_REF ? len : SC_BUF_SIZE_MAX,
.wpos = flags & SC_BUF_DATA ? len : 0,
.rpos = 0,
.ref = (bool) (flags & SC_BUF_REF),
@ -96,7 +100,7 @@ bool sc_buf_reserve(struct sc_buf *buf, uint32_t len)
if (buf->wpos + len > buf->cap) {
size = ((buf->cap + len + 4095) / 4096) * 4096;
if (size > buf->limit) {
if (size > buf->limit || buf->cap >= SC_BUF_SIZE_MAX - 4096) {
buf->error |= SC_BUF_OOM;
return false;
}