#80·microui

mu_Command invokes undefined behavior.

Author: teleprint-meCreated Oct 13, 2025Updated Oct 13, 2025

According to the C specification, a union must have objects of the same size.

6.5.16.1 Simple assignment

Semantics

  1. If the value being stored in an object is read from another object that overlaps in any way the storage of the first object, then the overlap shall be exact and the two objects shall have qualified or unqualified versions of a compatible type; otherwise, the behavior is undefined.

This means that invoking mu_Command in any other call to reference another structure misaligns that object and invokes undefined behavior.

If you enable ASAN or Valgrind, it will display unaligned pointers because the structures are required to be the same size. Each structure has a different set of fields which aligns each object differently as they each have a different size.

Here's a sample of the output I am able to reproduce upon invocation of the demo program.

bash
microui.c:430:18: runtime error: member access within misaligned address 0x7ac86f1bd974 for type 'union mu_Command', which requires 8 byte alignment
0x7ac86f1bd974: note: pointer points here
  32 32 32 ff 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
              ^ 
microui.c:431:18: runtime error: member access within misaligned address 0x7ac86f1bd974 for type 'union mu_Command', which requires 8 byte alignment
0x7ac86f1bd974: note: pointer points here
  32 32 32 ff 03 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00

The union expects an 8-byte alignment, but receives an integer which is 4-bytes. In other cases, such as char (which is 1-byte), you can see the same issue arise.

bash
microui.c:515:16: runtime error: member access within misaligned address 0x7ac86f1bda2c for type 'union mu_Command', which requires 8 byte alignment
0x7ac86f1bda2c: note: pointer points here
  00 00 00 00 05 00 00 00  20 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00

This undefined behavior can introduce subtle bugs which can led to larger issues if applied to a larger or more specific project.

A potential fix to this is to use void* to perform polymorphic methods and invocations which can be cast to the appropriate type on an as needed basis. A simple fix for this is to use a factory to produce the objects on demand based on the objects type id.

There are several other subtle bugs I noticed such as shadowed variables which are low vectors of potential exploitation within an application.

I appreciate the minimalism here and find the code to be absolutely ergonomic, pragmatic, and well thought out. I'm very impressed by the overall design. I just felt it was worth noting and reporting this issue because it's a fairly significant issue to overlook or even ignore.

PR #76 and ISS #19 both address this issue and propose potential solutions which can mitigate it, but do not fully resolve the issue.