- memory allocation check in ByteBuffer_create and apply fomatter (LIB61850-517)

v1.6
Michael Zillgith 1 month ago
parent 81ba6534fd
commit 1fc842037c

@ -21,19 +21,32 @@
* See COPYING file for the complete license text. * See COPYING file for the complete license text.
*/ */
#include "libiec61850_platform_includes.h"
#include "byte_buffer.h" #include "byte_buffer.h"
#include "libiec61850_platform_includes.h"
ByteBuffer* ByteBuffer*
ByteBuffer_create(ByteBuffer* self, int maxSize) ByteBuffer_create(ByteBuffer* self, int maxSize)
{ {
if (self == NULL) { if (self == NULL)
self = (ByteBuffer*) GLOBAL_CALLOC(1, sizeof(ByteBuffer)); {
self = (ByteBuffer*)GLOBAL_CALLOC(1, sizeof(ByteBuffer));
} }
self->buffer = (uint8_t*) GLOBAL_CALLOC(maxSize, sizeof(uint8_t)); if (self)
{
self->buffer = (uint8_t*)GLOBAL_CALLOC(maxSize, sizeof(uint8_t));
if (self->buffer == NULL)
{
GLOBAL_FREEMEM(self);
self = NULL;
}
else
{
self->maxSize = maxSize; self->maxSize = maxSize;
self->size = 0; self->size = 0;
}
}
return self; return self;
} }
@ -56,12 +69,14 @@ ByteBuffer_wrap(ByteBuffer* self, uint8_t* buf, int size, int maxSize)
int int
ByteBuffer_append(ByteBuffer* self, uint8_t* data, int dataSize) ByteBuffer_append(ByteBuffer* self, uint8_t* data, int dataSize)
{ {
if (self->size + dataSize <= self->maxSize) { if (self->size + dataSize <= self->maxSize)
{
memcpy(self->buffer + self->size, data, dataSize); memcpy(self->buffer + self->size, data, dataSize);
self->size += dataSize; self->size += dataSize;
return dataSize; return dataSize;
} }
else { else
{
return -1; return -1;
} }
} }
@ -69,16 +84,16 @@ ByteBuffer_append(ByteBuffer* self, uint8_t* data, int dataSize)
int int
ByteBuffer_appendByte(ByteBuffer* self, uint8_t byte) ByteBuffer_appendByte(ByteBuffer* self, uint8_t byte)
{ {
if (self->size < self->maxSize) { if (self->size < self->maxSize)
{
self->buffer[self->size] = byte; self->buffer[self->size] = byte;
self->size ++; self->size++;
return 1; return 1;
} }
else else
return 0; return 0;
} }
uint8_t* uint8_t*
ByteBuffer_getBuffer(ByteBuffer* self) ByteBuffer_getBuffer(ByteBuffer* self)
{ {

Loading…
Cancel
Save