git » libjio » commit a62d15e

Remove failed operations from the list.

author Alberto Bertogli
2004-11-28 15:20:16 UTC
committer Alberto Bertogli
2007-07-15 13:47:19 UTC
parent 7776533fc6c22a3f36b50d17a5ee200ebd3f1d51

Remove failed operations from the list.

If joper_add() fails when malloc()ing buffer memory, it returns 0 but never
removes the operation from the list, which could break and/or cause
corruption. The fix is just to allocate under the lock, and remove the
operation from the list if the allocation fails.

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

trans.c +9 -1

diff --git a/trans.c b/trans.c
index 7ee2978..688594d 100644
--- a/trans.c
+++ b/trans.c
@@ -167,14 +167,22 @@ int jtrans_add(struct jtrans *ts, const void *buf, size_t count, off_t offset)
 		tmpop->next->prev = tmpop;
 		jop = tmpop->next;
 	}
-	pthread_mutex_unlock(&(ts->lock));
 
 	jop->buf = malloc(count);
 	if (jop->buf == NULL) {
+		/* remove from the list and fail */
+		if (jop->prev == NULL) {
+			ts->op = NULL;
+		} else {
+			jop->prev->next = jop->next;
+		}
 		free(jop);
+		pthread_mutex_unlock(&(ts->lock));
 		return 0;
 	}
 
+	pthread_mutex_unlock(&(ts->lock));
+
 	/* we copy the buffer because then the caller can reuse it */
 	memcpy(jop->buf, buf, count);
 	jop->len = count;