git » libjio » commit ac6eb0e

Limit transaction's size.

author Alberto Bertogli
2004-11-28 15:51:12 UTC
committer Alberto Bertogli
2007-07-15 13:47:21 UTC
parent a62d15e8b2cf458118dfaac628be9e7f2720a64e

Limit transaction's size.

Check transaction size, and don't let it grow beyond SSIZE_MAX, since
otherwise jtrans_commit() return value could overflow and return a failure
when there is none.

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

common.h +2 -0
libjio.h +1 -0
trans.c +7 -2

diff --git a/common.h b/common.h
index 76a3e3a..b5861f6 100644
--- a/common.h
+++ b/common.h
@@ -26,6 +26,8 @@
 #define F_TLOCKW	(_F_TLOCK | _F_WRITE)
 #define F_UNLOCK	(_F_ULOCK)
 
+#define MAX_TSIZE	(SSIZE_MAX)
+
 
 off_t plockf(int fd, int cmd, off_t offset, off_t len);
 ssize_t spread(int fd, void *buf, size_t count, off_t offset);
diff --git a/libjio.h b/libjio.h
index 32519d5..0ff1ba6 100644
--- a/libjio.h
+++ b/libjio.h
@@ -63,6 +63,7 @@ struct jtrans {
 	int id;			/* transaction id */
 	uint32_t flags;		/* transaction flags */
 	unsigned int numops;	/* quantity of operations in the list */
+	ssize_t len;		/* transaction's length */
 	pthread_mutex_t lock;	/* used to modify the operation list */
 	struct joper *op;	/* list of operations */
 };
diff --git a/trans.c b/trans.c
index 688594d..0132f19 100644
--- a/trans.c
+++ b/trans.c
@@ -146,6 +146,11 @@ int jtrans_add(struct jtrans *ts, const void *buf, size_t count, off_t offset)
 		return 0;
 	}
 
+	if (ts->len + count > MAX_TSIZE) {
+		pthread_mutex_unlock(&(ts->lock));
+		return 0;
+	}
+
 	/* find the last operation in the transaction and create a new one at
 	 * the end */
 	if (ts->op == NULL) {
@@ -181,6 +186,8 @@ int jtrans_add(struct jtrans *ts, const void *buf, size_t count, off_t offset)
 		return 0;
 	}
 
+	ts->numops++;
+	ts->len += count;
 	pthread_mutex_unlock(&(ts->lock));
 
 	/* we copy the buffer because then the caller can reuse it */
@@ -192,8 +199,6 @@ int jtrans_add(struct jtrans *ts, const void *buf, size_t count, off_t offset)
 	jop->pdata = NULL;
 	jop->locked = 0;
 
-	ts->numops++;
-
 	return 1;
 }