From ceaea65296a9cb6e2235b0dd429ab3d590b634d3 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Tue, 15 Mar 2016 15:44:57 +0100 Subject: lib: Updated du_buff not limit allowable PCI size PCI can now occupy at most one block --- src/lib/du_buff.c | 31 ++++++++++++++++--------------- src/lib/tests/du_buff_test.c | 20 ++++++++++++++------ 2 files changed, 30 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/lib/du_buff.c b/src/lib/du_buff.c index cf523847..7e0f502e 100644 --- a/src/lib/du_buff.c +++ b/src/lib/du_buff.c @@ -24,16 +24,15 @@ #include #include #include -#include "ouroboros/du_buff.h" +#include +#include #define OUROBOROS_PREFIX "du_buff" -#ifndef DU_BUFF_BLOCKSIZE -#define DU_BUFF_BLOCKSIZE (1 << 16) -#endif - #include "ouroboros/logs.h" +#define DU_BLOCK_DATA_SIZE (DU_BUFF_BLOCK_SIZE - sizeof (struct buffer)) + struct buffer { uint8_t * data; size_t size; @@ -79,9 +78,13 @@ struct buffer * buffer_create (size_t size, size_t headspace, size_t len) { struct buffer * head = NULL; size_t remaining = size; - const size_t page_size = DU_BUFF_BLOCKSIZE; size_t ts = size - (headspace + len); - bool head_block = true; + + if (headspace > DU_BLOCK_DATA_SIZE || ts > DU_BLOCK_DATA_SIZE) + { + LOG_WARN("Illegal du_buff. Cannot fit PCI in DU_BUFF_BLOCK."); + return NULL; + } head = malloc(sizeof *head); if (head == NULL) @@ -97,17 +100,15 @@ struct buffer * buffer_create (size_t size, size_t headspace, size_t len) size_t sz; - if (size > DU_BUFF_BLOCKSIZE && head_block) { - sz = headspace; - head_block = false; - } else if (size > DU_BUFF_BLOCKSIZE - && remaining - ts <= DU_BUFF_BLOCKSIZE + if (size > DU_BLOCK_DATA_SIZE + && remaining - ts <= DU_BLOCK_DATA_SIZE && remaining != ts) { sz = remaining - ts; - } else if (size > DU_BUFF_BLOCKSIZE && remaining == ts) { + } else if (size > DU_BLOCK_DATA_SIZE && remaining == ts) { sz = ts; } else { - sz = remaining < page_size ? remaining : page_size; + sz = remaining < DU_BLOCK_DATA_SIZE ? + remaining : DU_BLOCK_DATA_SIZE; } buf = malloc(sizeof *buf); @@ -290,7 +291,7 @@ int du_buff_init(du_buff_t * dub, } if (start + len > dub->size) { - LOG_DBGF("Buffer too small for data %lu.", start); + LOG_DBGF("Buffer too small for data."); return -EINVAL; } diff --git a/src/lib/tests/du_buff_test.c b/src/lib/tests/du_buff_test.c index 7bdf1459..2b7b5e55 100644 --- a/src/lib/tests/du_buff_test.c +++ b/src/lib/tests/du_buff_test.c @@ -20,10 +20,12 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#include #include "du_buff.c" -#define TEST_BUFF_SIZE 16 * DU_BUFF_BLOCKSIZE -#define MAX(a,b) a > b ? a : b +#define TEST_BUFF_SIZE 16 * DU_BLOCK_DATA_SIZE +#define MAX(a,b) (a > b ? a : b) +#define MIN(a,b) (a < b ? a : b) int du_buff_test(int argc, char ** argv) { @@ -35,17 +37,23 @@ int du_buff_test(int argc, char ** argv) for (i = 0; i < TEST_BUFF_SIZE; i++) bits[i] = 170; - i_inc = MAX(1, DU_BUFF_BLOCKSIZE / 4); - j_inc = MAX(1, DU_BUFF_BLOCKSIZE / 8); - k_inc = MAX(1, DU_BUFF_BLOCKSIZE / 16); + i_inc = MAX(1, DU_BLOCK_DATA_SIZE / 4); + j_inc = MAX(1, DU_BLOCK_DATA_SIZE / 8); + k_inc = MAX(1, DU_BLOCK_DATA_SIZE / 16); - for (i = DU_BUFF_BLOCKSIZE / 4; i <= TEST_BUFF_SIZE; i += i_inc) { + for (i = DU_BUFF_BLOCK_SIZE / 4; i <= TEST_BUFF_SIZE; i += i_inc) { for (j = 0; j < i; j += j_inc) { for (k = 0; k < i - j; k += k_inc) { du_buff_t * dub = du_buff_create(i); if (dub == NULL) return -1; + if (k > DU_BLOCK_DATA_SIZE) + continue; + + if (i - (j + k) > DU_BLOCK_DATA_SIZE) + continue; + if (du_buff_init(dub, k, bits, j) < 0) return -1; -- cgit v1.2.3 From 69fd7316e545bd94da2b066362ebf58dd945cbb3 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Tue, 15 Mar 2016 15:52:35 +0100 Subject: lib: indentation fixes in du_buff --- src/lib/du_buff.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/lib/du_buff.c b/src/lib/du_buff.c index 7e0f502e..a13e520d 100644 --- a/src/lib/du_buff.c +++ b/src/lib/du_buff.c @@ -80,8 +80,7 @@ struct buffer * buffer_create (size_t size, size_t headspace, size_t len) size_t remaining = size; size_t ts = size - (headspace + len); - if (headspace > DU_BLOCK_DATA_SIZE || ts > DU_BLOCK_DATA_SIZE) - { + if (headspace > DU_BLOCK_DATA_SIZE || ts > DU_BLOCK_DATA_SIZE) { LOG_WARN("Illegal du_buff. Cannot fit PCI in DU_BUFF_BLOCK."); return NULL; } @@ -104,11 +103,11 @@ struct buffer * buffer_create (size_t size, size_t headspace, size_t len) && remaining - ts <= DU_BLOCK_DATA_SIZE && remaining != ts) { sz = remaining - ts; - } else if (size > DU_BLOCK_DATA_SIZE && remaining == ts) { + } else if (size > DU_BLOCK_DATA_SIZE && remaining == ts) { sz = ts; } else { - sz = remaining < DU_BLOCK_DATA_SIZE ? - remaining : DU_BLOCK_DATA_SIZE; + sz = remaining < DU_BLOCK_DATA_SIZE ? + remaining : DU_BLOCK_DATA_SIZE; } buf = malloc(sizeof *buf); @@ -249,7 +248,7 @@ int buffer_copy_data(struct buffer * head, du_buff_t * du_buff_create(size_t size) { - du_buff_t * dub = (du_buff_t *)malloc(sizeof(du_buff_t)); + du_buff_t * dub = malloc(sizeof *dub); if (dub == NULL) { LOG_DBGF("Bogus input, bugging out."); -- cgit v1.2.3 From 8c649a56e61012d0b245defbdea3ff5c3acb21c1 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Tue, 15 Mar 2016 16:02:08 +0100 Subject: lib: du_buff log an error when PCI > block size du_buff.c: LOG_WARN changed to LOG_ERR --- src/lib/du_buff.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/lib/du_buff.c b/src/lib/du_buff.c index a13e520d..918ee57d 100644 --- a/src/lib/du_buff.c +++ b/src/lib/du_buff.c @@ -81,7 +81,7 @@ struct buffer * buffer_create (size_t size, size_t headspace, size_t len) size_t ts = size - (headspace + len); if (headspace > DU_BLOCK_DATA_SIZE || ts > DU_BLOCK_DATA_SIZE) { - LOG_WARN("Illegal du_buff. Cannot fit PCI in DU_BUFF_BLOCK."); + LOG_ERR("Illegal du_buff: Cannot fit PCI in DU_BUFF_BLOCK."); return NULL; } -- cgit v1.2.3