Question

I've been writing a "Byte Buffer" utility module - just a set of functions for personal use in low level development.

Unfortunately, my ByteBuffer_Append(...) function doesn't work properly when it null terminates the character at the end, and/or adds extra room for the null termination character. One result, when this is attempted, is when I call printf() on the buffer's data (a cast to (char*) is performed): I'll get only a section of the string, as the first null termination character within the buffer will be found.

So, what I'm looking for is a means to incorporate some kind of null terminating functionality within the function, but I'm kind of drawing a blank in terms of what would be a good way of going about this, and could use a point in the right direction.

Here's the code, if that helps:

void ByteBuffer_Append( ByteBuffer_t* destBuffer, uInt8* source, uInt32 sourceLength )
{
    if ( !destBuffer )
    {
        puts( "[ByteBuffer_Append]: param 'destBuffer' received is NULL, bailing out...\n" );

        return;
    }

    if ( !source )
    {
        puts( "[ByteBuffer_Append]: param 'source' received is NULL, bailing out...\n" );

        return;
    }

    size_t byteLength = sizeof( uInt8 ) * sourceLength;

    // check to see if we need to reallocate the buffer

    if ( destBuffer->capacity < byteLength || destBuffer->length >= sourceLength )
    {
        destBuffer->capacity += byteLength;

        uInt8* newBuf = ( uInt8* ) realloc( destBuffer->data, destBuffer->capacity );

        if ( !newBuf )
        {
            Mem_BadAlloc( "ByteBuffer_Append - realloc" );
        }

        destBuffer->data = newBuf;
    }

    uInt32 end = destBuffer->length + sourceLength;

    // use a separate pointer for the source data as
    // we copy it into the destination buffer

    uInt8* pSource = source;

    for ( uInt32 iBuffer = destBuffer->length; iBuffer < end; ++iBuffer )
    {
        destBuffer->data[ iBuffer ] = *pSource;

        ++pSource;
    }

    // the commented code below
    // is where the null termination
    // was happening

    destBuffer->length += sourceLength; // + 1;

    //destBuffer->data[ destBuffer->length - 1 ] = '\0';
}

Many thanks to anyone providing input on this.

Was it helpful?

Solution

Looks like your issue is caused by memory corruption.

You have to fix the following three problems:

1 check if allocated space is enough

if ( destBuffer->capacity < byteLength || destBuffer->length >= sourceLength )

does not properly check if buffer reallocation is needed, replace with

if ( destBuffer->capacity <= destBuffer->length+byteLength )

2 allocating enough space

destBuffer->capacity += byteLength;

is better to become

destBuffer->capacity = destBuffer->length + byteLength + 1;

3 properly null terminating

destBuffer->data[ destBuffer->length - 1 ] = '\0';

should become

destBuffer->data[ destBuffer->length ] = '\0';

OTHER TIPS

In C/C++, a list of chars terminating by a '\0' is a string. There are a set of string functions, such as strcpy(), strcmp(), they take char * as parameter, and when they find a '\0', they the string end there. In your case, printf("%s", buf) treats buf as a string, so when it find a '\0', it stops print.

If you are doing a buffer, that means any data include '\0' is normal data in the buffer. So you should avoid to use string functions. To print a buffer, you need to implement your own function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top