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("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);
            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);
    
    bool TzifLocalOffsetSeconds(i64 unix_seconds, i32 *out_offset_seconds, Allocator *alloc) {
        Buf data = BufInit(alloc);
        if (FileReadAndClose("/etc/localtime", &data) < 0) {
            BufDeinit(&data);
            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);
            N = 10000
        };
        Buf  src = BufInit(alloc_base);
        bool ok  = BufResize(&src, N);
        for (i32 i = 0; i < N; ++i) {
        File r  = FileOpen(&path, "rb");
        ok      = ok && FileIsOpen(&r);
        Buf dst = BufInit(alloc_base);
        i64 got = FileRead(&r, &dst);
        FileClose(&r);
        FileClose(&seed);
    
        Buf  in = BufInit(alloc_base);
        bool ok = BufResize(&in, 5);
        MemCopy(BufData(&in), "12345", 5);
    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
        DefaultAllocator alloc  = DefaultAllocatorInit();
        Str              output = StrInit(&alloc);
        Buf              b      = BufInit(&alloc);
    
        BufPushBytes(&b, (const u8 *)"hello", 5);
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    bool test_buf_literal_magic_roundtrip(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    bool test_buf_literal_brace_escape(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    bool test_buf_literal_trailing_byte(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
        WriteFmt("m16: u8 width-1 round-trip\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
        WriteFmt("m16: u32 width-4 round-trip\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
        WriteFmt("m16: u64 width-8 round-trip\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
        WriteFmt("m16: i64 width-8 round-trip\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
        WriteFmt("m16: non-raw spec {2} must abort\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
    
        // Lay down 2 bytes so the mutant's (wrongly accepted) width-2 read has
    static bool test_m30_patch_exact_fit_succeeds(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    static bool test_m30_patch_empty_at_end_succeeds(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    static bool test_m30_patch_overlong_fails(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    static bool test_m31_raw_u8(void) {
        DefaultAllocator alloc    = DefaultAllocatorInit();
        Buf              b        = BufInit(&alloc);
        bool             ok       = BufAppendFmt(&b, "{<1r}", (u8)0x7E);
        const u8         expect[] = {0x7E};
    static bool test_m31_raw_i64(void) {
        DefaultAllocator alloc    = DefaultAllocatorInit();
        Buf              b        = BufInit(&alloc);
        bool             ok       = BufAppendFmt(&b, "{<8r}", (i64)0x0102030405060708ll);
        const u8         expect[] = {0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01};
    static bool test_m31_raw_f64(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        union {
            f64 f;
    static bool test_m32_raw_width1_byte_writes(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = BufWriteFmt(&b, "{<1r}", (u8)0xAB);
        ok                     = ok && (BufLength(&b) == 1) && (BufData(&b)[0] == 0xAB);
    static bool test_m32_deadend_nonraw_legal_width(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteFmt(&b, "{4}", (u32)0x11223344); // real: LOG_FATAL before return
        BufDeinit(&b);
    static bool test_m33_buf_append_fmt_emits_bytes(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
    
        bool r = BufAppendFmt(&b, "{>4r}", (u32)0xAABBCCDD);
    static bool test_m33_write_r16_explicit_big(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
    
        bool      ok = BufWriteFmt(&b, "{>2r}", (u16)0x1234);
    static bool test_m33_write_r16_native(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
    
        bool      ok = BufWriteFmt(&b, "{^2r}", (u16)0x1234);
    static bool test_m33_write_r32_native(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
    
        bool      ok = BufWriteFmt(&b, "{^4r}", (u32)0x11223344);
    static bool test_m33_write_r64_native(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
    
        bool      ok = BufWriteFmt(&b, "{^8r}", (u64)0x0102030405060708ULL);
    bool test_if_909_buf_read_u64_dispatch(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    static bool test_m8_caret_native_endian_raw_roundtrip(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
        WriteFmt("bufread too-few-args must abort\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        BufWriteFmt(&b, "{<1r}{<1r}", (u8)0x11, (u8)0x22); // 2 bytes available
        BufIter it = BufIterFromBuf(&b);
    static bool test_buf_write_multi(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = BufWriteFmt(&b, "{<2r}{>4r}", (u16)0x1234, (u32)0xAABBCCDD);
        ok                     = ok && (BufLength(&b) == 6);
    static bool test_m21_read_r32_big_endian_exact(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    static bool test_m21_read_r32_little_endian_exact(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    static bool test_m21_read_r32_little_endian_reversed(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    static bool test_m21_read_r32_native_resolves_roundtrip(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    static bool test_m22_little_endian_read_roundtrip_u64(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    static bool test_m22_little_endian_read_distinct_bytes(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    static bool test_m22_native_endian_read_roundtrip_u64(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = true;
    static bool test_m33_read_r8_value(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
    
        bool ok = BufAppendFmt(&b, "{<1r}", (u8)0xAB);
    static bool test_buf_read_u16(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        bool             ok    = BufWriteFmt(&b, "{<2r}", (u16)0xBEEF);
        BufIter          it    = BufIterFromBuf(&b);
    static bool test_buf_read_f64(void) {
        DefaultAllocator alloc = DefaultAllocatorInit();
        Buf              b     = BufInit(&alloc);
        union {
            f64 f;
        // Use the L-form so the taken Buf is the allocator's responsibility:
        // a copy buffer through `base`, handed to PdbOpenFromMemory.
        Buf in = BufInit(base);
        if (!BufReserve(&in, sizeof(sblob))) {
            DebugAllocatorDeinit(&dbg);
    static bool test_v1_resolve(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        Buf              b = BufInit(&a);
        build_v1(&b);
        i32  off;
    static bool test_v2_resolve(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        Buf              b = BufInit(&a);
        build_v2(&b);
        i32  off;
    static bool test_no_transitions_uses_std_fallback(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        Buf              b = BufInit(&a);
        BufAppendFmt(&b, "TZif{>1r}", (u8)0);
        put_reserved15(&b);
    static bool test_v1_no_std_type_fails(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        Buf              b = BufInit(&a);
        BufAppendFmt(&b, "TZif{>1r}", (u8)0);
        put_reserved15(&b);
    static bool test_v2_no_std_type_fails(void) {
        DefaultAllocator a = DefaultAllocatorInit();
        Buf              b = BufInit(&a);
        BufAppendFmt(&b, "TZif{>1r}", (u8)'2');
        put_reserved15(&b);
Last updated on