Files
STM32F4-Base/test/host/iec104_netmcu/check_static_selftest.py
T
wandering1 a2242b7345 fix(iec104): IEC104_Net_Listen 判空与解引用落在同一分支(真 cppcheck 抓出)
装真 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 改记仓库相对路径(原来写绝对路径,跨机器/跨工作树必漂)。
2026-11-19 13:05:22 +08:00

60 lines
2.3 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Self-test the static checker against a deliberately BAD source copy.
Generates a copy of the real backend with `net_close_sock` injected into the
IEC104_Net_Close CLIENT branch, runs check_static.py.main() on the copy, and
asserts S1 fails there while the real source still passes. The product source
is never edited; the mutant stays under results/static_mutant/.
"""
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import check_static # noqa: E402
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE)))
SRC = check_static.SRC
OUT = os.path.join(HERE, "results", "static_mutant")
MUTANT = os.path.join(OUT, "iec104_net_mcu_BAD.c")
NEEDLE = " client_slot_drop(slot);\n return result(IEC104_NET_OK, 0, native_error);\n }\n /* LISTEN 分支"
def _repo_rel(path):
"""报告里写仓库相对路径:绝对路径会随机器/工作树漂移,把入库产物变成噪音 diff。"""
try:
rel = os.path.relpath(path, ROOT)
except ValueError: # 不同盘符:Windows 下 relpath 会抛
return path
return path if rel.startswith("..") else rel.replace(os.sep, "/")
def main():
os.makedirs(OUT, exist_ok=True)
with open(SRC, "r", encoding="utf-8") as fh:
text = fh.read()
assert text.count(NEEDLE) == 1, "Close CLIENT branch marker not unique"
bad = text.replace(
NEEDLE,
" (void)net_close_sock(net_get_sock(slot->fd)); /* BAD mutant */\n" + NEEDLE,
1,
)
assert bad != text, "mutant injection did not change the copy"
with open(MUTANT, "w", encoding="utf-8", newline="\n") as fh:
fh.write(bad)
real_rc = check_static.main()
mutant_rc = check_static.main(MUTANT, OUT)
with open(os.path.join(OUT, "selftest.txt"), "w", encoding="utf-8",
newline="\n") as fh:
fh.write("real_rc={}\nmutant_rc={}\n".format(real_rc, mutant_rc))
fh.write("real source: {}\nmutant: {}\n".format(
_repo_rel(SRC), _repo_rel(MUTANT)))
print("static-checker selftest: real_rc={} mutant_rc={}".format(real_rc, mutant_rc))
return 0 if real_rc == 0 and mutant_rc != 0 else 1
if __name__ == "__main__":
raise SystemExit(main())