@mytechtoday/augment-extensions 1.4.0 → 1.5.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.
- package/augment-extensions/coding-standards/c/CHANGELOG.md +55 -0
- package/augment-extensions/coding-standards/c/LICENSE +22 -0
- package/augment-extensions/coding-standards/c/README.md +167 -0
- package/augment-extensions/coding-standards/c/config/defaults.json +26 -0
- package/augment-extensions/coding-standards/c/config/examples/embedded.yaml +25 -0
- package/augment-extensions/coding-standards/c/config/examples/systems.json +31 -0
- package/augment-extensions/coding-standards/c/config/schema.json +244 -0
- package/augment-extensions/coding-standards/c/docs/API.md +613 -0
- package/augment-extensions/coding-standards/c/docs/CONFIGURATION.md +259 -0
- package/augment-extensions/coding-standards/c/docs/USER_GUIDE.md +567 -0
- package/augment-extensions/coding-standards/c/examples/drivers/Makefile +33 -0
- package/augment-extensions/coding-standards/c/examples/drivers/README.md +192 -0
- package/augment-extensions/coding-standards/c/examples/drivers/dma-example.c +224 -0
- package/augment-extensions/coding-standards/c/examples/drivers/example.dts +64 -0
- package/augment-extensions/coding-standards/c/examples/drivers/platform-driver.c +174 -0
- package/augment-extensions/coding-standards/c/examples/embedded/README.md +167 -0
- package/augment-extensions/coding-standards/c/examples/embedded/gpio-control.c +172 -0
- package/augment-extensions/coding-standards/c/examples/embedded/timer-isr.c +198 -0
- package/augment-extensions/coding-standards/c/examples/embedded/uart-communication.c +212 -0
- package/augment-extensions/coding-standards/c/examples/kernel/Makefile +82 -0
- package/augment-extensions/coding-standards/c/examples/kernel/README.md +168 -0
- package/augment-extensions/coding-standards/c/examples/kernel/char-device.c +198 -0
- package/augment-extensions/coding-standards/c/examples/kernel/proc-file.c +131 -0
- package/augment-extensions/coding-standards/c/examples/kernel/simple-module.c +111 -0
- package/augment-extensions/coding-standards/c/examples/legacy/Makefile +62 -0
- package/augment-extensions/coding-standards/c/examples/legacy/README.md +255 -0
- package/augment-extensions/coding-standards/c/examples/legacy/c89-to-c11-migration.c +268 -0
- package/augment-extensions/coding-standards/c/examples/legacy/compatibility-layer.c +239 -0
- package/augment-extensions/coding-standards/c/examples/networking/Makefile +35 -0
- package/augment-extensions/coding-standards/c/examples/networking/README.md +207 -0
- package/augment-extensions/coding-standards/c/examples/networking/protocol-parser.c +270 -0
- package/augment-extensions/coding-standards/c/examples/networking/tcp-server.c +197 -0
- package/augment-extensions/coding-standards/c/examples/networking/udp-multicast.c +220 -0
- package/augment-extensions/coding-standards/c/examples/realtime/Makefile +53 -0
- package/augment-extensions/coding-standards/c/examples/realtime/README.md +199 -0
- package/augment-extensions/coding-standards/c/examples/realtime/deadline-monitoring.c +260 -0
- package/augment-extensions/coding-standards/c/examples/realtime/priority-scheduling.c +258 -0
- package/augment-extensions/coding-standards/c/examples/systems/Makefile +34 -0
- package/augment-extensions/coding-standards/c/examples/systems/README.md +123 -0
- package/augment-extensions/coding-standards/c/examples/systems/ipc-pipes.c +181 -0
- package/augment-extensions/coding-standards/c/examples/systems/process-management.c +153 -0
- package/augment-extensions/coding-standards/c/examples/systems/signal-handling.c +162 -0
- package/augment-extensions/coding-standards/c/module.json +149 -0
- package/augment-extensions/coding-standards/c/rules/categories/drivers.md +635 -0
- package/augment-extensions/coding-standards/c/rules/categories/embedded.md +510 -0
- package/augment-extensions/coding-standards/c/rules/categories/kernel.md +653 -0
- package/augment-extensions/coding-standards/c/rules/categories/legacy.md +526 -0
- package/augment-extensions/coding-standards/c/rules/categories/networking.md +735 -0
- package/augment-extensions/coding-standards/c/rules/categories/realtime.md +631 -0
- package/augment-extensions/coding-standards/c/rules/categories/systems.md +586 -0
- package/augment-extensions/coding-standards/c/rules/universal/const-correctness.md +275 -0
- package/augment-extensions/coding-standards/c/rules/universal/documentation.md +251 -0
- package/augment-extensions/coding-standards/c/rules/universal/error-handling.md +250 -0
- package/augment-extensions/coding-standards/c/rules/universal/header-guards.md +254 -0
- package/augment-extensions/coding-standards/c/rules/universal/memory-safety.md +233 -0
- package/augment-extensions/coding-standards/c/rules/universal/naming.md +146 -0
- package/augment-extensions/coding-standards/c/src/conflict-detector.ts +461 -0
- package/augment-extensions/coding-standards/c/src/prompt-generator.ts +307 -0
- package/augment-extensions/coding-standards/c/src/rule-evaluator.ts +307 -0
- package/augment-extensions/coding-standards/c/src/rule-override.ts +427 -0
- package/augment-extensions/coding-standards/c/src/template-engine.ts +217 -0
- package/augment-extensions/coding-standards/c/templates/prompts/drivers.txt +191 -0
- package/augment-extensions/coding-standards/c/templates/prompts/embedded.txt +164 -0
- package/augment-extensions/coding-standards/c/templates/prompts/kernel.txt +175 -0
- package/augment-extensions/coding-standards/c/templates/prompts/legacy.txt +280 -0
- package/augment-extensions/coding-standards/c/templates/prompts/networking.txt +259 -0
- package/augment-extensions/coding-standards/c/templates/prompts/realtime.txt +219 -0
- package/augment-extensions/coding-standards/c/templates/prompts/systems.txt +147 -0
- package/augment-extensions/coding-standards/c/tests/integration/category-specific.test.ts +356 -0
- package/augment-extensions/coding-standards/c/tests/integration/end-to-end-workflow.test.ts +377 -0
- package/augment-extensions/coding-standards/c/tests/performance/benchmarks.test.ts +407 -0
- package/augment-extensions/coding-standards/c/tests/unit/config-manager.test.ts +345 -0
- package/augment-extensions/coding-standards/c/tests/unit/conflict-detector.test.ts +294 -0
- package/augment-extensions/coding-standards/c/tests/unit/prompt-generator.test.ts +174 -0
- package/augment-extensions/coding-standards/c/tests/unit/registry.test.ts +313 -0
- package/augment-extensions/coding-standards/c/tests/unit/rule-evaluator.test.ts +318 -0
- package/augment-extensions/coding-standards/c/tests/unit/rule-override.test.ts +326 -0
- package/augment-extensions/coding-standards/c/tests/unit/template-engine.test.ts +314 -0
- package/augment-extensions/coding-standards/go/CHARACTER-COUNT-REPORT.md +135 -0
- package/augment-extensions/coding-standards/go/PHASE1-COMPLETION.md +146 -0
- package/augment-extensions/coding-standards/go/PHASE4-COMPLETION.md +184 -0
- package/augment-extensions/coding-standards/go/README.md +200 -0
- package/augment-extensions/coding-standards/go/VALIDATION-CHECKLIST.md +154 -0
- package/augment-extensions/coding-standards/go/config/examples/example-cli.json +15 -0
- package/augment-extensions/coding-standards/go/config/examples/example-microservices.json +21 -0
- package/augment-extensions/coding-standards/go/config/examples/example-multi-category.yaml +24 -0
- package/augment-extensions/coding-standards/go/config/examples/example-web.json +15 -0
- package/augment-extensions/coding-standards/go/config/schema.json +110 -0
- package/augment-extensions/coding-standards/go/docs/CATEGORIES.md +221 -0
- package/augment-extensions/coding-standards/go/docs/CONFIGURATION.md +198 -0
- package/augment-extensions/coding-standards/go/docs/TROUBLESHOOTING.md +285 -0
- package/augment-extensions/coding-standards/go/examples/cli/cobra-app.go +287 -0
- package/augment-extensions/coding-standards/go/examples/cloud-native-app.go +217 -0
- package/augment-extensions/coding-standards/go/examples/devops-tool.go +250 -0
- package/augment-extensions/coding-standards/go/examples/distributed-system.go +247 -0
- package/augment-extensions/coding-standards/go/examples/microservices/grpc-service.go +253 -0
- package/augment-extensions/coding-standards/go/examples/rest-api.go +270 -0
- package/augment-extensions/coding-standards/go/examples/web/http-server.go +224 -0
- package/augment-extensions/coding-standards/go/module.json +139 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/api-versioning.md +149 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/rate-limiting.md +209 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/rest-api-design.md +183 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/cloud-config.md +193 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/health-checks.md +231 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/kubernetes.md +180 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/automation.md +179 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/ci-cd-integration.md +147 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/infrastructure-as-code.md +231 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/caching.md +150 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/consensus.md +187 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/event-sourcing.md +246 -0
- package/augment-extensions/coding-standards/go/rules/cli/command-parsing.md +264 -0
- package/augment-extensions/coding-standards/go/rules/cli/configuration.md +268 -0
- package/augment-extensions/coding-standards/go/rules/cli/cross-platform.md +324 -0
- package/augment-extensions/coding-standards/go/rules/microservices/distributed-tracing.md +253 -0
- package/augment-extensions/coding-standards/go/rules/microservices/grpc.md +257 -0
- package/augment-extensions/coding-standards/go/rules/microservices/metrics.md +278 -0
- package/augment-extensions/coding-standards/go/rules/microservices/service-discovery.md +249 -0
- package/augment-extensions/coding-standards/go/rules/universal/code-organization.md +221 -0
- package/augment-extensions/coding-standards/go/rules/universal/documentation.md +269 -0
- package/augment-extensions/coding-standards/go/rules/universal/performance.md +323 -0
- package/augment-extensions/coding-standards/go/rules/universal/testing.md +162 -0
- package/augment-extensions/coding-standards/go/rules/web/graceful-shutdown.md +249 -0
- package/augment-extensions/coding-standards/go/rules/web/http-handlers.md +164 -0
- package/augment-extensions/coding-standards/go/rules/web/middleware.md +234 -0
- package/augment-extensions/coding-standards/go/rules/web/routing.md +251 -0
- package/augment-extensions/coding-standards/go/templates/prompts/api.md +160 -0
- package/augment-extensions/coding-standards/go/templates/prompts/cli.md +225 -0
- package/augment-extensions/coding-standards/go/templates/prompts/cloud-native.md +121 -0
- package/augment-extensions/coding-standards/go/templates/prompts/devops.md +146 -0
- package/augment-extensions/coding-standards/go/templates/prompts/distributed.md +133 -0
- package/augment-extensions/coding-standards/go/templates/prompts/microservices.md +225 -0
- package/augment-extensions/coding-standards/go/templates/prompts/web.md +181 -0
- package/augment-extensions/coding-standards/go/tests/integration/module-integration.test.ts +164 -0
- package/augment-extensions/coding-standards/go/tests/unit/category-selection.test.ts +147 -0
- package/augment-extensions/coding-standards/go/tests/unit/module-structure.test.ts +154 -0
- package/augment-extensions/coding-standards/go/tests/validate-character-count.ps1 +13 -0
- package/augment-extensions/coding-standards/go/tests/validate-examples.ps1 +148 -0
- package/augment-extensions/coding-standards/go/tests/validate-examples.sh +135 -0
- package/cli/dist/analysis/ast-parser.d.ts +47 -0
- package/cli/dist/analysis/ast-parser.d.ts.map +1 -0
- package/cli/dist/analysis/ast-parser.js +161 -0
- package/cli/dist/analysis/ast-parser.js.map +1 -0
- package/cli/dist/analysis/complexity-analyzer.d.ts +27 -0
- package/cli/dist/analysis/complexity-analyzer.d.ts.map +1 -0
- package/cli/dist/analysis/complexity-analyzer.js +189 -0
- package/cli/dist/analysis/complexity-analyzer.js.map +1 -0
- package/cli/dist/analysis/dependency-analyzer.d.ts +23 -0
- package/cli/dist/analysis/dependency-analyzer.d.ts.map +1 -0
- package/cli/dist/analysis/dependency-analyzer.js +237 -0
- package/cli/dist/analysis/dependency-analyzer.js.map +1 -0
- package/cli/dist/analysis/index.d.ts +9 -0
- package/cli/dist/analysis/index.d.ts.map +1 -0
- package/cli/dist/analysis/index.js +25 -0
- package/cli/dist/analysis/index.js.map +1 -0
- package/cli/dist/analysis/security-scanner.d.ts +11 -0
- package/cli/dist/analysis/security-scanner.d.ts.map +1 -0
- package/cli/dist/analysis/security-scanner.js +294 -0
- package/cli/dist/analysis/security-scanner.js.map +1 -0
- package/cli/dist/analysis/types.d.ts +151 -0
- package/cli/dist/analysis/types.d.ts.map +1 -0
- package/cli/dist/analysis/types.js +6 -0
- package/cli/dist/analysis/types.js.map +1 -0
- package/cli/dist/cli.js +24 -0
- package/cli/dist/cli.js.map +1 -1
- package/cli/dist/commands/code-analysis.d.ts +11 -0
- package/cli/dist/commands/code-analysis.d.ts.map +1 -0
- package/cli/dist/commands/code-analysis.js +412 -0
- package/cli/dist/commands/code-analysis.js.map +1 -0
- package/modules.md +99 -3
- package/package.json +14 -2
- package/cli/dist/commands/agent.d.ts +0 -37
- package/cli/dist/commands/agent.d.ts.map +0 -1
- package/cli/dist/commands/agent.js +0 -222
- package/cli/dist/commands/agent.js.map +0 -1
- package/cli/dist/commands/beads.d.ts +0 -64
- package/cli/dist/commands/beads.d.ts.map +0 -1
- package/cli/dist/commands/beads.js +0 -377
- package/cli/dist/commands/beads.js.map +0 -1
- package/cli/dist/commands/change.d.ts +0 -54
- package/cli/dist/commands/change.d.ts.map +0 -1
- package/cli/dist/commands/change.js +0 -243
- package/cli/dist/commands/change.js.map +0 -1
- package/cli/dist/commands/clean.d.ts +0 -15
- package/cli/dist/commands/clean.d.ts.map +0 -1
- package/cli/dist/commands/clean.js +0 -63
- package/cli/dist/commands/clean.js.map +0 -1
- package/cli/dist/commands/clone.d.ts +0 -15
- package/cli/dist/commands/clone.d.ts.map +0 -1
- package/cli/dist/commands/clone.js +0 -49
- package/cli/dist/commands/clone.js.map +0 -1
- package/cli/dist/commands/config.d.ts +0 -33
- package/cli/dist/commands/config.d.ts.map +0 -1
- package/cli/dist/commands/config.js +0 -166
- package/cli/dist/commands/config.js.map +0 -1
- package/cli/dist/commands/context.d.ts +0 -38
- package/cli/dist/commands/context.d.ts.map +0 -1
- package/cli/dist/commands/context.js +0 -205
- package/cli/dist/commands/context.js.map +0 -1
- package/cli/dist/commands/create.d.ts +0 -18
- package/cli/dist/commands/create.d.ts.map +0 -1
- package/cli/dist/commands/create.js +0 -178
- package/cli/dist/commands/create.js.map +0 -1
- package/cli/dist/commands/diff.d.ts +0 -19
- package/cli/dist/commands/diff.d.ts.map +0 -1
- package/cli/dist/commands/diff.js +0 -104
- package/cli/dist/commands/diff.js.map +0 -1
- package/cli/dist/commands/doctor.d.ts +0 -14
- package/cli/dist/commands/doctor.d.ts.map +0 -1
- package/cli/dist/commands/doctor.js +0 -62
- package/cli/dist/commands/doctor.js.map +0 -1
- package/cli/dist/commands/export.d.ts +0 -28
- package/cli/dist/commands/export.d.ts.map +0 -1
- package/cli/dist/commands/export.js +0 -135
- package/cli/dist/commands/export.js.map +0 -1
- package/cli/dist/commands/import.d.ts +0 -23
- package/cli/dist/commands/import.d.ts.map +0 -1
- package/cli/dist/commands/import.js +0 -118
- package/cli/dist/commands/import.js.map +0 -1
- package/cli/dist/commands/prompt.d.ts +0 -45
- package/cli/dist/commands/prompt.d.ts.map +0 -1
- package/cli/dist/commands/prompt.js +0 -223
- package/cli/dist/commands/prompt.js.map +0 -1
- package/cli/dist/commands/spec.d.ts +0 -57
- package/cli/dist/commands/spec.d.ts.map +0 -1
- package/cli/dist/commands/spec.js +0 -279
- package/cli/dist/commands/spec.js.map +0 -1
- package/cli/dist/commands/stats.d.ts +0 -18
- package/cli/dist/commands/stats.d.ts.map +0 -1
- package/cli/dist/commands/stats.js +0 -85
- package/cli/dist/commands/stats.js.map +0 -1
- package/cli/dist/commands/task.d.ts +0 -65
- package/cli/dist/commands/task.d.ts.map +0 -1
- package/cli/dist/commands/task.js +0 -282
- package/cli/dist/commands/task.js.map +0 -1
- package/cli/dist/commands/template.d.ts +0 -17
- package/cli/dist/commands/template.d.ts.map +0 -1
- package/cli/dist/commands/template.js +0 -55
- package/cli/dist/commands/template.js.map +0 -1
- package/cli/dist/utils/agent-config.d.ts +0 -129
- package/cli/dist/utils/agent-config.d.ts.map +0 -1
- package/cli/dist/utils/agent-config.js +0 -297
- package/cli/dist/utils/agent-config.js.map +0 -1
- package/cli/dist/utils/beads-graph.d.ts +0 -17
- package/cli/dist/utils/beads-graph.d.ts.map +0 -1
- package/cli/dist/utils/beads-graph.js +0 -150
- package/cli/dist/utils/beads-graph.js.map +0 -1
- package/cli/dist/utils/beads-integration.d.ts +0 -112
- package/cli/dist/utils/beads-integration.d.ts.map +0 -1
- package/cli/dist/utils/beads-integration.js +0 -312
- package/cli/dist/utils/beads-integration.js.map +0 -1
- package/cli/dist/utils/beads-reporter.d.ts +0 -17
- package/cli/dist/utils/beads-reporter.d.ts.map +0 -1
- package/cli/dist/utils/beads-reporter.js +0 -160
- package/cli/dist/utils/beads-reporter.js.map +0 -1
- package/cli/dist/utils/cache-manager.d.ts +0 -55
- package/cli/dist/utils/cache-manager.d.ts.map +0 -1
- package/cli/dist/utils/cache-manager.js +0 -150
- package/cli/dist/utils/cache-manager.js.map +0 -1
- package/cli/dist/utils/change-manager.d.ts +0 -70
- package/cli/dist/utils/change-manager.d.ts.map +0 -1
- package/cli/dist/utils/change-manager.js +0 -412
- package/cli/dist/utils/change-manager.js.map +0 -1
- package/cli/dist/utils/config-manager-enhanced.d.ts +0 -66
- package/cli/dist/utils/config-manager-enhanced.d.ts.map +0 -1
- package/cli/dist/utils/config-manager-enhanced.js +0 -77
- package/cli/dist/utils/config-manager-enhanced.js.map +0 -1
- package/cli/dist/utils/context-manager.d.ts +0 -96
- package/cli/dist/utils/context-manager.d.ts.map +0 -1
- package/cli/dist/utils/context-manager.js +0 -258
- package/cli/dist/utils/context-manager.js.map +0 -1
- package/cli/dist/utils/diff-engine.d.ts +0 -78
- package/cli/dist/utils/diff-engine.d.ts.map +0 -1
- package/cli/dist/utils/diff-engine.js +0 -233
- package/cli/dist/utils/diff-engine.js.map +0 -1
- package/cli/dist/utils/export-system.d.ts +0 -101
- package/cli/dist/utils/export-system.d.ts.map +0 -1
- package/cli/dist/utils/export-system.js +0 -289
- package/cli/dist/utils/export-system.js.map +0 -1
- package/cli/dist/utils/health-checker.d.ts +0 -66
- package/cli/dist/utils/health-checker.d.ts.map +0 -1
- package/cli/dist/utils/health-checker.js +0 -285
- package/cli/dist/utils/health-checker.js.map +0 -1
- package/cli/dist/utils/import-system.d.ts +0 -74
- package/cli/dist/utils/import-system.d.ts.map +0 -1
- package/cli/dist/utils/import-system.js +0 -317
- package/cli/dist/utils/import-system.js.map +0 -1
- package/cli/dist/utils/module-cloner.d.ts +0 -40
- package/cli/dist/utils/module-cloner.d.ts.map +0 -1
- package/cli/dist/utils/module-cloner.js +0 -136
- package/cli/dist/utils/module-cloner.js.map +0 -1
- package/cli/dist/utils/prompt-manager.d.ts +0 -90
- package/cli/dist/utils/prompt-manager.d.ts.map +0 -1
- package/cli/dist/utils/prompt-manager.js +0 -302
- package/cli/dist/utils/prompt-manager.js.map +0 -1
- package/cli/dist/utils/spec-manager.d.ts +0 -65
- package/cli/dist/utils/spec-manager.d.ts.map +0 -1
- package/cli/dist/utils/spec-manager.js +0 -329
- package/cli/dist/utils/spec-manager.js.map +0 -1
- package/cli/dist/utils/stats-collector.d.ts +0 -74
- package/cli/dist/utils/stats-collector.d.ts.map +0 -1
- package/cli/dist/utils/stats-collector.js +0 -164
- package/cli/dist/utils/stats-collector.js.map +0 -1
- package/cli/dist/utils/template-engine.d.ts +0 -47
- package/cli/dist/utils/template-engine.d.ts.map +0 -1
- package/cli/dist/utils/template-engine.js +0 -204
- package/cli/dist/utils/template-engine.js.map +0 -1
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
# Rule: Error Handling
|
|
2
|
+
|
|
3
|
+
## Metadata
|
|
4
|
+
- **ID**: universal-error-handling
|
|
5
|
+
- **Category**: universal
|
|
6
|
+
- **Severity**: ERROR
|
|
7
|
+
- **Standard**: POSIX, CERT C
|
|
8
|
+
- **Version**: 1.0.0
|
|
9
|
+
|
|
10
|
+
## Description
|
|
11
|
+
Always check return values from functions, use errno appropriately, implement proper error propagation patterns, and ensure cleanup on error paths.
|
|
12
|
+
|
|
13
|
+
## Rationale
|
|
14
|
+
Unchecked errors lead to undefined behavior, security vulnerabilities, and difficult-to-debug issues. Proper error handling makes code robust, maintainable, and secure.
|
|
15
|
+
|
|
16
|
+
## Applies To
|
|
17
|
+
- C Standards: c89, c99, c11, c17, c23
|
|
18
|
+
- Categories: all
|
|
19
|
+
|
|
20
|
+
## Rule Details
|
|
21
|
+
|
|
22
|
+
### 1. Return Value Checking
|
|
23
|
+
- Check return values from all functions that can fail
|
|
24
|
+
- Never ignore return values (use `(void)` cast if intentional)
|
|
25
|
+
- System calls, library functions, and custom functions
|
|
26
|
+
|
|
27
|
+
### 2. errno Usage
|
|
28
|
+
- Check `errno` after functions that set it
|
|
29
|
+
- Save and restore `errno` in signal handlers
|
|
30
|
+
- Clear `errno` before operations that may set it
|
|
31
|
+
|
|
32
|
+
### 3. Error Propagation
|
|
33
|
+
- Propagate errors up the call stack
|
|
34
|
+
- Use consistent error codes or return values
|
|
35
|
+
- Provide context with error messages
|
|
36
|
+
|
|
37
|
+
### 4. Cleanup on Error Paths
|
|
38
|
+
- Free allocated resources on all error paths
|
|
39
|
+
- Use goto for centralized cleanup
|
|
40
|
+
- Ensure consistent state on error
|
|
41
|
+
|
|
42
|
+
## Examples
|
|
43
|
+
|
|
44
|
+
### ✅ Good Example 1: System Call Error Checking
|
|
45
|
+
|
|
46
|
+
```c
|
|
47
|
+
#include <stdio.h>
|
|
48
|
+
#include <errno.h>
|
|
49
|
+
#include <string.h>
|
|
50
|
+
|
|
51
|
+
int read_file(const char* filename, char* buffer, size_t size) {
|
|
52
|
+
FILE* file = fopen(filename, "r");
|
|
53
|
+
if (file == NULL) {
|
|
54
|
+
fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
|
|
55
|
+
return -1;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
size_t bytes_read = fread(buffer, 1, size - 1, file);
|
|
59
|
+
if (ferror(file)) {
|
|
60
|
+
fprintf(stderr, "Error reading %s: %s\n", filename, strerror(errno));
|
|
61
|
+
fclose(file);
|
|
62
|
+
return -1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
buffer[bytes_read] = '\0';
|
|
66
|
+
fclose(file);
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### ✅ Good Example 2: Error Propagation with Cleanup
|
|
72
|
+
|
|
73
|
+
```c
|
|
74
|
+
#include <stdlib.h>
|
|
75
|
+
#include <string.h>
|
|
76
|
+
|
|
77
|
+
typedef struct {
|
|
78
|
+
char* name;
|
|
79
|
+
int* data;
|
|
80
|
+
} Resource;
|
|
81
|
+
|
|
82
|
+
int initialize_resource(Resource* res, const char* name, size_t data_size) {
|
|
83
|
+
int result = 0;
|
|
84
|
+
|
|
85
|
+
if (res == NULL || name == NULL) {
|
|
86
|
+
return -1;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
res->name = strdup(name);
|
|
90
|
+
if (res->name == NULL) {
|
|
91
|
+
result = -1;
|
|
92
|
+
goto cleanup;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
res->data = malloc(data_size * sizeof(int));
|
|
96
|
+
if (res->data == NULL) {
|
|
97
|
+
result = -1;
|
|
98
|
+
goto cleanup;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
memset(res->data, 0, data_size * sizeof(int));
|
|
102
|
+
return 0;
|
|
103
|
+
|
|
104
|
+
cleanup:
|
|
105
|
+
if (res->name != NULL) {
|
|
106
|
+
free(res->name);
|
|
107
|
+
res->name = NULL;
|
|
108
|
+
}
|
|
109
|
+
if (res->data != NULL) {
|
|
110
|
+
free(res->data);
|
|
111
|
+
res->data = NULL;
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### ✅ Good Example 3: errno Handling
|
|
118
|
+
|
|
119
|
+
```c
|
|
120
|
+
#include <errno.h>
|
|
121
|
+
#include <unistd.h>
|
|
122
|
+
#include <stdio.h>
|
|
123
|
+
|
|
124
|
+
ssize_t safe_write(int fd, const void* buf, size_t count) {
|
|
125
|
+
ssize_t written = 0;
|
|
126
|
+
ssize_t result;
|
|
127
|
+
|
|
128
|
+
while (written < count) {
|
|
129
|
+
errno = 0; // Clear before operation
|
|
130
|
+
result = write(fd, (char*)buf + written, count - written);
|
|
131
|
+
|
|
132
|
+
if (result < 0) {
|
|
133
|
+
if (errno == EINTR) {
|
|
134
|
+
continue; // Interrupted, retry
|
|
135
|
+
}
|
|
136
|
+
perror("write failed");
|
|
137
|
+
return -1;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
written += result;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return written;
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### ✅ Good Example 4: Multiple Error Paths
|
|
148
|
+
|
|
149
|
+
```c
|
|
150
|
+
int process_data(const char* input_file, const char* output_file) {
|
|
151
|
+
FILE* in = NULL;
|
|
152
|
+
FILE* out = NULL;
|
|
153
|
+
char* buffer = NULL;
|
|
154
|
+
int result = -1;
|
|
155
|
+
|
|
156
|
+
in = fopen(input_file, "r");
|
|
157
|
+
if (in == NULL) {
|
|
158
|
+
fprintf(stderr, "Cannot open input: %s\n", strerror(errno));
|
|
159
|
+
goto cleanup;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
out = fopen(output_file, "w");
|
|
163
|
+
if (out == NULL) {
|
|
164
|
+
fprintf(stderr, "Cannot open output: %s\n", strerror(errno));
|
|
165
|
+
goto cleanup;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
buffer = malloc(4096);
|
|
169
|
+
if (buffer == NULL) {
|
|
170
|
+
fprintf(stderr, "Memory allocation failed\n");
|
|
171
|
+
goto cleanup;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Process data...
|
|
175
|
+
result = 0; // Success
|
|
176
|
+
|
|
177
|
+
cleanup:
|
|
178
|
+
if (buffer != NULL) free(buffer);
|
|
179
|
+
if (out != NULL) fclose(out);
|
|
180
|
+
if (in != NULL) fclose(in);
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### ❌ Bad Example 1: Ignoring Return Values
|
|
186
|
+
|
|
187
|
+
```c
|
|
188
|
+
void bad_file_handling(const char* filename) {
|
|
189
|
+
FILE* file = fopen(filename, "r");
|
|
190
|
+
// No null check!
|
|
191
|
+
|
|
192
|
+
char buffer[100];
|
|
193
|
+
fread(buffer, 1, 100, file); // Ignoring return value
|
|
194
|
+
// No error checking!
|
|
195
|
+
|
|
196
|
+
fclose(file);
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### ❌ Bad Example 2: No Error Propagation
|
|
201
|
+
|
|
202
|
+
```c
|
|
203
|
+
int bad_allocation(void) {
|
|
204
|
+
char* data = malloc(1024);
|
|
205
|
+
// No null check, no error return
|
|
206
|
+
strcpy(data, "test"); // Crash if malloc failed
|
|
207
|
+
return 0; // Always returns success!
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### ❌ Bad Example 3: Missing Cleanup
|
|
212
|
+
|
|
213
|
+
```c
|
|
214
|
+
int bad_cleanup(const char* file) {
|
|
215
|
+
char* buffer = malloc(1024);
|
|
216
|
+
FILE* f = fopen(file, "r");
|
|
217
|
+
|
|
218
|
+
if (f == NULL) {
|
|
219
|
+
return -1; // Memory leak! buffer not freed
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// ... use buffer and file ...
|
|
223
|
+
|
|
224
|
+
fclose(f);
|
|
225
|
+
free(buffer);
|
|
226
|
+
return 0;
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Error Code Conventions
|
|
231
|
+
|
|
232
|
+
```c
|
|
233
|
+
// Use negative values for errors, 0 for success
|
|
234
|
+
#define SUCCESS 0
|
|
235
|
+
#define ERR_INVALID_PARAM -1
|
|
236
|
+
#define ERR_NO_MEMORY -2
|
|
237
|
+
#define ERR_IO_ERROR -3
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## References
|
|
241
|
+
|
|
242
|
+
- [CERT C - ERR33-C](https://wiki.sei.cmu.edu/confluence/display/c/ERR33-C.+Detect+and+handle+standard+library+errors)
|
|
243
|
+
- [POSIX Error Handling](https://pubs.opengroup.org/onlinepubs/9699919799/)
|
|
244
|
+
- [Linux errno man page](https://man7.org/linux/man-pages/man3/errno.3.html)
|
|
245
|
+
|
|
246
|
+
## Related Rules
|
|
247
|
+
|
|
248
|
+
- `universal-memory-safety` - Error handling for allocations
|
|
249
|
+
- `category-systems` - POSIX error handling patterns
|
|
250
|
+
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# Rule: Header Guards
|
|
2
|
+
|
|
3
|
+
## Metadata
|
|
4
|
+
- **ID**: universal-header-guards
|
|
5
|
+
- **Category**: universal
|
|
6
|
+
- **Severity**: ERROR
|
|
7
|
+
- **Standard**: C Standards, C++ Standards
|
|
8
|
+
- **Version**: 1.0.0
|
|
9
|
+
|
|
10
|
+
## Description
|
|
11
|
+
Protect header files from multiple inclusion using `#ifndef/#define/#endif` pattern or `#pragma once` directive.
|
|
12
|
+
|
|
13
|
+
## Rationale
|
|
14
|
+
Header guards prevent multiple inclusion of the same header file, which can cause:
|
|
15
|
+
- Redefinition errors
|
|
16
|
+
- Increased compilation time
|
|
17
|
+
- Potential ODR (One Definition Rule) violations
|
|
18
|
+
- Unexpected behavior from duplicate definitions
|
|
19
|
+
|
|
20
|
+
## Applies To
|
|
21
|
+
- C Standards: c89, c99, c11, c17, c23
|
|
22
|
+
- Categories: all
|
|
23
|
+
|
|
24
|
+
## Rule Details
|
|
25
|
+
|
|
26
|
+
### 1. Traditional Header Guards (#ifndef/#define/#endif)
|
|
27
|
+
- Use unique guard names based on file path
|
|
28
|
+
- Format: `PROJECT_PATH_FILENAME_H`
|
|
29
|
+
- Place at the very beginning and end of header
|
|
30
|
+
- Include all content between guards
|
|
31
|
+
|
|
32
|
+
### 2. Pragma Once (Alternative)
|
|
33
|
+
- Modern compilers support `#pragma once`
|
|
34
|
+
- Simpler syntax, less error-prone
|
|
35
|
+
- Not part of C standard but widely supported
|
|
36
|
+
- Faster compilation in some cases
|
|
37
|
+
|
|
38
|
+
### 3. Guard Naming Convention
|
|
39
|
+
- Use uppercase letters
|
|
40
|
+
- Include project/module name to ensure uniqueness
|
|
41
|
+
- Replace path separators and dots with underscores
|
|
42
|
+
- Add trailing `_H` or `_INCLUDED`
|
|
43
|
+
|
|
44
|
+
## Examples
|
|
45
|
+
|
|
46
|
+
### ✅ Good Example 1: Traditional Header Guard
|
|
47
|
+
|
|
48
|
+
```c
|
|
49
|
+
/**
|
|
50
|
+
* @file network_utils.h
|
|
51
|
+
* @brief Network utility functions
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
#ifndef MYPROJECT_NETWORK_UTILS_H
|
|
55
|
+
#define MYPROJECT_NETWORK_UTILS_H
|
|
56
|
+
|
|
57
|
+
#include <stdint.h>
|
|
58
|
+
#include <stddef.h>
|
|
59
|
+
|
|
60
|
+
// Function declarations
|
|
61
|
+
int connect_to_server(const char* host, uint16_t port);
|
|
62
|
+
void disconnect_from_server(int socket_fd);
|
|
63
|
+
ssize_t send_data(int socket_fd, const void* data, size_t len);
|
|
64
|
+
|
|
65
|
+
#endif /* MYPROJECT_NETWORK_UTILS_H */
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### ✅ Good Example 2: Pragma Once
|
|
69
|
+
|
|
70
|
+
```c
|
|
71
|
+
/**
|
|
72
|
+
* @file config.h
|
|
73
|
+
* @brief Configuration constants
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
#pragma once
|
|
77
|
+
|
|
78
|
+
#define MAX_CONNECTIONS 100
|
|
79
|
+
#define DEFAULT_PORT 8080
|
|
80
|
+
#define BUFFER_SIZE 4096
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### ✅ Good Example 3: Nested Path Header Guard
|
|
84
|
+
|
|
85
|
+
```c
|
|
86
|
+
/**
|
|
87
|
+
* @file src/core/memory/allocator.h
|
|
88
|
+
* @brief Custom memory allocator
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
#ifndef MYPROJECT_SRC_CORE_MEMORY_ALLOCATOR_H
|
|
92
|
+
#define MYPROJECT_SRC_CORE_MEMORY_ALLOCATOR_H
|
|
93
|
+
|
|
94
|
+
typedef struct allocator Allocator;
|
|
95
|
+
|
|
96
|
+
Allocator* allocator_create(size_t pool_size);
|
|
97
|
+
void allocator_destroy(Allocator* alloc);
|
|
98
|
+
void* allocator_alloc(Allocator* alloc, size_t size);
|
|
99
|
+
void allocator_free(Allocator* alloc, void* ptr);
|
|
100
|
+
|
|
101
|
+
#endif /* MYPROJECT_SRC_CORE_MEMORY_ALLOCATOR_H */
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### ✅ Good Example 4: Header with External C Linkage
|
|
105
|
+
|
|
106
|
+
```c
|
|
107
|
+
/**
|
|
108
|
+
* @file api.h
|
|
109
|
+
* @brief Public API for C++ compatibility
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
#ifndef MYPROJECT_API_H
|
|
113
|
+
#define MYPROJECT_API_H
|
|
114
|
+
|
|
115
|
+
#ifdef __cplusplus
|
|
116
|
+
extern "C" {
|
|
117
|
+
#endif
|
|
118
|
+
|
|
119
|
+
// API functions
|
|
120
|
+
int initialize_library(void);
|
|
121
|
+
void shutdown_library(void);
|
|
122
|
+
|
|
123
|
+
#ifdef __cplusplus
|
|
124
|
+
}
|
|
125
|
+
#endif
|
|
126
|
+
|
|
127
|
+
#endif /* MYPROJECT_API_H */
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### ❌ Bad Example 1: Missing Header Guard
|
|
131
|
+
|
|
132
|
+
```c
|
|
133
|
+
/**
|
|
134
|
+
* @file utils.h
|
|
135
|
+
*/
|
|
136
|
+
|
|
137
|
+
// No header guard!
|
|
138
|
+
|
|
139
|
+
void utility_function(void);
|
|
140
|
+
int helper_function(int x);
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### ❌ Bad Example 2: Incomplete Header Guard
|
|
144
|
+
|
|
145
|
+
```c
|
|
146
|
+
/**
|
|
147
|
+
* @file data.h
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
#ifndef DATA_H
|
|
151
|
+
#define DATA_H
|
|
152
|
+
|
|
153
|
+
struct Data {
|
|
154
|
+
int value;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// Missing #endif - will cause errors!
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### ❌ Bad Example 3: Non-Unique Guard Name
|
|
161
|
+
|
|
162
|
+
```c
|
|
163
|
+
/**
|
|
164
|
+
* @file src/module_a/config.h
|
|
165
|
+
*/
|
|
166
|
+
|
|
167
|
+
#ifndef CONFIG_H // Too generic, may conflict!
|
|
168
|
+
#define CONFIG_H
|
|
169
|
+
|
|
170
|
+
#define MAX_SIZE 100
|
|
171
|
+
|
|
172
|
+
#endif
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### ❌ Bad Example 4: Guard Doesn't Cover All Content
|
|
176
|
+
|
|
177
|
+
```c
|
|
178
|
+
#include <stdio.h> // Outside guard - will be included multiple times!
|
|
179
|
+
|
|
180
|
+
#ifndef UTILS_H
|
|
181
|
+
#define UTILS_H
|
|
182
|
+
|
|
183
|
+
void print_message(const char* msg);
|
|
184
|
+
|
|
185
|
+
#endif
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Guard Naming Examples
|
|
189
|
+
|
|
190
|
+
| File Path | Guard Name |
|
|
191
|
+
|-----------|------------|
|
|
192
|
+
| `utils.h` | `MYPROJECT_UTILS_H` |
|
|
193
|
+
| `src/core/memory.h` | `MYPROJECT_SRC_CORE_MEMORY_H` |
|
|
194
|
+
| `include/api/v2/client.h` | `MYPROJECT_INCLUDE_API_V2_CLIENT_H` |
|
|
195
|
+
| `lib/network/tcp.h` | `MYPROJECT_LIB_NETWORK_TCP_H` |
|
|
196
|
+
|
|
197
|
+
## Choosing Between #ifndef and #pragma once
|
|
198
|
+
|
|
199
|
+
### Use #ifndef/#define/#endif when:
|
|
200
|
+
- Maximum portability is required
|
|
201
|
+
- Working with very old compilers
|
|
202
|
+
- Project coding standards mandate it
|
|
203
|
+
- Building for embedded systems with limited toolchains
|
|
204
|
+
|
|
205
|
+
### Use #pragma once when:
|
|
206
|
+
- Working with modern compilers (GCC, Clang, MSVC)
|
|
207
|
+
- Simplicity and readability are priorities
|
|
208
|
+
- Faster compilation is desired
|
|
209
|
+
- Team/project standards allow it
|
|
210
|
+
|
|
211
|
+
## Common Mistakes
|
|
212
|
+
|
|
213
|
+
1. **Forgetting the closing #endif**
|
|
214
|
+
- Causes compilation errors
|
|
215
|
+
- Use editor features to match directives
|
|
216
|
+
|
|
217
|
+
2. **Using non-unique guard names**
|
|
218
|
+
- Can cause silent bugs
|
|
219
|
+
- Always include project/module prefix
|
|
220
|
+
|
|
221
|
+
3. **Placing includes before guard**
|
|
222
|
+
- Defeats the purpose of guards
|
|
223
|
+
- Guard should be first non-comment line
|
|
224
|
+
|
|
225
|
+
4. **Typos in guard names**
|
|
226
|
+
- #ifndef and #define must match exactly
|
|
227
|
+
- Use copy-paste to avoid errors
|
|
228
|
+
|
|
229
|
+
## Automation
|
|
230
|
+
|
|
231
|
+
### Generate guard name from filename (bash):
|
|
232
|
+
```bash
|
|
233
|
+
#!/bin/bash
|
|
234
|
+
filename="$1"
|
|
235
|
+
guard=$(echo "$filename" | tr '[:lower:]' '[:upper:]' | tr './' '__')
|
|
236
|
+
echo "#ifndef ${guard}"
|
|
237
|
+
echo "#define ${guard}"
|
|
238
|
+
echo ""
|
|
239
|
+
echo "// Content here"
|
|
240
|
+
echo ""
|
|
241
|
+
echo "#endif /* ${guard} */"
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## References
|
|
245
|
+
|
|
246
|
+
- [C Standard - 6.10 Preprocessing directives](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)
|
|
247
|
+
- [GCC Documentation - Once-Only Headers](https://gcc.gnu.org/onlinedocs/cpp/Once-Only-Headers.html)
|
|
248
|
+
- [Google C++ Style Guide - Header Guards](https://google.github.io/styleguide/cppguide.html#The__define_Guard)
|
|
249
|
+
|
|
250
|
+
## Related Rules
|
|
251
|
+
|
|
252
|
+
- `universal-naming` - Guard naming conventions
|
|
253
|
+
- `universal-documentation` - File header documentation
|
|
254
|
+
|