From 34d681a00a7fc3c8d82a5ba3082dd5524f2a8389 Mon Sep 17 00:00:00 2001
From: Chernov Dmitriy <cd_work@mail.ru>
Date: 周二, 01 3月 2016 21:26:22 +0800
Subject: [PATCH] The basic implementation completed. Tests are needed. (issue #73)

---
 libscpi/src/utils.c |   19 +++++++++++++------
 1 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c
index de8fb64..b2b9e03 100644
--- a/libscpi/src/utils.c
+++ b/libscpi/src/utils.c
@@ -763,14 +763,16 @@
 	if(!s || !heap) {
 		return NULL;
 	}
+
 	if(heap->data[heap->wr]!='\0'){
 		return NULL;
 	}
 	
-	size_t len=strlen(s) + 1; // additional '\0' at end
-	if(len > heap->count){
+	size_t len=strlen(s);
+	if( ( len == 0 ) || ( len > heap->count ) ){
 		return NULL;
 	}
+	len++;	// additional '\0' at end
 	char * ptrs = s;
 	char * head = &heap->data[heap->wr];
 	size_t rem = heap->size - (&heap->data[heap->wr]-heap->data);
@@ -839,7 +841,6 @@
 		
 	if( !OUR_get_parts( heap, s, &len[0], &data_add, &len[1] ) ) return;
 	
-	
 	if(data_add) {
 		len[1]++;
 		memset(data_add,0,len[1]);
@@ -849,10 +850,16 @@
 	}
 	memset(s,0,len[0]);
 	heap->count += len[0];
+	if( heap->count == heap->size){
+		heap->wr = 0;
+		return;
+	}
 	if(rollback){
-		heap->wr-=len[0];
-		heap->wr-=len[1];
-		if(heap->wr < 0)heap->wr += heap->size;
+		size_t rb = len[0] + len[1];
+		if( rb > heap->wr){
+			heap->wr += heap->size; 
+		}
+		heap->wr -= rb;
 	}
 }
 

--
Gitblit v1.9.1