a2242b7345
装真 cppcheck 2.21.0 后全库扫出 nullPointerRedundantCheck 两条,落在
App/iec60870/104/iec104_net_mcu.c 的 IEC104_Net_Listen:
if (... || listener == NULL || bound_port == NULL || ...) {
*listener = IEC104_NET_INVALID_SOCKET; /* listener 可能就是 NULL */
*bound_port = 0U; /* bound_port 可能就是 NULL */
return ...;
}
判空之后在同一个失败分支里解引用这两个指针。真机上当前唯一调用点传的都是
合法对象地址,所以现网不发作;但这是 ABI 的防御分支,任何传 NULL 的新调用点
或测试都会踩空指针——实测坏实现上进程直接 0xC0000005 终止。
同目录 IEC104_Net_Windows.c 的同名函数早已是正确的两步写法(先条件清空输出、
再判空返回),本次把 MCU 版对齐到那个顺序。s_inited 与 owner 两个条件原样保留。
校验之后原来那两行无条件清空同时删除——拆分后走到那里两指针必然非空、输出
已是 INVALID/0,保留只是同值重赋。
TDD:先加 N13~N16 四条入参校验用例(bind_address NULL / 未 Init / listener NULL
/ bound_port NULL),在旧实现上跑是崩溃红(run_rc=0xC0000005、无 FAIL 行),
修完 17/17 绿。四条断言 ERROR+NET_ERR_INVAL、可写输出被清成 INVALID/0、零硬件调用。
顺带修掉 runner 一个假绿:旧口径用例数取 CASE_COUNT - len(failed_cases),进程
被访问违例杀掉时没有 FAIL 行,于是打印 "passed 13/13" 并 return 0。现改为
run_rc != 0 或没跑到汇总行 → 打印 NOT green + return 1。这是本次红证据能成立
的前提——没有它,崩溃会被读成满分。
另:results/ 下三份 report 与 selftest 改为 LF + 尾换行(json.dump 原来不带),
selftest.txt 改记仓库相对路径(原来写绝对路径,跨机器/跨工作树必漂)。
118 lines
4.3 KiB
Python
118 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""Static acceptance checks for App/iec60870/104/iec104_net_mcu.c
|
||
|
||
S1: IEC104_Net_Close CLIENT/CLIENT_EOF branch must not contain net_close_sock.
|
||
S2: IEC104_Net_Recv rc==0 EOF branch must not call close/drop on the session.
|
||
Both checks use brace matching from the exact branch header, not line guesses.
|
||
"""
|
||
import hashlib
|
||
import json
|
||
import os
|
||
import re
|
||
|
||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
|
||
SRC = os.path.join(ROOT, "App", "iec60870", "104", "iec104_net_mcu.c")
|
||
OUT = os.path.join(HERE, "results", "static")
|
||
|
||
|
||
def sha256(path):
|
||
h = hashlib.sha256()
|
||
with open(path, "rb") as fh:
|
||
h.update(fh.read())
|
||
return h.hexdigest()
|
||
|
||
|
||
def brace_span(text, brace_pos):
|
||
assert text[brace_pos] == "{", "not a brace"
|
||
depth = 0
|
||
i = brace_pos
|
||
while i < len(text):
|
||
c = text[i]
|
||
if c == "{":
|
||
depth += 1
|
||
elif c == "}":
|
||
depth -= 1
|
||
if depth == 0:
|
||
return brace_pos, i + 1
|
||
i += 1
|
||
raise ValueError("unbalanced brace")
|
||
|
||
|
||
def func_body(text, name):
|
||
m = re.search(r"\b" + re.escape(name) + r"\s*\([^;{]*\)\s*\{", text)
|
||
if m is None:
|
||
raise ValueError("function not found: " + name)
|
||
start = text.index("{", m.start())
|
||
return brace_span(text, start)
|
||
|
||
|
||
def branch_body(text, span, needle):
|
||
a, b = span
|
||
pos = text.find(needle, a, b)
|
||
if pos < 0:
|
||
raise ValueError("branch not found: " + needle)
|
||
brace = text.index("{", pos)
|
||
assert brace < b
|
||
return brace_span(text, brace)
|
||
|
||
|
||
def main(src_path=None, report_path=None):
|
||
src_path = src_path or SRC
|
||
report_path = report_path or OUT
|
||
os.makedirs(report_path, exist_ok=True)
|
||
with open(src_path, "r", encoding="utf-8") as fh:
|
||
text = fh.read()
|
||
checks = []
|
||
close_span = func_body(text, "IEC104_Net_Close")
|
||
client_branch = branch_body(text, close_span,
|
||
"if (slot->role == SLOT_CLIENT || slot->role == SLOT_CLIENT_EOF)")
|
||
body = text[client_branch[0]:client_branch[1]]
|
||
checks.append({
|
||
"id": "S1",
|
||
"description": "Close CLIENT/CLIENT_EOF branch contains no net_close_sock",
|
||
"passed": "net_close_sock" not in body,
|
||
"source_lines": [text.count("\n", 0, client_branch[0]) + 1,
|
||
text.count("\n", 0, client_branch[1]) + 1],
|
||
"evidence": "\n".join(text[client_branch[0]:client_branch[1]].splitlines()[:3]),
|
||
})
|
||
recv_span = func_body(text, "IEC104_Net_Recv")
|
||
eof_branch = branch_body(text, recv_span, "if (rc == 0)")
|
||
eof_body = text[eof_branch[0]:eof_branch[1]]
|
||
forbidden = [tok for tok in ["net_close_sock", "net_drop_conn_locked",
|
||
"net_close(", "net_drop_conn("] if tok in eof_body]
|
||
checks.append({
|
||
"id": "S2",
|
||
"description": "Recv EOF branch only changes role; no close/drop call",
|
||
"passed": not forbidden,
|
||
"source_lines": [text.count("\n", 0, eof_branch[0]) + 1,
|
||
text.count("\n", 0, eof_branch[1]) + 1],
|
||
"evidence": eof_body.strip(),
|
||
})
|
||
report = {
|
||
"source": src_path,
|
||
"source_sha256": sha256(src_path),
|
||
"checks": checks,
|
||
"passed": sum(1 for c in checks if c["passed"]),
|
||
"total": len(checks),
|
||
}
|
||
# newline="\n":results/ 下入库产物统一 LF(同 run_netmcu_tests.py)。
|
||
with open(os.path.join(report_path, "report.json"), "w", encoding="utf-8",
|
||
newline="\n") as fh:
|
||
json.dump(report, fh, ensure_ascii=False, indent=2)
|
||
fh.write("\n") # end-of-file-fixer 要求文本文件以换行收尾
|
||
print("static source sha256={}".format(report["source_sha256"]))
|
||
for c in checks:
|
||
print("{}: {} {}-{} passed={}".format(c["id"], c["description"],
|
||
c["source_lines"][0], c["source_lines"][1],
|
||
c["passed"]))
|
||
if not c["passed"]:
|
||
print(" evidence: {}".format(c["evidence"].replace("\n", " | ")))
|
||
print("static passed {}/{}".format(report["passed"], report["total"]))
|
||
return 0 if report["passed"] == report["total"] else 1
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|