aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2010-10-03 13:31:02 +0200
committerAris Adamantiadis <aris@0xbadc0de.be>2010-10-03 13:31:02 +0200
commit9a89fba3329d7557f9415c5d629b0bf783fe5685 (patch)
treee42694d05c75a978f2356bd4e1013426668ce345
parent0bc032726dbd40b2ee478c5f34b4bcfd04627115 (diff)
downloadlibssh-9a89fba3329d7557f9415c5d629b0bf783fe5685.tar.gz
libssh-9a89fba3329d7557f9415c5d629b0bf783fe5685.tar.xz
libssh-9a89fba3329d7557f9415c5d629b0bf783fe5685.zip
resolve memory consumption bug
-rw-r--r--src/buffer.c18
-rw-r--r--tests/unittests/torture_buffer.c3
2 files changed, 19 insertions, 2 deletions
diff --git a/src/buffer.c b/src/buffer.c
index fd07177c..27d2592d 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -114,7 +114,7 @@ static int realloc_buffer(struct ssh_buffer_struct *buffer, int needed) {
char *new = NULL;
buffer_verify(buffer);
/* Find the smallest power of two which is greater or equal to needed */
- while(smallest < needed) {
+ while(smallest <= needed) {
smallest <<= 1;
}
needed = smallest;
@@ -128,6 +128,20 @@ static int realloc_buffer(struct ssh_buffer_struct *buffer, int needed) {
return 0;
}
+/** @internal
+ * @brief shifts a buffer to remove unused data in the beginning
+ * @param buffer SSH buffer
+ */
+static void buffer_shift(ssh_buffer buffer){
+ buffer_verify(buffer);
+ if(buffer->pos==0)
+ return;
+ memmove(buffer->data, buffer->data + buffer->pos, buffer->used - buffer->pos);
+ buffer->used -= buffer->pos;
+ buffer->pos=0;
+ buffer_verify(buffer);
+}
+
/**
* @internal
*
@@ -167,6 +181,8 @@ int buffer_reinit(struct ssh_buffer_struct *buffer) {
int buffer_add_data(struct ssh_buffer_struct *buffer, const void *data, uint32_t len) {
buffer_verify(buffer);
if (buffer->allocated < (buffer->used + len)) {
+ if(buffer->pos > 0)
+ buffer_shift(buffer);
if (realloc_buffer(buffer, buffer->used + len) < 0) {
return -1;
}
diff --git a/tests/unittests/torture_buffer.c b/tests/unittests/torture_buffer.c
index 5653c4db..32ce102c 100644
--- a/tests/unittests/torture_buffer.c
+++ b/tests/unittests/torture_buffer.c
@@ -1,7 +1,8 @@
#define LIBSSH_STATIC
#include "torture.h"
-#include "libssh/buffer.h"
+#define DEBUG_BUFFER
+#include "buffer.c"
ssh_buffer buffer;
#define LIMIT (8*1024*1024)