diff --git a/include/xmlsec/buffer.h b/include/xmlsec/buffer.h index 5002a63f3..04bba9477 100644 --- a/include/xmlsec/buffer.h +++ b/include/xmlsec/buffer.h @@ -41,23 +41,6 @@ typedef enum { } xmlSecAllocMode; -/** - * @brief Use the exact memory allocation mode. - * @details The double memory allocation mode (see #xmlSecAllocModeDouble) is - * the default; the exact memory allocation mode (see #xmlSecAllocModeExact) - * is used when the #XMLSEC_BUFFER_FLAG_ALLOC_MODE_DOUBLE flag is not set. - * This flag is not actually set (its value is 0x0000), so setting it has - * no observable effect. - */ -#define XMLSEC_BUFFER_FLAG_ALLOC_MODE_EXACT 0x0000 - -/** - * @brief Use the double memory allocation mode. - * @details If this flag is set then the buffer uses the double memory - * allocation mode (see #xmlSecAllocModeDouble). - */ -#define XMLSEC_BUFFER_FLAG_ALLOC_MODE_DOUBLE 0x0001 - /** * @brief Wipe the buffer data with xmlSecMemCleanse() on release. * @details If this flag is set then the buffer data is wiped using @@ -66,7 +49,7 @@ typedef enum { * when the buffer is grown, the newly allocated area is always zeroed * with memset(0), regardless of this flag. */ -#define XMLSEC_BUFFER_FLAG_SECURE 0x1000 +#define XMLSEC_BUFFER_FLAG_SECURE 0x0001 /****************************************************************************** * @@ -81,6 +64,7 @@ struct _xmlSecBuffer { xmlSecByte* data; /**< the pointer to buffer data. */ xmlSecSize size; /**< the current data size. */ xmlSecSize maxSize; /**< the max data size (allocated buffer size). */ + xmlSecAllocMode allocMode; /**< the memory allocation mode. */ int flags; /**< the buffer behavior flags. */ }; diff --git a/src/buffer.c b/src/buffer.c index 05e847fcd..51a6daf02 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -130,20 +130,9 @@ xmlSecBufferInitialize(xmlSecBufferPtr buf, xmlSecSize size) { buf->data = NULL; buf->size = buf->maxSize = 0; + buf->allocMode = gAllocMode; buf->flags = 0; - switch(gAllocMode) { - case xmlSecAllocModeExact: - buf->flags |= XMLSEC_BUFFER_FLAG_ALLOC_MODE_EXACT; - break; - case xmlSecAllocModeDouble: - buf->flags |= XMLSEC_BUFFER_FLAG_ALLOC_MODE_DOUBLE; - break; - default: - xmlSecInvalidIntegerDataError("allocMode", (int)(gAllocMode), "xmlSecAllocModeExact or xmlSecAllocModeDouble", NULL); - return(-1); - } - return(xmlSecBufferSetMaxSize(buf, size)); } @@ -304,27 +293,32 @@ xmlSecBufferSetMaxSize(xmlSecBufferPtr buf, xmlSecSize size) { return(0); } - - if((buf->flags & XMLSEC_BUFFER_FLAG_ALLOC_MODE_DOUBLE) != 0) { - if(size > ((XMLSEC_SIZE_MAX - 32) / 2)) { - xmlSecInvalidSizeError("size", size, ((XMLSEC_SIZE_MAX - 32) / 2), NULL); - return(-1); - } - newSize = 2 * size + 32; - } else { - /* use exact mode */ + /* determine the new buffer size based on the allocation mode */ + switch(buf->allocMode) { + case xmlSecAllocModeExact: if(size > XMLSEC_SIZE_MAX - 8) { xmlSecInvalidSizeError("size", size, (XMLSEC_SIZE_MAX - 8), NULL); return(-1); } newSize = size + 8; + break; + case xmlSecAllocModeDouble: + if(size > ((XMLSEC_SIZE_MAX - 32) / 2)) { + xmlSecInvalidSizeError("size", size, ((XMLSEC_SIZE_MAX - 32) / 2), NULL); + return(-1); + } + newSize = 2 * size + 32; + break; + default: + xmlSecUnsupportedEnumValueError("alloc mode", buf->allocMode, NULL); + return(-1); } - if(newSize < gInitialSize) { newSize = gInitialSize; } + /* allocate or reallocate the buffer to the new size */ if(buf->data != NULL) { newData = (xmlSecByte*)xmlRealloc(buf->data, newSize); } else { @@ -334,15 +328,16 @@ xmlSecBufferSetMaxSize(xmlSecBufferPtr buf, xmlSecSize size) { xmlSecMallocError(newSize, NULL); return(-1); } - buf->data = newData; buf->maxSize = newSize; + /* zero out the newly allocated area */ if(buf->size < buf->maxSize) { xmlSecAssert2(buf->data != NULL, -1); memset(buf->data + buf->size, 0, buf->maxSize - buf->size); } + /* success */ return(0); } @@ -361,6 +356,7 @@ xmlSecBufferSwap(xmlSecBufferPtr buf1, xmlSecBufferPtr buf2) { SWAP(xmlSecByte*, buf1->data, buf2->data); SWAP(xmlSecSize, buf1->size, buf2->size); SWAP(xmlSecSize, buf1->maxSize, buf2->maxSize); + SWAP(xmlSecAllocMode, buf1->allocMode, buf2->allocMode); SWAP(int, buf1->flags, buf2->flags); }