#!/usr/bin/env python3 """Guard only insert_to_usedpool's entry locals and original operation order.""" import re import sys from pathlib import Path source = Path(sys.argv[1]).read_text() match = re.search( r"\ninsert_to_usedpool\(poolp pool\)\s*\{(.*?)\n\}", source, re.S, ) assert match, "insert_to_usedpool definition not found" body = re.sub(r"/\*.*?\*/", "", match.group(1), flags=re.S) declarations = """ uint size; poolp next; poolp prev; """ assert body.split()[:len(declarations.split())] == declarations.split(), ( "insert_to_usedpool must declare size, next, and prev without initializers at function entry" ) # Keep the assertion before size evaluation and both lookups, then preserve # the four pointer-link writes in their original order. expected = declarations + """ assert(pool->ref.count > 0); size = pool->szidx; next = usedpools[size + size]; prev = next->prevpool; pool->nextpool = next; pool->prevpool = prev; next->prevpool = pool; prev->nextpool = pool; """ assert body.split() == expected.split(), ( "preserve insert_to_usedpool assertion, size/lookup evaluation, and pointer-link mutation order" ) print("obmalloc insert_to_usedpool entry declarations and original operation order: PASS")