Skip to content

BufInit

Description

Construct an empty Buf. The variadic form accepts an optional allocator pointer; with no argument, the allocator is taken from the enclosing Scope / ScopeWith block via MisraScope.

Success

Returns an initialized Buf with zero length and a lazily-allocated backing store.

Failure

Macro cannot fail; the backing allocator’s failure behaviour applies only on subsequent growth operations.

Usage example (Cross-references)

Usage examples (Cross-references)
            LOG_FATAL("MachoOpenFromMemoryCopy: NULL argument (contract violation)");
        }
        Buf copy = BufInit(alloc);
        if (!VecReserve(&copy, (u64)data_size)) {
            LOG_ERROR("MachoOpenFromMemoryCopy: allocation failed ({} bytes)", (u64)data_size);
            LOG_FATAL("MachoOpen: NULL argument (contract violation)");
        }
        Buf data = BufInit(alloc);
        if (FileReadAndClose(path, &data) < 0) {
            BufDeinit(&data);
            LOG_FATAL("ElfOpenFromMemoryCopy: NULL argument (contract violation)");
        }
        Buf copy = BufInit(alloc);
        if (!VecReserve(&copy, (u64)data_size)) {
            LOG_ERROR("ElfOpenFromMemoryCopy: allocation failed ({} bytes)", (u64)data_size);
            LOG_FATAL("ElfOpen: NULL argument (contract violation)");
        }
        Buf data = BufInit(alloc);
        if (FileReadAndClose(path, &data) < 0) {
            BufDeinit(&data);
            LOG_FATAL("PdbOpenFromMemoryCopy: NULL argument (contract violation)");
        }
        Buf copy = BufInit(alloc);
        if (!BufReserve(&copy, (u64)data_size)) {
            LOG_ERROR("PdbOpenFromMemoryCopy: allocation failed ({} bytes)", (u64)data_size);
            LOG_FATAL("PdbOpen: NULL argument (contract violation)");
        }
        Buf data = BufInit(alloc);
        if (FileReadAndClose(path, &data) < 0) {
            BufDeinit(&data);
            LOG_FATAL("PeOpenFromMemoryCopy: NULL argument (contract violation)");
        }
        Buf copy = BufInit(alloc);
        if (!BufReserve(&copy, (u64)data_size)) {
            LOG_ERROR("PeOpenFromMemoryCopy: allocation failed ({} bytes)", (u64)data_size);
            LOG_FATAL("PeOpen: NULL argument (contract violation)");
        }
        Buf data = BufInit(alloc);
        if (FileReadAndClose(path, &data) < 0) {
            BufDeinit(&data);
    bool test_buf_init_clear(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = BufLength(&b) == 0;
        BufWriteU8(&b, 0x42);
    bool test_buf_write_u_le_be(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteU16LE(&b, 0x1234);
        BufWriteU16BE(&b, 0x1234);
    bool test_buf_write_leb128(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteULeb128(&b, 0);
        BufWriteULeb128(&b, 127);
    bool test_buf_write_zstr(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteZstr(&b, "hi");
        const u8 expect[] = {'h', 'i', 0};
    bool test_buf_read_round_trip(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteU16LE(&b, 0xABCD);
        BufWriteU32BE(&b, 0x12345678);
    bool test_buf_read_leb128_round_trip(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteULeb128(&b, 624485);
        BufWriteSLeb128(&b, -123456);
    bool test_buf_read_zstr_round_trip(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteZstr(&b, "hello");
        BufWriteZstr(&b, "world");
    bool test_buf_append_fmt(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteU8(&b, 0x99); // existing byte; append must preserve
        bool     ok       = BufAppendFmt(&b, "{<2r}{>4r}", (u16)0xCAFE, (u32)0xDEADBEEF);
    bool test_buf_write_fmt_clears(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteU8(&b, 0xAA);
        BufWriteU8(&b, 0xBB);
    bool test_buf_patch_fmt(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        // Reserve a 4-byte length placeholder followed by 8 bytes of body.
        BufAppendFmt(&b, "{<4r}", (u32)0);
    
        // Out-of-range patch must fail and leave the buf unchanged.
        Buf snapshot = BufInit(&alloc);
        BufPushBytes(&snapshot, BufData(&b), BufLength(&b));
        ok = ok && !BufPatchFmt(&b, BufLength(&b), "{<2r}", (u16)0);
    bool test_buf_read_fmt(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufAppendFmt(&b, "{<2r}{>4r}{<8r}", (u16)0x1234, (u32)0xDEADBEEF, (u64)0x0102030405060708ull);
    bool test_buf_read_fmt_truncated_atomic(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufAppendFmt(&b, "{<2r}", (u16)0xABCD); // only 2 bytes; reader wants 6
Last updated on