From 491e7800a33f7291099205da1377850a3daaf93b Mon Sep 17 00:00:00 2001 From: tezc Date: Wed, 24 Feb 2021 09:53:27 +0300 Subject: [PATCH] buf-fix --- buffer/CMakeLists.txt | 2 +- buffer/buf_test.c | 10 ++++++++++ buffer/sc_buf.c | 8 ++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/buffer/CMakeLists.txt b/buffer/CMakeLists.txt index 6ddc38d..6aa881d 100644 --- a/buffer/CMakeLists.txt +++ b/buffer/CMakeLists.txt @@ -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 diff --git a/buffer/buf_test.c b/buffer/buf_test.c index 83df6f8..14773ad 100644 --- a/buffer/buf_test.c +++ b/buffer/buf_test.c @@ -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() diff --git a/buffer/sc_buf.c b/buffer/sc_buf.c index 336a63f..b0dd04b 100644 --- a/buffer/sc_buf.c +++ b/buffer/sc_buf.c @@ -27,6 +27,10 @@ #include #include +#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; }