Skip to content

mpack crashes without a matching call to mpack_complete_* #88

Open
@cataphract

Description

@cataphract

Taking the example from the README:

#include <stdlib.h>
#include <stdio.h>
#include <mpack.h>

int main()
{
    char* data;
    size_t size;
    mpack_writer_t writer;
    mpack_writer_init_growable(&writer, &data, &size);

    // write the example on the msgpack homepage
    mpack_build_map(&writer);
    mpack_write_cstr(&writer, "compact");
    mpack_write_bool(&writer, true);
    mpack_write_cstr(&writer, "schema");
    mpack_write_uint(&writer, 0);
    //mpack_complete_map(&writer);

    // finish writing
    if (mpack_writer_destroy(&writer) != mpack_ok) {
        fprintf(stderr, "An error occurred encoding the data!\n");
        return 1;
    }

    // use the data
    printf("%.*s", (int)size, data);
    free(data);
    return 0;
}
glopes /tmp $ gcc -o t -g -I/tmp a.c mpack.c && valgrind -q ./t
==14417== Invalid free() / delete / delete[] / realloc()
==14417==    at 0x4C37DAD: realloc (vg_replace_malloc.c:1192)
==14417==    by 0x108D37: mpack_realloc (mpack.h:1842)
==14417==    by 0x10ABAB: mpack_growable_writer_teardown (mpack.c:1208)
==14417==    by 0x10B420: mpack_writer_destroy (mpack.c:1499)
==14417==    by 0x108C95: main (a.c:21)
==14417==  Address 0x52360b0 is 48 bytes inside a block of size 4,096 alloc'd
==14417==    at 0x4C32FB5: malloc (vg_replace_malloc.c:380)
==14417==    by 0x10DC1F: mpack_builder_begin (mpack.c:2470)
==14417==    by 0x10DCDF: mpack_builder_build (mpack.c:2495)
==14417==    by 0x10E1EC: mpack_build_map (mpack.c:2711)
==14417==    by 0x108C32: main (a.c:13)
An error occurred encoding the data!

This makes it impossible to use error chaining. In this case, I omitted the call to mpack_complete_map altogether, but it can also be skipped because of an earlier error. In fact, it's a problem even with:

mpack_build_map(&writer);
mpack_write_cstr(&writer, "foo");
mpack_complete_map(&writer);
mpack_writer_destroy(&writer);

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions