@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,279 @@
1
+ ---
2
+ rule_id: "StabilityCodeReview_MemoryStability_007"
3
+ name: "函数返回指针未检查NULL"
4
+ category: "内存稳定性"
5
+ severity: "CRITICAL"
6
+ language: ["cpp", "c++"]
7
+ author: "OH-Department7 Stability Team"
8
+ ---
9
+
10
+ # 函数返回指针未检查NULL
11
+
12
+ ## 问题描述
13
+
14
+ 函数返回指针类型时可能返回NULL表示错误或特殊情况,调用方未检查返回值直接使用会导致空指针解引用,引发程序崩溃或拒绝服务。
15
+
16
+ ## 检测示例
17
+
18
+ ### ❌ 问题代码
19
+
20
+ ```cpp
21
+ // 场景1:工厂函数返回NULL未检查
22
+ void ProcessObject()
23
+ {
24
+ Object* obj = CreateObject(); // 可能返回nullptr
25
+ obj->Initialize(); // 错误:未检查obj是否为nullptr
26
+ obj->Process();
27
+ }
28
+
29
+ // 场景2:查找函数返回NULL未检查
30
+ void UseFoundItem()
31
+ {
32
+ Item* item = FindItem(id); // 可能返回nullptr(未找到)
33
+ item->Update(); // 错误:未检查item是否为nullptr
34
+ }
35
+
36
+ // 场景3:分配函数返回NULL未检查
37
+ void AllocateResource()
38
+ {
39
+ Resource* res = AllocateResource(); // 可能返回nullptr(分配失败)
40
+ res->Use(); // 错误:未检查res是否为nullptr
41
+ }
42
+
43
+ // 场景4:获取器函数返回NULL未检查
44
+ void AccessMember()
45
+ {
46
+ Member* member = obj.GetMember(); // 可能返回nullptr
47
+ member->SetValue(100); // 错误:未检查member是否为nullptr
48
+ }
49
+
50
+ // 场景5:智能指针get()后未检查
51
+ void UseSmartPointer()
52
+ {
53
+ std::shared_ptr<Data> ptr = GetData();
54
+ Data* raw = ptr.get(); // ptr可能为空,raw为nullptr
55
+ raw->Process(); // 错误:未检查raw是否为nullptr
56
+ }
57
+
58
+ // 场景6:链式调用返回NULL
59
+ void ChainAccess()
60
+ {
61
+ Node* node = GetRootNode(); // 可能返回nullptr
62
+ node->GetNext()->GetValue(); // 错误:node或GetNext可能返回nullptr
63
+ }
64
+ ```
65
+
66
+ ### ✅ 修复方案
67
+
68
+ ```cpp
69
+ // 修复场景1:工厂函数返回值检查
70
+ void ProcessObject()
71
+ {
72
+ Object* obj = CreateObject();
73
+ if (obj == nullptr) { // 正确:检查返回值
74
+ LOGE("CreateObject failed");
75
+ return;
76
+ }
77
+ obj->Initialize();
78
+ obj->Process();
79
+ }
80
+
81
+ // 修复场景2:查找函数返回值检查
82
+ void UseFoundItem()
83
+ {
84
+ Item* item = FindItem(id);
85
+ if (item == nullptr) { // 正确:检查返回值
86
+ LOGE("Item not found: %d", id);
87
+ return;
88
+ }
89
+ item->Update();
90
+ }
91
+
92
+ // 修复场景3:分配函数返回值检查
93
+ void AllocateResource()
94
+ {
95
+ Resource* res = AllocateResource();
96
+ if (res == nullptr) { // 正确:检查返回值
97
+ LOGE("AllocateResource failed");
98
+ return;
99
+ }
100
+ res->Use();
101
+ }
102
+
103
+ // 修复场景4:获取器函数返回值检查
104
+ void AccessMember()
105
+ {
106
+ Member* member = obj.GetMember();
107
+ if (member == nullptr) { // 正确:检查返回值
108
+ LOGE("GetMember returned nullptr");
109
+ return;
110
+ }
111
+ member->SetValue(100);
112
+ }
113
+
114
+ // 修复场景5:智能指针直接使用或检查
115
+ void UseSmartPointer()
116
+ {
117
+ std::shared_ptr<Data> ptr = GetData();
118
+ if (!ptr) { // 正确:检查智能指针
119
+ LOGE("GetData returned empty");
120
+ return;
121
+ }
122
+ ptr->Process(); // 直接使用智能指针,无需get()
123
+ }
124
+
125
+ // 修复场景6:链式调用逐级检查
126
+ void ChainAccess()
127
+ {
128
+ Node* node = GetRootNode();
129
+ if (node == nullptr) { // 正确:检查第一级
130
+ LOGE("GetRootNode returned nullptr");
131
+ return;
132
+ }
133
+
134
+ Node* next = node->GetNext();
135
+ if (next == nullptr) { // 正确:检查第二级
136
+ LOGE("GetNext returned nullptr");
137
+ return;
138
+ }
139
+
140
+ next->GetValue();
141
+ }
142
+
143
+ // 修复场景7:使用智能指针代替裸指针
144
+ void UseSmartPointerSafe()
145
+ {
146
+ std::unique_ptr<Object> obj = CreateObjectUnique(); // 使用智能指针版本
147
+ if (!obj) {
148
+ return;
149
+ }
150
+ obj->Initialize(); // 安全:智能指针管理
151
+ }
152
+ ```
153
+
154
+ ## 检测范围
155
+
156
+ 检查以下模式:
157
+
158
+ - 工厂函数(Create、New、Make)返回指针未检查
159
+ - 查找函数(Find、Get、Lookup)返回指针未检查
160
+ - 分配函数(Allocate、Acquire)返回指针未检查
161
+ - 获取器函数(GetMember、GetData)返回指针未检查
162
+ - 智能指针get()后裸指针未检查
163
+ - 链式调用中每一级返回指针未检查
164
+
165
+ ## 检测要点
166
+
167
+ 1. 识别函数返回类型为指针(Type*)
168
+ 2. 检查函数名特征(Create、Find、Get、Allocate等)
169
+ 3. 检查调用后是否有判空操作(if ptr == nullptr)
170
+ 4. 检查是否在判空前有解引用操作
171
+ 5. 排除已知不会返回nullptr的函数(如成员函数返回this)
172
+ 6. 排除NOPROTECT标记
173
+
174
+ ## 风险流分析(RiskFlow)
175
+
176
+ - **RISK_SOURCE**:返回指针类型的函数
177
+ - **RISK_TYPE**:空指针解引用
178
+ - **RISK_PATH**:函数返回nullptr -> 未检查返回值 -> 直接解引用 -> 空指针崩溃
179
+ - **IMPACT_POINT**:程序崩溃、服务中断
180
+
181
+ ## 影响分析(ImpactAnalysis)
182
+
183
+ - **Trigger**:函数返回nullptr(表示错误或特殊情况)
184
+ - **Propagation**:调用方未检查,直接解引用nullptr指针
185
+ - **Consequence**:空指针解引用导致程序崩溃(SIGSEGV)
186
+ - **Mitigation**:所有指针返回值必须判空后使用,或使用智能指针
187
+
188
+ ## 误报排除
189
+
190
+ | 场景 | 识别特征 | 处理方式 |
191
+ |------|----------|----------|
192
+ | 已判空 | 有 if (ptr == nullptr) 检查 | 不报 |
193
+ | 智能指针直接用 | shared_ptr/unique_ptr->method() | 不报 |
194
+ | 成员函数返回this | return this 或 return *this | 不报 |
195
+ | 已知安全函数 | 函数注释保证不返回nullptr | 上下文分析 |
196
+ | NOPROTECT标记 | // NOPROTECT 注释 | 不报 |
197
+ | 测试代码 | 文件名包含 _test.cpp | 白名单排除 |
198
+ ## 测试用例
199
+
200
+ ### 触发用例(应该报)
201
+
202
+ ```cpp
203
+ // test_MemoryStability_007_trigger.cpp
204
+ void trigger_bad_1()
205
+ {
206
+ Object* obj = CreateObject(); // 应该报:返回指针未检查
207
+ obj->Initialize();
208
+ }
209
+
210
+ void trigger_bad_2()
211
+ {
212
+ Item* item = FindItem(id); // 应该报:返回指针未检查
213
+ item->Update();
214
+ }
215
+
216
+ void trigger_bad_3()
217
+ {
218
+ std::shared_ptr<Data> ptr = GetData();
219
+ Data* raw = ptr.get();
220
+ raw->Process(); // 应该报:智能指针get()后未检查
221
+ }
222
+
223
+ void trigger_bad_4()
224
+ {
225
+ Node* node = GetRootNode(); // 应该报:链式调用未逐级检查
226
+ node->GetNext()->GetValue();
227
+ }
228
+ ```
229
+
230
+ ### 安全用例(不应该报)
231
+
232
+ ```cpp
233
+ // test_MemoryStability_007_safe.cpp
234
+ void safe_good_1()
235
+ {
236
+ Object* obj = CreateObject();
237
+ if (obj == nullptr) { // 安全:有判空检查
238
+ return;
239
+ }
240
+ obj->Initialize();
241
+ }
242
+
243
+ void safe_good_2()
244
+ {
245
+ std::shared_ptr<Data> ptr = GetData();
246
+ if (!ptr) { // 安全:智能指针判空
247
+ return;
248
+ }
249
+ ptr->Process();
250
+ }
251
+
252
+ void safe_good_3()
253
+ {
254
+ Object* obj = CreateObject();
255
+ if (obj) { // 安全:if条件中判空
256
+ obj->Initialize();
257
+ }
258
+ }
259
+
260
+ void safe_good_4()
261
+ {
262
+ Node* node = GetRootNode();
263
+ if (node == nullptr) { // 安全:第一级检查
264
+ return;
265
+ }
266
+ Node* next = node->GetNext();
267
+ if (next == nullptr) { // 安全:第二级检查
268
+ return;
269
+ }
270
+ next->GetValue();
271
+ }
272
+
273
+ // NOPROTECT: 特殊场景
274
+ void noprotect_case()
275
+ {
276
+ Object* obj = CreateObject();
277
+ obj->Initialize(); // NOPROTECT标记,不报
278
+ }
279
+ ```
@@ -0,0 +1,65 @@
1
+ # 检出问题格式模板
2
+
3
+ 本文档定义了稳定性代码检视报告中每个检出问题的标准格式。
4
+
5
+ ## 问题格式规范
6
+
7
+ 每个检出的问题**必须**包含以下完整信息:
8
+
9
+ #### 问题 `<序号>` : `<问题标题>`
10
+
11
+ **分类**: `<分类名称>`
12
+ **规则**: `<规则ID> - <规则名称> - <严重程度>`
13
+ **位置**: `<文件相对路径>:<问题代码关键行号>`
14
+ **函数**: `<类名>::<函数名>` 或 `<命名空间>::<函数名>` 或 `<函数名>`
15
+
16
+ **关键源码片段**:
17
+ ```cpp
18
+ `<起始行号> | <代码行1>`
19
+ `<起始行号+1> | <代码行2>`
20
+ `...` (省略非必要代码)
21
+ `<关键行号> | <问题代码行>`
22
+ `...` (省略非必要代码)
23
+ `<结束行号> | <代码行n>`
24
+ ```
25
+
26
+ **问题描述**:
27
+ `<问题详细描述,说明违反了什么规则,存在什么风险>`
28
+
29
+ **风险流分析(RiskFlow)**:
30
+ ```
31
+ 风险来源: `<风险如何产生>`
32
+
33
+ 传播路径: `<风险如何传播>`
34
+
35
+ 影响点: `<风险在何处造成影响>`
36
+ ```
37
+
38
+ **影响分析 (ImpactAnalysis)**:
39
+ ```
40
+ 触发条件: `<什么条件下会触发问题>`
41
+
42
+ 传播方式: `<问题如何扩散>`
43
+
44
+ 后果: `<问题造成的稳定性影响>`
45
+ ```
46
+
47
+ **修复建议**:
48
+ `<修复建议描述>`
49
+ ```cpp
50
+ `<起始行号> | <修复后的代码行1>`
51
+ `<起始行号+1> | <修复后的代码行2>`
52
+ `...` (省略非必要代码)
53
+ `<关键行号> | <修复后的问题代码行>`
54
+ `...` (省略非必要代码)
55
+ `<结束行号> | <修复后的代码行n>`
56
+ ```
57
+
58
+ ## 注意事项
59
+
60
+ 1. **代码片段完整性**:提取的代码片段应包含足够的上下文,便于理解问题
61
+ 2. **行号格式规范**:代码行号采用 `行号 | 代码内容` 格式,保持竖线对齐
62
+ 3. **省略号使用**:使用 `...` 省略非必要的代码行,仅保留问题相关的关键上下文
63
+ 4. **问题描述清晰**:说明违反的规则、存在的风险、潜在影响
64
+ 5. **风险流分析完整**:从风险来源到影响点,完整描述传播链路
65
+ 6. **修复建议可行**:提供具体的修复方案和代码示例,确保可落地执行