#!/usr/bin/env python3 """Guard arena_map_is_used's C89 locals and original evaluation order.""" import re import sys from pathlib import Path source = Path(sys.argv[1]).read_text() match = re.search(r"\narena_map_is_used\(block \*p\)\s*\{(.*?)\n\}", source, re.S) assert match, "arena_map_is_used definition not found" body = re.sub(r"/\*.*?\*/", "", match.group(1), flags=re.S) declarations = """ arena_map_bot_t *n = arena_map_get(p, 0); int i3; int32_t hi; int32_t lo; int32_t tail; """ assert body.split()[:len(declarations.split())] == declarations.split(), ( "arena_map_is_used must declare i3, hi, lo, tail before the NULL guard" ) # Failed lookup must return before index evaluation or node dereferences. # Preserve lookup count, assignment order, and the short-circuit predicate. expected = declarations + """ if (n == NULL) { return 0; } i3 = MAP_BOT_INDEX(p); hi = n->arenas[i3].tail_hi; lo = n->arenas[i3].tail_lo; tail = (int32_t)(AS_UINT(p) & ARENA_SIZE_MASK); return (tail < lo) || (tail >= hi && hi != 0); """ assert body.split() == expected.split(), ( "preserve arena_map_is_used lookup, NULL return, evaluation order and predicate" ) print("obmalloc arena_map_is_used C89 declarations and original evaluation order: PASS")