Files
TransPyC/includes/viperio.vp
T
TermiNexus d2d319eb21 fix(includes): ASan 集成支持 + 越界写封堵 + TypePrint 类型名截断 40→255
1. Phase2Translator 增加 TPC_ASAN 支持(opt asan 插桩+链接 -fsanitize=address),配合 clang64 定位 0xbe 崩溃。2. llvmlite/memhub/viperio/viperlib 多处理无界 strcpy/strcat/下溢/递归守卫/栈数组钳制。3. memhub alloc 块尾 canary(MEMBUDDY_CANARY)+check_alive_blocks 校验+chk_stack realloc 扩容。4. TypePrint Ptr(Struct) 类型名截断上限 40→255:含 sha1 前缀类型名(如 bbdf3bbd4c3bc28c.CONSOLE_SCREEN_BUFFER_INFO=42)被截断产生未定义类型引用(llc undefined type)。
2026-08-10 18:09:13 +08:00

60 lines
1.6 KiB
Plaintext

import t, c
from stdint import *
class Buf:
data: t.CChar | t.CPtr
length: t.CSizeT
capacity: t.CSizeT
owned: bool
def __init__(self, data: t.CChar | t.CPtr, capacity: t.CSizeT, length: t.CSizeT = 0, owned: bool = False):
self.data = data
self.capacity = capacity
self.length = length
self.owned = owned
def clear(self):
self.length = 0
if self.data != None and self.capacity > 0:
self.data[0] = 0
def write(self, src: t.CChar | t.CPtr, count: t.CSizeT) -> t.CSizeT:
if self.data == None: return 0
if src == None: return 0
if count == 0: return 0
avail: t.CSizeT = self.capacity - self.length
if avail <= 0: return 0
n: t.CSizeT = count if count < avail else avail
i: t.CSizeT = 0
while i < n:
self.data[self.length + i] = src[i]
i += 1
self.length += n
# 恰好写满(n==avail)时 data[capacity] 越界 1 字节,加保护
if self.length < self.capacity:
self.data[self.length] = 0
return n
def cstr(self) -> t.CChar | t.CPtr:
return self.data
def reset(self):
self.length = 0
if self.data != None and self.capacity > 0:
self.data[0] = 0
def __enter__(self) -> 'Buf' | t.CPtr:
return self
def __exit__(self):
self.reset()
def free(self):
if self.owned and self.data != None:
self.data[0] = 0
self.owned = False
self.data = None
self.length = 0
self.capacity = 0