summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDwayne C. Litzenberger <dlitz@dlitz.net>2009-10-10 21:28:04 -0400
committerDwayne C. Litzenberger <dlitz@dlitz.net>2009-10-12 14:53:59 -0400
commitacf6183a53e2ce8849bc27afd51991babe69338d (patch)
tree1ddad71d697a28269a144fce54836ddddbd5d8ba
parentafbaf76c8c38147a827935293fb9b93a77095cbd (diff)
downloadpycrypto-acf6183a53e2ce8849bc27afd51991babe69338d.tar.gz
Counter: Add some assert() statements
-rw-r--r--src/_counter.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/_counter.c b/src/_counter.c
index 9e14da4..5e0e928 100644
--- a/src/_counter.c
+++ b/src/_counter.c
@@ -97,6 +97,12 @@ CounterObject_init(PCT_CounterObject *self, PyObject *args, PyObject *kwargs)
}
self->p = self->val + PyString_GET_SIZE(prefix);
+ /* Sanity-check pointers */
+ assert(self->val <= self->p);
+ assert(self->p + self->nbytes <= self->val + self->buf_size);
+ assert(self->val + PyString_GET_SIZE(self->prefix) == self->p);
+ assert(PyString_GET_SIZE(self->prefix) + self->nbytes + PyString_GET_SIZE(self->suffix) == self->buf_size);
+
/* Copy the prefix, suffix, and initial value into the buffer. */
memcpy(self->val, PyString_AS_STRING(prefix), PyString_GET_SIZE(prefix));
memcpy(self->p, PyString_AS_STRING(initval), self->nbytes);
@@ -164,6 +170,10 @@ _CounterObject_next_value(PCT_CounterObject *self, int little_endian)
increment = 1;
}
for (i = 0; i < self->nbytes; i++, p += increment) {
+ /* Sanity check pointer */
+ assert(self->p <= p);
+ assert(p < self->p + self->nbytes);
+
/* ch = ord(p) */
Py_CLEAR(ch); /* delete old ch */
ch = PyInt_FromLong((long) *p);
@@ -217,6 +227,10 @@ CounterLEObject_increment(PCT_CounterObject *self)
carry = 1;
p = self->p;
for (i = 0; i < self->nbytes; i++, p++) {
+ /* Sanity check pointer */
+ assert(self->p <= p);
+ assert(p < self->p + self->nbytes);
+
tmp = *p + carry;
carry = tmp >> 8; /* This will only ever be 0 or 1 */
*p = tmp & 0xff;
@@ -235,6 +249,10 @@ CounterBEObject_increment(PCT_CounterObject *self)
carry = 1;
p = self->p + self->nbytes-1;
for (i = 0; i < self->nbytes; i++, p--) {
+ /* Sanity check pointer */
+ assert(self->p <= p);
+ assert(p < self->p + self->nbytes);
+
tmp = *p + carry;
carry = tmp >> 8; /* This will only ever be 0 or 1 */
*p = tmp & 0xff;