From 7aaea3a4d3ec4e9eeb629c5c20dda8d6b02aa3fa Mon Sep 17 00:00:00 2001 From: Microindole Date: Tue, 12 May 2026 23:12:51 +0800 Subject: [PATCH] tools: reject bare negative errno returns --- docs/agents/code-style.md | 2 ++ scripts/tools/check_structure.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/docs/agents/code-style.md b/docs/agents/code-style.md index 931bc3c..d23d84b 100644 --- a/docs/agents/code-style.md +++ b/docs/agents/code-style.md @@ -90,4 +90,6 @@ `-EINVAL`、`-ENOMEM`、`-ENOENT`、`-EBUSY`、`-EEXIST`、`-ETIMEDOUT`。 - 不要用裸 `-1` 表达多个失败原因;调用者需要能区分输入错误、资源耗尽、 对象已存在和超时。 +- 不要直接返回 `-22` 这类负数字面量;使用 `-EINVAL` 这类符号化 errno, + 结构检查会拒绝裸负数返回。 - errno 常量放在 `include/tianole/errno.h`,只增加当前内核实际使用的值。 diff --git a/scripts/tools/check_structure.py b/scripts/tools/check_structure.py index 8fa7007..1e02679 100644 --- a/scripts/tools/check_structure.py +++ b/scripts/tools/check_structure.py @@ -109,6 +109,21 @@ def check_no_relative_parent_includes(root: Path, files: list[Path]) -> list[str return errors +def check_no_bare_negative_errno(root: Path, files: list[Path]) -> list[str]: + errors = [] + return_re = re.compile(r"\breturn\s+-[0-9]+\s*;") + + for path in files: + for line_no, line in enumerate(read_text(root, path).splitlines(), 1): + if return_re.search(line): + errors.append( + f"{path}:{line_no}: return a symbolic negative errno " + "such as -EINVAL instead of a bare negative number" + ) + + return errors + + def is_function_declaration_start(line: str) -> bool: stripped = line.strip() @@ -408,6 +423,7 @@ def main() -> int: errors = [] errors.extend(check_no_relative_parent_includes(root, source_files(files))) + errors.extend(check_no_bare_negative_errno(root, source_files(files))) errors.extend(check_public_header_docs(root, public_headers(files))) errors.extend(check_selftests_are_centralized(c_files(files))) errors.extend(check_makefile_source_lists(root, files, all_mode))