Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions include/xmlsec/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

/******************************************************************************
*
Expand All @@ -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. */
};

Expand Down
42 changes: 19 additions & 23 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
Loading