@ohos-graphics/stability-code-review 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/README.md +169 -0
  2. package/SKILL.md +518 -0
  3. package/bin/install.js +165 -0
  4. package/config/rules.yaml +445 -0
  5. package/config/whitelist.yaml +52 -0
  6. package/package.json +40 -0
  7. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_001.md +275 -0
  8. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_002.md +273 -0
  9. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_003.md +305 -0
  10. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_004.md +350 -0
  11. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_005.md +301 -0
  12. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_006.md +320 -0
  13. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_007.md +432 -0
  14. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_008.md +394 -0
  15. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_009.md +425 -0
  16. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_010.md +472 -0
  17. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_011.md +204 -0
  18. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_012.md +210 -0
  19. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_013.md +226 -0
  20. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_014.md +222 -0
  21. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_015.md +256 -0
  22. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_016.md +269 -0
  23. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_017.md +222 -0
  24. package/references/BoundaryCondition/StabilityCodeReview_BoundaryCondition_018.md +336 -0
  25. package/references/ConcurrencyStability/StabilityCodeReview_ConcurrencyStability_001.md +414 -0
  26. package/references/ConcurrencyStability/StabilityCodeReview_ConcurrencyStability_002.md +335 -0
  27. package/references/ConcurrencyStability/StabilityCodeReview_ConcurrencyStability_003.md +284 -0
  28. package/references/ConcurrencyStability/StabilityCodeReview_ConcurrencyStability_004.md +313 -0
  29. package/references/ConcurrencyStability/StabilityCodeReview_ConcurrencyStability_005.md +364 -0
  30. package/references/ExceptionHandling/StabilityCodeReview_ExceptionHandling_001.md +142 -0
  31. package/references/ExceptionHandling/StabilityCodeReview_ExceptionHandling_002.md +222 -0
  32. package/references/ExceptionHandling/StabilityCodeReview_ExceptionHandling_003.md +383 -0
  33. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_001.md +258 -0
  34. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_002.md +131 -0
  35. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_003.md +220 -0
  36. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_004.md +224 -0
  37. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_005.md +250 -0
  38. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_006.md +153 -0
  39. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_007.md +169 -0
  40. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_008.md +153 -0
  41. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_009.md +144 -0
  42. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_010.md +152 -0
  43. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_011.md +221 -0
  44. package/references/GraphicsStability/StabilityCodeReview_GraphicsStability_012.md +318 -0
  45. package/references/InitializationOrder/StabilityCodeReview_InitializationOrder_001.md +411 -0
  46. package/references/Lifecycle/StabilityCodeReview_Lifecycle_001.md +255 -0
  47. package/references/Lifecycle/StabilityCodeReview_Lifecycle_002.md +177 -0
  48. package/references/MemoryStability/StabilityCodeReview_MemoryStability_001.md +332 -0
  49. package/references/MemoryStability/StabilityCodeReview_MemoryStability_002.md +261 -0
  50. package/references/MemoryStability/StabilityCodeReview_MemoryStability_003.md +428 -0
  51. package/references/MemoryStability/StabilityCodeReview_MemoryStability_004.md +400 -0
  52. package/references/MemoryStability/StabilityCodeReview_MemoryStability_005.md +364 -0
  53. package/references/MemoryStability/StabilityCodeReview_MemoryStability_006.md +359 -0
  54. package/references/MemoryStability/StabilityCodeReview_MemoryStability_007.md +279 -0
  55. package/references/PROBLEM_TEMPLATE.md +65 -0
  56. package/references/PerformanceStability/StabilityCodeReview_PerformanceStability_001.md +380 -0
  57. package/references/PerformanceStability/StabilityCodeReview_PerformanceStability_002.md +437 -0
  58. package/references/REPORT_TEMPLATE.csv +5 -0
  59. package/references/REPORT_TEMPLATE.md +132 -0
  60. package/references/RULE_DEVELOPMENT_GUIDE.md +711 -0
  61. package/references/RULE_INDEX.md +101 -0
  62. package/references/RULE_TEMPLATE.md +192 -0
  63. package/references/ResourceManagement/StabilityCodeReview_ResourceManagement_001.md +334 -0
  64. package/references/ResourceManagement/StabilityCodeReview_ResourceManagement_002.md +425 -0
  65. package/references/ResourceManagement/StabilityCodeReview_ResourceManagement_003.md +420 -0
  66. package/references/ResourceManagement/StabilityCodeReview_ResourceManagement_004.md +409 -0
  67. package/references/ResourceManagement/StabilityCodeReview_ResourceManagement_005.md +445 -0
  68. package/references/ResourceManagement/StabilityCodeReview_ResourceManagement_006.md +384 -0
  69. package/references/ResourceManagement/StabilityCodeReview_ResourceManagement_007.md +395 -0
  70. package/scripts/add-rule.py +423 -0
@@ -0,0 +1,142 @@
1
+ ---
2
+ rule_id: "StabilityCodeReview_ExceptionHandling_001"
3
+ name: "禁止异常处理机制"
4
+ category: "异常处理"
5
+ severity: "MEDIUM"
6
+ language: ["cpp", "c++"]
7
+ author: "OH-Department7 Stability Team"
8
+ ---
9
+
10
+ # 禁止异常处理机制
11
+
12
+ ## 问题描述
13
+
14
+ OpenHarmony 不允许使用 C++ 异常处理机制(try/catch/throw)。异常处理机制会引入运行时开销和代码膨胀,影响系统稳定性和性能。代码中如果使用了异常处理,违反了 OpenHarmony 编码规范。
15
+
16
+ ## 检测示例
17
+
18
+ ### ❌ 问题代码
19
+
20
+ ```cpp
21
+ // 使用 try/catch - 禁止
22
+ void ProcessData() {
23
+ try {
24
+ DoSomething();
25
+ } catch (const std::exception& e) {
26
+ LOGE("Exception: %s", e.what());
27
+ }
28
+ }
29
+
30
+ // 使用 throw - 禁止
31
+ void ValidateInput(int value) {
32
+ if (value < 0) {
33
+ throw std::invalid_argument("Invalid value");
34
+ }
35
+ }
36
+ ```
37
+
38
+ ### ✅ 修复方案
39
+
40
+ ```cpp
41
+ // 使用错误码返回值替代
42
+ ErrorCode ProcessData() {
43
+ ErrorCode ret = DoSomething();
44
+ if (ret != ErrorCode::SUCCESS) {
45
+ LOGE("DoSomething failed: %d", ret);
46
+ return ret;
47
+ }
48
+ return ErrorCode::SUCCESS;
49
+ }
50
+
51
+ // 使用错误码返回值替代
52
+ ErrorCode ValidateInput(int value) {
53
+ if (value < 0) {
54
+ LOGE("Invalid value: %d", value);
55
+ return ErrorCode::INVALID_PARAM;
56
+ }
57
+ return ErrorCode::SUCCESS;
58
+ }
59
+ ```
60
+
61
+ ## 检测范围
62
+
63
+ 检查以下关键字:
64
+
65
+ - `try` - try 块关键字
66
+ - `catch` - catch 块关键字
67
+ - `throw` - throw 语句关键字
68
+
69
+ ## 检测要点
70
+
71
+ 1. 检测代码中是否包含 try 关键字
72
+ 2. 检测代码中是否包含 catch 关键字
73
+ 3. 检测代码中是否包含 throw 关键字
74
+ 4. 排除 NOPROTECT 标记的代码
75
+
76
+ ## 风险流分析(RiskFlow)
77
+
78
+ - **RISK_SOURCE**: 代码中使用了 try/catch/throw 异常处理关键字
79
+ - **RISK_TYPE**: 违反 OpenHarmony 编码规范
80
+ - **RISK_PATH**: 异常处理机制引入运行时开销和代码膨胀
81
+ - **IMPACT_POINT**: 影响系统稳定性和性能
82
+
83
+ ## 影响分析(ImpactAnalysis)
84
+
85
+ - **Trigger**: 代码中使用异常处理机制
86
+ - **Propagation**: 异常处理机制引入运行时开销和代码膨胀
87
+ - **Consequence**: 违反 OpenHarmony 编码规范,可能导致稳定性问题
88
+ - **Mitigation**: 使用错误码返回值替代异常处理机制
89
+
90
+ ## 误报排除
91
+
92
+ | 场景 | 识别特征 | 处理方式 |
93
+ |------|----------|----------|
94
+ | NOPROTECT 标记 | 有 // NOPROTECT 注释 | 不报 |
95
+ | 第三方库 | 位于 third_party 目录 | 白名单排除 |
96
+ | 测试代码 | 位于 test 目录或 _test.cpp 文件 | 自动跳过 |
97
+ ## 测试用例
98
+
99
+ ### 触发用例(应该报)
100
+
101
+ ```cpp
102
+ // test_ExceptionHandling_001_trigger.cpp
103
+ void trigger_bad_1() {
104
+ try { // 应该报:使用了 try
105
+ DoSomething();
106
+ } catch (...) {
107
+ }
108
+ }
109
+
110
+ void trigger_bad_2() {
111
+ throw std::runtime_error("error"); // 应该报:使用了 throw
112
+ }
113
+
114
+ void trigger_bad_3() {
115
+ try {
116
+ Process();
117
+ } catch (const std::exception& e) { // 应该报:使用了 catch
118
+ }
119
+ }
120
+ ```
121
+
122
+ ### 安全用例(不应该报)
123
+
124
+ ```cpp
125
+ // test_ExceptionHandling_001_safe.cpp
126
+ void safe_good() {
127
+ // 使用错误码返回值
128
+ ErrorCode ret = DoSomething();
129
+ if (ret != ErrorCode::SUCCESS) {
130
+ LOGE("Failed: %d", ret);
131
+ }
132
+ }
133
+
134
+ // NOPROTECT 标记
135
+ void noprotect_case() {
136
+ // NOPROTECT: 第三方库接口要求使用异常
137
+ try {
138
+ ThirdPartyCall();
139
+ } catch (...) {
140
+ }
141
+ }
142
+ ```
@@ -0,0 +1,222 @@
1
+ ---
2
+ rule_id: "StabilityCodeReview_ExceptionHandling_002"
3
+ name: "异常分支应正确处理"
4
+ category: "异常处理"
5
+ severity: "MEDIUM"
6
+ language: ["cpp", "c++"]
7
+ author: "OH-Department7 Stability Team"
8
+ ---
9
+
10
+ # 异常分支应正确处理
11
+
12
+ ## 问题描述
13
+
14
+ 异常处理分支应该有合适的处理方式,不能静默忽略或遗漏return。异常分支中如果只打印日志而没有return,会导致程序继续执行后续逻辑,可能引发更严重的问题。需要根据业务需要来决定异常分支的处理方式。
15
+
16
+ ## 检测示例
17
+
18
+ ### ❌ 问题代码
19
+
20
+ ```cpp
21
+ // 场景1:异常分支只打印日志,缺少return
22
+ void ProcessData(int* data, int size) {
23
+ if (data == nullptr) {
24
+ LOGE("data is null"); // 缺少 return
25
+ }
26
+ // 继续执行,导致空指针访问
27
+ for (int i = 0; i < size; i++) {
28
+ data[i] = 0;
29
+ }
30
+ }
31
+
32
+ // 场景2:异常分支空实现,完全静默
33
+ int CalculateResult(int value) {
34
+ if (value < 0) {
35
+ // 空实现,既没有日志也没有return
36
+ }
37
+ return value * 2;
38
+ }
39
+
40
+ // 场景3:异常分支只有注释,缺少实际处理
41
+ bool ValidateConfig(const Config& config) {
42
+ if (config.path.empty()) {
43
+ // TODO: handle error // 只有TODO注释
44
+ }
45
+ return true; // 错误地返回true
46
+ }
47
+ ```
48
+
49
+ ### ✅ 修复方案
50
+
51
+ ```cpp
52
+ // 修复场景1:根据业务需要添加return
53
+ void ProcessData(int* data, int size) {
54
+ if (data == nullptr) {
55
+ LOGE("data is null");
56
+ return; // 添加return,避免空指针访问
57
+ }
58
+ for (int i = 0; i < size; i++) {
59
+ data[i] = 0;
60
+ }
61
+ }
62
+
63
+ // 修复场景2:异常分支记录日志并返回错误值
64
+ int CalculateResult(int value) {
65
+ if (value < 0) {
66
+ LOGE("Invalid value: %d, must be non-negative", value);
67
+ return -1; // 返回错误码
68
+ }
69
+ return value * 2;
70
+ }
71
+
72
+ // 修复场景3:异常分支返回失败状态
73
+ bool ValidateConfig(const Config& config) {
74
+ if (config.path.empty()) {
75
+ LOGE("Config path is empty");
76
+ return false; // 返回失败状态
77
+ }
78
+ return true;
79
+ }
80
+
81
+ // 修复场景4:异常分支包含资源清理
82
+ void ProcessResource(Resource* res, const std::string& data) {
83
+ if (res == nullptr) {
84
+ LOGE("Resource is null");
85
+ return; // 添加return,避免后续资源操作
86
+ }
87
+ if (data.empty()) {
88
+ LOGE("Data is empty, releasing resource");
89
+ ReleaseResource(res); // 清理已分配的资源
90
+ return;
91
+ }
92
+ // 正常处理流程
93
+ }
94
+
95
+ // 修复场景5:void函数无日志return(适用于Dump/Debug函数)
96
+ void DumpNodeInfo(const Node* node) {
97
+ if (node == nullptr) {
98
+ return; // Debug函数中只return无日志是合理的
99
+ }
100
+ PrintNodeDetails(node);
101
+ }
102
+ ```
103
+
104
+ ## 检测范围
105
+
106
+ 检查以下异常分支模式:
107
+
108
+ - `if` 条件判断后只有日志语句
109
+ - `if` 条件判断后空实现(只有注释或空白)
110
+ - 错误处理分支缺少 `return` 语句
111
+
112
+ ## 检测要点
113
+
114
+ 1. 识别 `if` 语句块,特别是包含错误检查的条件(如 `nullptr`、`< 0`、`empty()`)
115
+ 2. 检查 `if` 块内是否只有日志语句(`LOGE`、`LOG_ERROR` 等)或空实现
116
+ 3. 检查是否缺少 `return` 语句或有效的错误处理
117
+ 4. 排除有明确业务逻辑处理的异常分支
118
+
119
+ ### 日志级别使用说明
120
+
121
+ 异常分支中的日志级别应根据问题严重程度选择:
122
+
123
+ - **LOGE/LOG_ERROR**:严重错误,影响核心功能或系统稳定性,必须立即处理
124
+ - 示例:空指针检查、关键资源分配失败、数据完整性错误
125
+ - 场景:`if (ptr == nullptr) { LOGE("Critical: null pointer"); return; }`
126
+
127
+ - **LOGW/LOG_WARN**:警告信息,可能影响功能但系统可继续运行
128
+ - 示例:参数范围警告、性能降级、兼容性问题
129
+ - 场景:`if (value < 0) { LOGW("Warning: negative value"); return -1; }`
130
+
131
+ - **LOGD/LOG_DEBUG**:调试信息,仅在调试构建中输出
132
+ - 示例:调试函数中的状态检查、非关键分支路径
133
+ - 场景:Dump类函数、调试辅助函数
134
+
135
+ - **LOGI/LOG_INFO**:一般信息,记录程序运行状态
136
+ - 示例:非关键条件分支、可选功能未启用
137
+ - 场景:配置缺失但使用默认值的场景
138
+
139
+ ## 风险流分析(RiskFlow)
140
+
141
+ - **RISK_SOURCE**:异常分支缺少有效处理(只打印日志或空实现)
142
+ - **RISK_TYPE**:异常处理不当导致逻辑缺陷
143
+ - **RISK_PATH**:异常情况发生后程序继续执行,导致后续代码在非法状态下运行
144
+ - **IMPACT_POINT**:可能引发空指针访问、数据损坏、功能异常等严重问题
145
+
146
+ ## 影响分析(ImpactAnalysis)
147
+
148
+ - **Trigger**:异常条件触发,但异常分支未正确处理
149
+ - **Propagation**:程序继续执行后续代码,使用非法数据或状态
150
+ - **Consequence**:空指针崩溃、数据损坏、功能异常,影响系统稳定性
151
+ - **Mitigation**:异常分支应根据业务需要添加return语句或有效错误处理
152
+
153
+ ## 误报排除
154
+
155
+ | 场景 | 识别特征 | 处理方式 |
156
+ |------|----------|----------|
157
+ | 故意继续执行 | 有明确业务逻辑处理 | 不报 |
158
+ | NOPROTECT 标记 | 有 // NOPROTECT 注释 | 不报 |
159
+ | 统计日志场景 | 只有统计目的的日志 | 根据上下文判断 |
160
+ | 条件设置场景 | if 用于设置默认值 | 不报 |
161
+ | Dump/Debug 函数 | 函数名含Dump/Debug/Log/Print | 可接受无日志的return |
162
+ | 资源清理场景 | 异常分支包含Release/Close/Delete等清理操作 | 不报(已有处理) |
163
+ ## 测试用例
164
+
165
+ ### 触发用例(应该报)
166
+
167
+ ```cpp
168
+ // test_ExceptionHandling_002_trigger.cpp
169
+ void trigger_bad_1() {
170
+ int* ptr = nullptr;
171
+ if (ptr == nullptr) {
172
+ LOGE("ptr is null"); // 应该报:缺少return
173
+ }
174
+ *ptr = 10; // 空指针访问
175
+ }
176
+
177
+ int trigger_bad_2(int value) {
178
+ if (value < 0) {
179
+ // 空实现 // 应该报:既没有日志也没有return
180
+ }
181
+ return value * 2;
182
+ }
183
+
184
+ bool trigger_bad_3(const std::string& path) {
185
+ if (path.empty()) {
186
+ // TODO: handle error // 应该报:只有TODO注释
187
+ }
188
+ return true;
189
+ }
190
+ ```
191
+
192
+ ### 安全用例(不应该报)
193
+
194
+ ```cpp
195
+ // test_ExceptionHandling_002_safe.cpp
196
+ void safe_good_1(int* data, int size) {
197
+ if (data == nullptr) {
198
+ LOGE("data is null");
199
+ return; // 正确:有return
200
+ }
201
+ for (int i = 0; i < size; i++) {
202
+ data[i] = 0;
203
+ }
204
+ }
205
+
206
+ int safe_good_2(int value) {
207
+ if (value < 0) {
208
+ LOGE("Invalid value: %d", value);
209
+ return -1; // 正确:返回错误码
210
+ }
211
+ return value * 2;
212
+ }
213
+
214
+ // NOPROTECT 标记
215
+ void noprotect_case(int* ptr) {
216
+ // NOPROTECT: 此处故意继续执行
217
+ if (ptr == nullptr) {
218
+ LOGW("ptr is null, using default");
219
+ }
220
+ // 继续执行...
221
+ }
222
+ ```