Skip to content

UB in blob packing (misaligned u32 writes) + signed shift UB in bitset ops #49

@Pesky01

Description

@Pesky01

Integrating kb_text_shape.h via Zig. 2 UB issues crash immediately under Zig's debug UB traps.

Environment: Windows 11 x86_64, Zig 0.15.2 (mingw), kb_text_shape.h v2.07

Called kbts_ShapePushFontFromMemory(ctx, bytes, len, 0) with Geist or GeistMono variable fonts. No shaping required - crashes during font init.


Issue 1: Misaligned u32 store in kbts_PlaceBlob

kbts_PlaceBlob stamps font tables back-to-back into OutData (char*), then casts to kbts_u32* for baked matrices. If total table bytes % 4 != 0, the cast is misaligned.

thread 240276 panic: store of misaligned address 0x1a005cb5676 for type 'unsigned int', which requires 4 byte alignment
C:\Users\pesky\source\repos\cl\third_party\kb\kb_text_shape.h:28215:0: 0x7ff6e0c0b01a in kbts_PlaceBlob (kb.lib)
              LookupSubtableIndexOffsets[RunningLookupIndex] = (kbts_u32)RunningSubtableIndex;

C:\Users\pesky\source\repos\cl\third_party\kb\kb_text_shape.h:25856:0: 0x7ff6e0c00c69 in kbts_ShapePushFontFromMemory (kb.lib)
        Error = kbts_PlaceBlob(Result, &State, Scratch, Output);

C:\Users\pesky\source\repos\cl\src\draw\text_shape.zig:92:53: 0x7ff6e0bab9d6 in pushFont (chess_lab_zcu.obj)
    const font = kbts.c.kbts_ShapePushFontFromMemory(ctx, @ptrCast(@constCast(bytes.ptr)), @intCast(bytes.len), 0);

Issue 2: Signed shift UB in bitset ops

Multiple sites do:

Matrix[...] |= 1 << MatrixIndex.BitIndex;

When BitIndex == 31, shifting signed 1 into the sign bit is UB.

This pattern appears in other places as well, where they might be an issue?

thread 240672 panic: left shift of 1 by 31 places cannot be represented in type 'int'
C:\Users\pesky\source\repos\cl\third_party\kb\kb_text_shape.h:27581:0: 0x7ff606fd0b99 in kbts__MarkMatrixCoverage (kb.lib)
            Matrix[MatrixIndex.WordIndex] |= 1 << MatrixIndex.BitIndex;

C:\Users\pesky\source\repos\cl\third_party\kb\kb_text_shape.h:28494:0: 0x7ff606fb0302 in kbts_PlaceBlob (kb.lib)
                      kbts__MarkMatrixCoverage(...);

C:\Users\pesky\source\repos\cl\third_party\kb\kb_text_shape.h:25856:0: 0x7ff606fa0ca9 in kbts_ShapePushFontFromMemory (kb.lib)
        Error = kbts_PlaceBlob(Result, &State, Scratch, Output);

C:\Users\pesky\source\repos\cl\src\draw\text_shape.zig:92:53: 0x7ff606f4b9d6 in pushFont (chess_lab_zcu.obj)
    const font = kbts.c.kbts_ShapePushFontFromMemory(ctx, @ptrCast(@constCast(bytes.ptr)), @intCast(bytes.len), 0);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions