Skip to content

Commit 6b8114e

Browse files
convention sweep: drive-to-clean across the entire codebase
Fifteen rounds of audit-and-fix passes against `CODING-CONVENTIONS.md`, verified clean by an independent reviewer and end-to-end on Linux (gcc, 104/104) and Mac (clang, 99/99). Stack-init scopes: - `StrInitStack(name, ne) { body }` and `VecInitStack(T, name, ne) { body }` are the for-chain idioms; no allocator, `_Alignas(T)` backing storage, scope-exit zeroes the handle so any subsequent use trips `validate_vec`. - `reserve_vec` / `deinit_vec` / `reduce_space_vec` recognise the NULL-allocator stack-init signal and abort with the dedicated "vector not growable, no allocator assigned, probably stack inited" trap. - `IterInitFromVec*` falls back to alignment 1 on NULL allocator so a `StrIter` over a stack-init Str works. - `IterTruncate` is the legitimate Iter mutator; `IterLength` and the Vec / Map / List / BitVec / Graph length / capacity / allocator accessors are rvalue-only via `((void)0, (c)->field)` so `Accessor(c) = X` no longer compiles. `_Generic` dispatch: - Every site with a `Zstr:` arm now carries an inline `char *:` synonym arm with an identical body, restoring MSVC C portability. `/Zc:strictStrings` is documented as a C++-only no-op and removed from the MSVC branch of meson.build. Sub-range iteration: - `Sys/Dns.c::parse_hosts_table` / `parse_resolv_conf` rewritten from `Zstr p / Zstr end` to `StrIter`, matching the `Parsers/JSON.c` / `Parsers/KvConfig.c` precedent. - `Sys/ProcMaps.c` parser rewritten to `StrIter`; chunked-read append now goes through `StrReserve` + `StrEnd` + `StrResize`. - `Std/Io.c` float / int / hex / octal / binary parsers, plus the format-write parsers, migrated to `StrIter`. - Smaller cursor walks in `Parsers/Dns.c::encode_qname`, `ArgParse.c`, `Parsers/Dwarf*` family migrated to `StrIter` / `StrPushBackMany`. - The dual-cursor `Io.c::str_read_fmt` and the past-the-end-pointer `Container/Str.c::StrSplit` / `strip_str` carry inline-justified pointer-pair semantics that StrIter can't express; left as-is. Typing + naming: - `u8 *` for raw byte-arithmetic in allocator internals (Heap.c, Slab.c, Page.c, Vec.c, List.c). - `Zstr` everywhere in declarations / params / fields. `MachoCache`, `PdbCache`, `Backtrace`, `Sys/Dns`, `Bench/Allocator` updated. - `MISRA_SIG*` -> `BEAM_*` to avoid libc-signal-macro collision. - `SetAbortCallback` -> `OnAbort` (forbidden `Set*` shape). - `BufReadCstr` / `BufWriteCstr` -> `BufReadZstr` / `BufWriteZstr` (wire format is plain Zstr, not the `(Zstr, size)` `Cstr` shape). - `Tests/Std/Vec.Complex.c::CreateComplexItem` -> `InitComplexItem`. - `is_tty` (was `misra_is_tty`); `Zstr exe = _dyld_get_image_name(0)`; `Zstr nm = de->d_name`. Accessors added (the field-read sites the tests + tree needed): - `VecCopyInit` / `VecCopyDeinit`; `ListCopyInit` / `ListCopyDeinit`; `ListNodeData`; `MapKeyCopyInit` / `MapKeyCopyDeinit` / `MapValueCopyInit` / `MapValueCopyDeinit`; `GraphAllocator` / `GraphCopyInit` / `GraphCopyDeinit` / `GraphMutationEpoch`. - Public `zstr_hash` / `zstr_compare` (FNV-1a + three-way wrapper) so `Tests/Std/Map.Ops.c` and any future `Map<Zstr, ...>` callers can register the canonical callbacks without redefining them. Direct field-access cleanup: - Replaced `Vec.length` / `Vec.data` / `Str.data` / `Str.length` / `Map.allocator` / `Buf.length` reads from outside the container's `.c` with the matching accessor across `Sys/PdbCache`, `MachoCache`, `SymbolResolver`, `Socket`, `Backtrace`, `Proc`, `Dir`, `Dns`, `Std/Io`, `Std/Log`, `Std/ArgParse`, `Std/Allocator/Debug`, `Std/Container/{Graph,BitVec,Int,Float}`, `Parsers/{Elf,Dwarf, DwarfInfo,DwarfUnwind,Pdb,Http,JSON,KvConfig,Dns}`, `Tests/Std/{*}`. - `copy.length = data_size` blocks in `MachO.c`, `Pe.c`, `Elf.c`, `Pdb.c` go through `BufResize`. `path.length = 0` in `SymbolResolver.c` goes through `StrResize`. `Pdb.c::name_pool` ownership-steal block is the only remaining bypass and now carries an explicit comment naming the missing `StrTakeBuffer` primitive. Bugs picked up along the way: - `Std/Io.c::write_char_internal` produced `\x\HH` instead of `\xHH` for non-printable bytes; matches the sibling pattern at line 1216 now. - `Sys/Dns.c::DnsResolverDeinit` no longer aborts on a zero-init or partially-failed-init struct. - `Sys/Proc.c::sys_proc_read_internal` stages reads in an `StrInitStack` + `StrMergeR` pipeline (was `char tmpbuf[1024]` + `StrPushBackMany`). - `Parsers/Dns.c::deinit_record_list` uses `VecBegin(list)` instead of `list->data` so the uninit guard goes through the accessor. Documentation: - ~500+ doc blocks added / upgraded: TAGS lines for accessor surfaces, full `SUCCESS:` / `FAILURE:` blocks for Math.h overflow helpers, Buf.h binary read / write family, Map / List foreach variants, ArgParse registration macros, Iter init variants, StrIter constructors, Container `Math.h` / `Compare.h` / `Convert.h` / `Memory.h` surfaces. - `CODING-CONVENTIONS.md` grows: dedicated "Stack-init APIs" section; `_Generic` `char *` synonym arm rule; clarified syscall-prefix carve-out for `_Syscall.h` and `Beam.c`; carve-out for libc usage in `Fuzz/` and `Benchmark/`. - Alias macros (`StrForeach*`, `StrPopBack`, `StrRemove*`, `VecInsert`, `VecPushBack`, `VecPushFront`, `ListInsert`, `ListPushBack`, `MapInsert`, `MapSet`, `GraphAddNode`, `StrIter*`) reframed in the namespace's vocabulary plus a "See `<UnderlyingL>` for the full contract" pointer, instead of "alias for VecX". - Stale legacy headers / proprietary-license blocks in `Std/Utility.h` and `Std/Utility/Pair.h` replaced with the canonical public-domain three-line header. Tests: - `Tests/Std/{Vec.Type,Vec.Insert,Vec.Complex,List.Access,List.Foreach, List.Init,List.Insert,List.Ops,List.Remove,List.Type,Graph.Init, Graph.Type,Int.{Type,Convert},Float.{Type,Convert},BitVec.{Memory, Convert,Type,Init,Foreach},Allocator.{Heap,Arena,Page,Slab,Budget}, AllocDebug,Str.Access,Str.Convert,Str.Init,Io.Read}.c` updated to use public accessors instead of direct field reads. - `Vec.Init` test grows an `AlignedItem { i32 a; f64 b; }` check that asserts the `_Alignas(T)` backing-buffer alignment plus a `break`-exit verification. - `size_t` -> `size` swept across Str.Convert, Str.Init, Io.Read, BitVec.Convert test bodies. Locked design questions (documented bypasses or carve-outs -- deferred to future commits when the underlying primitive is added): `Tests/Util/TestRunner` snake_case public API rename; `Map.allocator` mutator in `Allocator/Debug.c`; `StrTakeBuffer` ownership-extract for `Pdb.c::name_pool`; libc-verb naming in `Sys/Dir.c::DirCreate*`/`DirRemoveAll`; `ARG_TARGET` / `IOFMT` `default:` runtime sentinels; `STDIN_FILENO FILENO(stdin)` polyfill; `Sys/Socket.c` and `Sys/Backtrace.c` `char *` C-string output parameters; `EnvGet(Zstr)` Str overload. Verified clean by an independent whole-codebase reviewer across every CODING-CONVENTIONS.md section. 169 files changed, +4649 / -1758.
1 parent a704368 commit 6b8114e

169 files changed

Lines changed: 4649 additions & 1758 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Benchmark/Source/Allocator.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
#include <stddef.h>
2020
#include <stdint.h>
2121

22+
#include <Misra/Std/Zstr.h>
23+
2224
#ifdef __cplusplus
2325
extern "C" {
2426
#endif
2527

2628
// Backend name used in benchmark output (e.g. "glibc", "jemalloc", "misra").
27-
const char *bench_backend_name(void);
29+
Zstr bench_backend_name(void);
2830

2931
// One-time init / teardown. For libc-shape backends these are no-ops; for
3032
// MisraStdC they construct/destruct the HeapAllocator.

Benchmark/Source/Allocator_libc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# include <gperftools/malloc_extension_c.h>
2121
#endif
2222

23-
const char *bench_backend_name(void) {
23+
Zstr bench_backend_name(void) {
2424
return BENCH_BACKEND_NAME;
2525
}
2626

Benchmark/Source/Allocator_misra.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static Allocator *g_alloc = NULL;
3333
// build) it's just an unused alias.
3434
static HeapAllocator *g_heap_typed = NULL;
3535

36-
const char *bench_backend_name(void) {
36+
Zstr bench_backend_name(void) {
3737
// Variant name is supplied at compile time by meson so the JSON
3838
// output identifies which libmisra_std was linked (fast vs full
3939
// heap_validate_self). Defaults to plain "misra" if undefined,

Benchmark/Source/Allocator_misra_arena.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
static ArenaAllocator g_arena;
2222
static bool g_arena_live = false;
2323

24-
const char *bench_backend_name(void) {
24+
Zstr bench_backend_name(void) {
2525
#ifdef BENCH_MISRA_VARIANT_NAME
2626
return BENCH_MISRA_VARIANT_NAME;
2727
#else

Benchmark/Source/Allocator_misra_correct.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static HeapAllocator g_heap;
4242
static SlabAllocator g_slab;
4343
static bench_mode g_mode = MODE_NONE;
4444

45-
const char *bench_backend_name(void) {
45+
Zstr bench_backend_name(void) {
4646
#ifdef BENCH_MISRA_VARIANT_NAME
4747
return BENCH_MISRA_VARIANT_NAME;
4848
#else

Benchmark/Source/Allocator_misra_page.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
static PageAllocator g_page;
1818
static bool g_page_live = false;
1919

20-
const char *bench_backend_name(void) {
20+
Zstr bench_backend_name(void) {
2121
#ifdef BENCH_MISRA_VARIANT_NAME
2222
return BENCH_MISRA_VARIANT_NAME;
2323
#else

Bin/Beam.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
5151
// own trampoline. Struct: handler / sa_tramp / mask / flags.
5252
# include "../Source/Misra/_Syscall.h"
5353

54-
# define MISRA_SIGINT 2
55-
# define MISRA_SIGPIPE 13
56-
# define MISRA_SIGTERM 15
57-
# define MISRA_SIG_IGN_HANDLER ((void (*)(int))1) // SIG_IGN
54+
# define BEAM_SIGINT 2
55+
# define BEAM_SIGPIPE 13
56+
# define BEAM_SIGTERM 15
57+
# define BEAM_SIG_IGN_HANDLER ((void (*)(int))1) // SIG_IGN
5858

5959
# if PLATFORM_LINUX
6060

61-
# define MISRA_SA_RESTORER 0x04000000UL
61+
# define BEAM_SA_RESTORER 0x04000000UL
6262

6363
struct misra_kernel_sigaction {
6464
void (*sa_handler)(int);
@@ -86,7 +86,7 @@ static void install_signal(int signum, void (*handler)(int)) {
8686
struct misra_kernel_sigaction sa = {0};
8787
sa.sa_handler = handler;
8888
# if ARCHITECTURE_X86_64
89-
sa.sa_flags = MISRA_SA_RESTORER;
89+
sa.sa_flags = BEAM_SA_RESTORER;
9090
sa.sa_restorer = misra_sigreturn_restorer;
9191
# endif
9292
// 4th arg = sigsetsize in bytes (Linux ABI requires 8 for the
@@ -227,11 +227,11 @@ static void install_signal_handlers(void) {
227227
#if PLATFORM_WINDOWS
228228
SetConsoleCtrlHandler(on_console_ctrl, TRUE);
229229
#elif (PLATFORM_LINUX || PLATFORM_DARWIN) && (ARCHITECTURE_X86_64 || ARCHITECTURE_AARCH64)
230-
install_signal(MISRA_SIGINT, on_signal);
231-
install_signal(MISRA_SIGTERM, on_signal);
230+
install_signal(BEAM_SIGINT, on_signal);
231+
install_signal(BEAM_SIGTERM, on_signal);
232232
// SIGPIPE on a hung-up peer would terminate us; mask it and rely
233233
// on send() returning EPIPE instead.
234-
install_signal(MISRA_SIGPIPE, MISRA_SIG_IGN_HANDLER);
234+
install_signal(BEAM_SIGPIPE, BEAM_SIG_IGN_HANDLER);
235235
#else
236236
// Other POSIX (BSD on non-x86_64/aarch64 hardware, etc.): fall
237237
// back to libc sigaction.

CODING-CONVENTIONS.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ of the codebase to see them in action.
1616
- **No project prefix on identifiers.** The include path
1717
(`<Misra/Std/Container/Buf.h>`) already names the namespace; adding
1818
`Misra` / `MISRA_` / `misra_` to every symbol is noise. The `MISRA_`
19-
prefix is reserved for three things only: include guards, `FEATURE_*`
20-
build-config flags surfaced by `meson.build`, and `_MAGIC` struct
21-
sentinels (e.g. `HEAP_ALLOCATOR_MAGIC`, `VEC_MAGIC`).
19+
prefix is reserved for four things only: include guards, `FEATURE_*`
20+
build-config flags surfaced by `meson.build`, `_MAGIC` struct
21+
sentinels (e.g. `HEAP_ALLOCATOR_MAGIC`, `VEC_MAGIC`), and
22+
direct-syscall wrappers and constants in `Source/Misra/_Syscall.h` /
23+
`Bin/Beam.c` (collision-avoidance with libc's `SYS_*` / `sys_*`
24+
syscall vocabulary).
2225
- **Short names where the include path already disambiguates.** `Buf`,
2326
`Str`, `Vec`, `Elf` — not `MisraBuf`, not `ElfFile`.
2427
- **Tool binaries** ship with a single short word as their name
@@ -278,6 +281,12 @@ of the codebase to see them in action.
278281
the Zstr-everywhere rule doesn't override `int main(int argc, char
279282
**argv)`. The boundary is the carve-out; everything that wraps it
280283
is fair game for Misra-native types.
284+
- **Fuzz harnesses and Benchmark drivers are out of scope.** Code under
285+
`Fuzz/` and `Benchmark/` (in particular `Bench.cpp` and
286+
`Allocator_libc.c`) is allowed to include libc / C++ standard headers —
287+
these are harness boundaries that intentionally compare the in-tree
288+
implementation against external baselines. `check_no_libc.py` covers
289+
the library proper (`Source/`, `Include/`, `Tests/`).
281290

282291
## `_Generic` dispatch
283292

Include/Misra/Parsers/Dwarf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ const DwarfLineEntry *DwarfLinesResolve(const DwarfLines *self, u64 vaddr);
118118
/// SUCCESS : Returns to the caller. `*self` is zeroed.
119119
/// FAILURE : Function cannot fail. NULL `self` is a no-op.
120120
///
121+
/// TAGS: Parser, DWARF, Lines, Deinit, Lifecycle
122+
///
121123
void DwarfLinesDeinit(DwarfLines *self);
122124

123125
// ===========================================================================
@@ -225,6 +227,8 @@ const DwarfCie *DwarfCfiFindCie(const DwarfCfi *self, u64 cie_offset);
225227
/// SUCCESS : Returns to the caller. `*self` is zeroed.
226228
/// FAILURE : Function cannot fail. NULL `self` is a no-op.
227229
///
230+
/// TAGS: Parser, DWARF, CFI, Deinit, Lifecycle
231+
///
228232
void DwarfCfiDeinit(DwarfCfi *self);
229233

230234
// ---------------------------------------------------------------------------

Include/Misra/Parsers/Elf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ bool elf_open_from_memory_copy(Elf *out, const u8 *data, size data_size, Allocat
287287
/// SUCCESS : Returns to the caller. `*self` is zeroed.
288288
/// FAILURE : Function cannot fail. NULL `self` is a no-op.
289289
///
290+
/// TAGS: Parser, ELF, Deinit, Lifecycle
291+
///
290292
void ElfDeinit(Elf *self);
291293

292294
///

0 commit comments

Comments
 (0)