@hivehub/rulebook 2.0.0 → 3.0.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 (188) hide show
  1. package/README.md +779 -654
  2. package/dist/cli/commands.d.ts +22 -0
  3. package/dist/cli/commands.d.ts.map +1 -1
  4. package/dist/cli/commands.js +291 -8
  5. package/dist/cli/commands.js.map +1 -1
  6. package/dist/core/claude-mcp.d.ts +32 -0
  7. package/dist/core/claude-mcp.d.ts.map +1 -0
  8. package/dist/core/claude-mcp.js +92 -0
  9. package/dist/core/claude-mcp.js.map +1 -0
  10. package/dist/core/config-manager.d.ts.map +1 -1
  11. package/dist/core/config-manager.js +27 -6
  12. package/dist/core/config-manager.js.map +1 -1
  13. package/dist/core/generator.d.ts.map +1 -1
  14. package/dist/core/generator.js +98 -49
  15. package/dist/core/generator.js.map +1 -1
  16. package/dist/core/migrator.d.ts +13 -0
  17. package/dist/core/migrator.d.ts.map +1 -1
  18. package/dist/core/migrator.js +76 -9
  19. package/dist/core/migrator.js.map +1 -1
  20. package/dist/core/openspec-migrator.d.ts +1 -1
  21. package/dist/core/openspec-migrator.d.ts.map +1 -1
  22. package/dist/core/openspec-migrator.js +14 -7
  23. package/dist/core/openspec-migrator.js.map +1 -1
  24. package/dist/core/workflow-generator.js +297 -176
  25. package/dist/core/workflow-generator.js.map +1 -1
  26. package/dist/index.js +40 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/mcp/rulebook-server.d.ts.map +1 -1
  29. package/dist/mcp/rulebook-server.js +255 -74
  30. package/dist/mcp/rulebook-server.js.map +1 -1
  31. package/dist/memory/hnsw-index.d.ts +63 -0
  32. package/dist/memory/hnsw-index.d.ts.map +1 -0
  33. package/dist/memory/hnsw-index.js +421 -0
  34. package/dist/memory/hnsw-index.js.map +1 -0
  35. package/dist/memory/memory-cache.d.ts +33 -0
  36. package/dist/memory/memory-cache.d.ts.map +1 -0
  37. package/dist/memory/memory-cache.js +85 -0
  38. package/dist/memory/memory-cache.js.map +1 -0
  39. package/dist/memory/memory-hooks.d.ts +42 -0
  40. package/dist/memory/memory-hooks.d.ts.map +1 -0
  41. package/dist/memory/memory-hooks.js +193 -0
  42. package/dist/memory/memory-hooks.js.map +1 -0
  43. package/dist/memory/memory-manager.d.ts +55 -0
  44. package/dist/memory/memory-manager.d.ts.map +1 -0
  45. package/dist/memory/memory-manager.js +209 -0
  46. package/dist/memory/memory-manager.js.map +1 -0
  47. package/dist/memory/memory-search.d.ts +42 -0
  48. package/dist/memory/memory-search.d.ts.map +1 -0
  49. package/dist/memory/memory-search.js +166 -0
  50. package/dist/memory/memory-search.js.map +1 -0
  51. package/dist/memory/memory-store.d.ts +59 -0
  52. package/dist/memory/memory-store.d.ts.map +1 -0
  53. package/dist/memory/memory-store.js +394 -0
  54. package/dist/memory/memory-store.js.map +1 -0
  55. package/dist/memory/memory-types.d.ts +69 -0
  56. package/dist/memory/memory-types.d.ts.map +1 -0
  57. package/dist/memory/memory-types.js +7 -0
  58. package/dist/memory/memory-types.js.map +1 -0
  59. package/dist/memory/memory-vectorizer.d.ts +29 -0
  60. package/dist/memory/memory-vectorizer.d.ts.map +1 -0
  61. package/dist/memory/memory-vectorizer.js +104 -0
  62. package/dist/memory/memory-vectorizer.js.map +1 -0
  63. package/dist/types.d.ts +7 -0
  64. package/dist/types.d.ts.map +1 -1
  65. package/package.json +107 -106
  66. package/templates/cli/CLAUDE_CODE.md +114 -13
  67. package/templates/commands/rulebook-memory-save.md +48 -0
  68. package/templates/commands/rulebook-memory-search.md +47 -0
  69. package/templates/commands/rulebook-task-apply.md +2 -2
  70. package/templates/commands/rulebook-task-archive.md +2 -2
  71. package/templates/commands/rulebook-task-create.md +2 -2
  72. package/templates/commands/rulebook-task-list.md +2 -2
  73. package/templates/commands/rulebook-task-show.md +2 -2
  74. package/templates/commands/rulebook-task-validate.md +2 -2
  75. package/templates/git/CI_CD_PATTERNS.md +4 -4
  76. package/templates/git/GITHUB_ACTIONS.md +3 -3
  77. package/templates/git/GITLAB_CI.md +4 -4
  78. package/templates/git/SECRETS_MANAGEMENT.md +4 -4
  79. package/templates/hooks/COMMIT_MSG.md +4 -4
  80. package/templates/hooks/POST_CHECKOUT.md +3 -3
  81. package/templates/hooks/PREPARE_COMMIT_MSG.md +3 -3
  82. package/templates/hooks/PRE_COMMIT.md +4 -4
  83. package/templates/hooks/PRE_PUSH.md +4 -4
  84. package/templates/modules/MEMORY.md +63 -0
  85. package/templates/skills/cli/aider/SKILL.md +59 -0
  86. package/templates/skills/cli/amazon-q/SKILL.md +35 -0
  87. package/templates/skills/cli/auggie/SKILL.md +42 -0
  88. package/templates/skills/cli/claude/SKILL.md +42 -0
  89. package/templates/skills/cli/claude-code/SKILL.md +146 -0
  90. package/templates/skills/cli/cline/SKILL.md +42 -0
  91. package/templates/skills/cli/codebuddy/SKILL.md +30 -0
  92. package/templates/skills/cli/codeium/SKILL.md +30 -0
  93. package/templates/skills/cli/codex/SKILL.md +31 -0
  94. package/templates/skills/cli/continue/SKILL.md +44 -0
  95. package/templates/skills/cli/cursor-cli/SKILL.md +38 -0
  96. package/templates/skills/cli/factory/SKILL.md +28 -0
  97. package/templates/skills/cli/gemini/SKILL.md +45 -0
  98. package/templates/skills/cli/kilocode/SKILL.md +28 -0
  99. package/templates/skills/cli/opencode/SKILL.md +28 -0
  100. package/templates/skills/core/agent-automation/SKILL.md +194 -0
  101. package/templates/skills/core/dag/SKILL.md +314 -0
  102. package/templates/skills/core/documentation-rules/SKILL.md +47 -0
  103. package/templates/skills/core/quality-enforcement/SKILL.md +78 -0
  104. package/templates/skills/frameworks/angular/SKILL.md +46 -0
  105. package/templates/skills/frameworks/django/SKILL.md +93 -0
  106. package/templates/skills/frameworks/electron/SKILL.md +157 -0
  107. package/templates/skills/frameworks/flask/SKILL.md +48 -0
  108. package/templates/skills/frameworks/flutter/SKILL.md +65 -0
  109. package/templates/skills/frameworks/jquery/SKILL.md +42 -0
  110. package/templates/skills/frameworks/laravel/SKILL.md +48 -0
  111. package/templates/skills/frameworks/nestjs/SKILL.md +53 -0
  112. package/templates/skills/frameworks/nextjs/SKILL.md +137 -0
  113. package/templates/skills/frameworks/nuxt/SKILL.md +50 -0
  114. package/templates/skills/frameworks/rails/SKILL.md +76 -0
  115. package/templates/skills/frameworks/react/SKILL.md +48 -0
  116. package/templates/skills/frameworks/react-native/SKILL.md +57 -0
  117. package/templates/skills/frameworks/spring/SKILL.md +49 -0
  118. package/templates/skills/frameworks/symfony/SKILL.md +46 -0
  119. package/templates/skills/frameworks/vue/SKILL.md +46 -0
  120. package/templates/skills/frameworks/zend/SKILL.md +45 -0
  121. package/templates/skills/ides/copilot/SKILL.md +47 -0
  122. package/templates/skills/ides/cursor/SKILL.md +53 -0
  123. package/templates/skills/ides/jetbrains-ai/SKILL.md +45 -0
  124. package/templates/skills/ides/replit/SKILL.md +46 -0
  125. package/templates/skills/ides/tabnine/SKILL.md +39 -0
  126. package/templates/skills/ides/vscode/SKILL.md +50 -0
  127. package/templates/skills/ides/windsurf/SKILL.md +46 -0
  128. package/templates/skills/ides/zed/SKILL.md +42 -0
  129. package/templates/skills/languages/ada/SKILL.md +68 -0
  130. package/templates/skills/languages/c/SKILL.md +343 -0
  131. package/templates/skills/languages/cpp/SKILL.md +753 -0
  132. package/templates/skills/languages/csharp/SKILL.md +427 -0
  133. package/templates/skills/languages/dart/SKILL.md +342 -0
  134. package/templates/skills/languages/elixir/SKILL.md +464 -0
  135. package/templates/skills/languages/erlang/SKILL.md +371 -0
  136. package/templates/skills/languages/go/SKILL.md +655 -0
  137. package/templates/skills/languages/haskell/SKILL.md +187 -0
  138. package/templates/skills/languages/java/SKILL.md +617 -0
  139. package/templates/skills/languages/javascript/SKILL.md +641 -0
  140. package/templates/skills/languages/julia/SKILL.md +107 -0
  141. package/templates/skills/languages/kotlin/SKILL.md +521 -0
  142. package/templates/skills/languages/lisp/SKILL.md +110 -0
  143. package/templates/skills/languages/lua/SKILL.md +84 -0
  144. package/templates/skills/languages/objectivec/SKILL.md +100 -0
  145. package/templates/skills/languages/php/SKILL.md +426 -0
  146. package/templates/skills/languages/python/SKILL.md +692 -0
  147. package/templates/skills/languages/r/SKILL.md +360 -0
  148. package/templates/skills/languages/ruby/SKILL.md +431 -0
  149. package/templates/skills/languages/rust/SKILL.md +487 -0
  150. package/templates/skills/languages/sas/SKILL.md +83 -0
  151. package/templates/skills/languages/scala/SKILL.md +358 -0
  152. package/templates/skills/languages/solidity/SKILL.md +590 -0
  153. package/templates/skills/languages/sql/SKILL.md +147 -0
  154. package/templates/skills/languages/swift/SKILL.md +476 -0
  155. package/templates/skills/languages/zig/SKILL.md +275 -0
  156. package/templates/skills/modules/atlassian/SKILL.md +265 -0
  157. package/templates/skills/modules/context7/SKILL.md +64 -0
  158. package/templates/skills/modules/figma/SKILL.md +277 -0
  159. package/templates/skills/modules/github-mcp/SKILL.md +74 -0
  160. package/templates/skills/modules/grafana/SKILL.md +338 -0
  161. package/templates/skills/modules/memory/SKILL.md +73 -0
  162. package/templates/skills/modules/notion/SKILL.md +257 -0
  163. package/templates/skills/modules/playwright/SKILL.md +100 -0
  164. package/templates/skills/modules/rulebook-mcp/SKILL.md +166 -0
  165. package/templates/skills/modules/serena/SKILL.md +347 -0
  166. package/templates/skills/modules/supabase/SKILL.md +233 -0
  167. package/templates/skills/modules/synap/SKILL.md +79 -0
  168. package/templates/skills/modules/vectorizer/SKILL.md +73 -0
  169. package/templates/skills/services/azure-blob/SKILL.md +194 -0
  170. package/templates/skills/services/cassandra/SKILL.md +249 -0
  171. package/templates/skills/services/dynamodb/SKILL.md +318 -0
  172. package/templates/skills/services/elasticsearch/SKILL.md +357 -0
  173. package/templates/skills/services/gcs/SKILL.md +188 -0
  174. package/templates/skills/services/influxdb/SKILL.md +275 -0
  175. package/templates/skills/services/kafka/SKILL.md +351 -0
  176. package/templates/skills/services/mariadb/SKILL.md +193 -0
  177. package/templates/skills/services/memcached/SKILL.md +252 -0
  178. package/templates/skills/services/minio/SKILL.md +211 -0
  179. package/templates/skills/services/mongodb/SKILL.md +278 -0
  180. package/templates/skills/services/mysql/SKILL.md +368 -0
  181. package/templates/skills/services/neo4j/SKILL.md +257 -0
  182. package/templates/skills/services/oracle/SKILL.md +300 -0
  183. package/templates/skills/services/postgresql/SKILL.md +336 -0
  184. package/templates/skills/services/rabbitmq/SKILL.md +296 -0
  185. package/templates/skills/services/redis/SKILL.md +302 -0
  186. package/templates/skills/services/s3/SKILL.md +308 -0
  187. package/templates/skills/services/sqlite/SKILL.md +304 -0
  188. package/templates/skills/services/sqlserver/SKILL.md +304 -0
@@ -0,0 +1,68 @@
1
+ ---
2
+ name: "Ada"
3
+ description: "Execute these commands after EVERY implementation (see AGENT_AUTOMATION module for full workflow)."
4
+ version: "1.0.0"
5
+ category: "languages"
6
+ author: "Rulebook"
7
+ tags: ["languages", "language"]
8
+ dependencies: []
9
+ conflicts: []
10
+ ---
11
+ <!-- ADA:START -->
12
+ # Ada Project Rules
13
+
14
+ ## Agent Automation Commands
15
+
16
+ **CRITICAL**: Execute these commands after EVERY implementation (see AGENT_AUTOMATION module for full workflow).
17
+
18
+ ```bash
19
+ # Complete quality check sequence:
20
+ gprbuild -P project.gpr # Build verification
21
+ gnatcheck -P project.gpr # Style/linting check
22
+ gprclean -P project.gpr && gprbuild -P project.gpr # Clean build
23
+ # Run tests (project-specific command)
24
+
25
+ # SPARK verification (if using SPARK):
26
+ gnatprove -P project.gpr # Formal verification
27
+ ```
28
+
29
+ ## Ada Configuration
30
+
31
+ **CRITICAL**: Use Ada 2012 or Ada 2022 with GNAT compiler.
32
+
33
+ - **Standard**: Ada 2012 or Ada 2022
34
+ - **Compiler**: GNAT 12+
35
+ - **Build**: GPRbuild
36
+ - **TestingMenuAUnit
37
+ - **StyleMenuGNAT style checks
38
+
39
+ ## Code Quality Standards
40
+
41
+ ### Mandatory Quality Checks
42
+
43
+ **IMPORTANT**: These commands MUST match your GitHub Actions workflows!
44
+
45
+ ```bash
46
+ # Pre-Commit Checklist (MUST match .github/workflows/*.yml)
47
+
48
+ # 1. Style check (matches workflow)
49
+ gnatcheck -P your_project.gpr -rules -from=gnat_style.rules
50
+
51
+ # 2. Build with warnings as errors (matches workflow)
52
+ gprbuild -P your_project.gpr -cargs -gnatwa -gnatwe
53
+
54
+ # 3. Run static analysis (matches workflow)
55
+ gnatprove -P your_project.gpr --level=2
56
+
57
+ # 4. Run all tests (matches workflow)
58
+ gprbuild -P test_project.gpr
59
+ ./bin/test_runner
60
+
61
+ # If ANY fails: ❌ DO NOT COMMIT - Fix first!
62
+ ```
63
+
64
+ **Why This Matters:**
65
+ - Example: Missing `-gnatwe` (warnings as errors) = CI failures
66
+
67
+ <!-- ADA:END -->
68
+
@@ -0,0 +1,343 @@
1
+ ---
2
+ name: "C"
3
+ description: "Execute these commands after EVERY implementation (see AGENT_AUTOMATION module for full workflow)."
4
+ version: "1.0.0"
5
+ category: "languages"
6
+ author: "Rulebook"
7
+ tags: ["languages", "language"]
8
+ dependencies: []
9
+ conflicts: []
10
+ ---
11
+ <!-- C:START -->
12
+ # C Project Rules
13
+
14
+ ## Agent Automation Commands
15
+
16
+ **CRITICAL**: Execute these commands after EVERY implementation (see AGENT_AUTOMATION module for full workflow).
17
+
18
+ ```bash
19
+ # Complete quality check sequence:
20
+ clang-format --dry-run --Werror src/**/*.c # Format check
21
+ make lint # Linting (if configured)
22
+ make test # All tests (100% pass)
23
+ make # Build verification
24
+
25
+ # Memory safety (recommended):
26
+ valgrind --leak-check=full ./build/test # Memory leak check
27
+ ```
28
+
29
+ ## C Configuration
30
+
31
+ **CRITICAL**: Use C11 or C17 standard with strict warnings enabled.
32
+
33
+ - **Standard**: C11 or C17
34
+ - **Compiler**: GCC 11+ or Clang 14+
35
+ - **Build System**: CMake 3.20+ (recommended) or Make
36
+ - **Warnings**: Treat all warnings as errors
37
+ - **Sanitizers**: ASAN, UBSAN for memory safety
38
+
39
+ ### CMakeLists.txt Requirements
40
+
41
+ ```cmake
42
+ cmake_minimum_required(VERSION 3.20)
43
+ project(YourProject C)
44
+
45
+ set(CMAKE_C_STANDARD 17)
46
+ set(CMAKE_C_STANDARD_REQUIRED ON)
47
+ set(CMAKE_C_EXTENSIONS OFF)
48
+
49
+ # Compiler warnings
50
+ if(MSVC)
51
+ add_compile_options(/W4 /WX)
52
+ else()
53
+ add_compile_options(-Wall -Wextra -Werror -pedantic)
54
+ endif()
55
+
56
+ # Enable sanitizers in Debug mode
57
+ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
58
+ add_compile_options(-fsanitize=address,undefined)
59
+ add_link_options(-fsanitize=address,undefined)
60
+ endif()
61
+
62
+ # Source files
63
+ add_executable(${PROJECT_NAME} src/main.c src/module.c)
64
+
65
+ # Include directories
66
+ target_include_directories(${PROJECT_NAME} PUBLIC include)
67
+
68
+ # Enable testing
69
+ enable_testing()
70
+ add_subdirectory(tests)
71
+ ```
72
+
73
+ ## Code Quality Standards
74
+
75
+ ### Mandatory Quality Checks
76
+
77
+ **CRITICAL**: After implementing ANY feature, you MUST run these commands in order.
78
+
79
+ **IMPORTANT**: These commands MUST match your GitHub Actions workflows to prevent CI/CD failures!
80
+
81
+ ```bash
82
+ # Pre-Commit Checklist (MUST match .github/workflows/*.yml)
83
+
84
+ # 1. Format check (matches workflow - use --dry-run, not -i!)
85
+ clang-format --dry-run --Werror src/**/*.c include/**/*.h tests/**/*.c
86
+
87
+ # 2. Static analysis (matches workflow)
88
+ clang-tidy src/**/*.c -- -std=c17 -Wall -Wextra -Werror
89
+
90
+ # 3. Build with warnings as errors (matches workflow)
91
+ cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS="-Werror -Wall -Wextra -pedantic"
92
+ cmake --build build
93
+
94
+ # 4. Run all tests (MUST pass 100% - matches workflow)
95
+ ctest --test-dir build --output-on-failure --verbose
96
+
97
+ # 5. Check with Address Sanitizer (matches workflow)
98
+ cmake -B build-asan -DCMAKE_BUILD_TYPE=Debug \
99
+ -DCMAKE_C_FLAGS="-fsanitize=address,undefined -g"
100
+ cmake --build build-asan
101
+ ctest --test-dir build-asan --output-on-failure
102
+
103
+ # 6. Check with Valgrind (matches workflow)
104
+ valgrind --leak-check=full --error-exitcode=1 ./build/YourProject
105
+
106
+ # 7. Check coverage (matches workflow)
107
+ cmake -B build-cov -DCMAKE_BUILD_TYPE=Coverage \
108
+ -DCMAKE_C_FLAGS="-fprofile-arcs -ftest-coverage"
109
+ cmake --build build-cov
110
+ ctest --test-dir build-cov
111
+ gcov build-cov/CMakeFiles/YourProject.dir/src/*.gcno
112
+ lcov --capture --directory build-cov --output-file coverage.info
113
+ lcov --list coverage.info
114
+
115
+ # If ANY fails: ❌ DO NOT COMMIT - Fix first!
116
+ ```
117
+
118
+ **If ANY of these fail, you MUST fix the issues before committing.**
119
+
120
+ **Why This Matters:**
121
+ - Running different commands locally than in CI causes "works on my machine" failures
122
+ - CI/CD failures happen when local checks differ from workflows
123
+ - Example: Using `clang-format -i` locally but `--dry-run --Werror` in CI = failure
124
+ - Example: Missing `-Werror` flag = warnings pass locally but fail in CI
125
+ - Example: Skipping sanitizers locally = CI catches memory bugs, use-after-free, buffer overflows
126
+ - Example: Not running Valgrind = memory leaks pass locally but fail in CI
127
+
128
+ ### Formatting
129
+
130
+ - Use clang-format for consistent code style
131
+ - Configuration in `.clang-format`
132
+ - Check formatting in CI (don't auto-format)
133
+
134
+ Example `.clang-format`:
135
+ ```yaml
136
+ Language: C
137
+ BasedOnStyle: LLVM
138
+ IndentWidth: 4
139
+ ColumnLimit: 100
140
+ AllowShortFunctionsOnASingleLine: Empty
141
+ BreakBeforeBraces: Attach
142
+ AlignConsecutiveMacros: true
143
+ ```
144
+
145
+ ### Static Analysis
146
+
147
+ - Use clang-tidy for static analysis
148
+ - Configuration in `.clang-tidy`
149
+ - Enable modernize and bugprone checks
150
+
151
+ Example `.clang-tidy`:
152
+ ```yaml
153
+ Checks: >
154
+ -*,
155
+ bugprone-*,
156
+ clang-analyzer-*,
157
+ modernize-*,
158
+ readability-*,
159
+ performance-*,
160
+ portability-*
161
+
162
+ CheckOptions:
163
+ - key: readability-identifier-naming.FunctionCase
164
+ value: lower_case
165
+ - key: readability-identifier-naming.VariableCase
166
+ value: lower_case
167
+ ```
168
+
169
+ ### Testing
170
+
171
+ - **Framework**: Unity, Check, or CTest
172
+ - **Location**: `/tests` directory
173
+ - **Coverage**: Must meet threshold (80%+)
174
+ - **Sanitizers**: ASAN, UBSAN, Valgrind
175
+ - **Memory Safety**: Zero memory leaks
176
+
177
+ Example Unity test:
178
+ ```c
179
+ #include "unity.h"
180
+ #include "module.h"
181
+
182
+ void setUp(void) {
183
+ // Setup before each test
184
+ }
185
+
186
+ void tearDown(void) {
187
+ // Cleanup after each test
188
+ }
189
+
190
+ void test_function_should_return_expected_value(void) {
191
+ int result = my_function(10);
192
+ TEST_ASSERT_EQUAL_INT(20, result);
193
+ }
194
+
195
+ void test_function_should_handle_null_pointer(void) {
196
+ TEST_ASSERT_NULL(my_function_with_null(NULL));
197
+ }
198
+
199
+ int main(void) {
200
+ UNITY_BEGIN();
201
+ RUN_TEST(test_function_should_return_expected_value);
202
+ RUN_TEST(test_function_should_handle_null_pointer);
203
+ return UNITY_END();
204
+ }
205
+ ```
206
+
207
+ ## Memory Safety
208
+
209
+ **CRITICAL**: Always check for memory issues.
210
+
211
+ ### Required Checks
212
+
213
+ 1. **Address Sanitizer (ASAN)**:
214
+ ```bash
215
+ gcc -fsanitize=address -g -o program main.c
216
+ ./program
217
+ ```
218
+
219
+ 2. **Undefined Behavior Sanitizer (UBSAN)**:
220
+ ```bash
221
+ gcc -fsanitize=undefined -g -o program main.c
222
+ ./program
223
+ ```
224
+
225
+ 3. **Valgrind**:
226
+ ```bash
227
+ valgrind --leak-check=full --show-leak-kinds=all ./program
228
+ ```
229
+
230
+ 4. **Static Analysis**:
231
+ ```bash
232
+ clang-tidy src/**/*.c
233
+ cppcheck --enable=all --error-exitcode=1 src/
234
+ ```
235
+
236
+ ### Common Memory Issues to Prevent
237
+
238
+ ```c
239
+ // ❌ BAD: Memory leak
240
+ char *buffer = malloc(100);
241
+ // ... use buffer ...
242
+ // Missing free()
243
+
244
+ // ✅ GOOD: Proper cleanup
245
+ char *buffer = malloc(100);
246
+ if (buffer == NULL) {
247
+ return ERROR_NO_MEMORY;
248
+ }
249
+ // ... use buffer ...
250
+ free(buffer);
251
+ buffer = NULL;
252
+
253
+ // ❌ BAD: Use after free
254
+ char *ptr = malloc(10);
255
+ free(ptr);
256
+ strcpy(ptr, "test"); // UNDEFINED BEHAVIOR!
257
+
258
+ // ✅ GOOD: NULL after free
259
+ char *ptr = malloc(10);
260
+ free(ptr);
261
+ ptr = NULL;
262
+ if (ptr != NULL) {
263
+ strcpy(ptr, "test");
264
+ }
265
+
266
+ // ❌ BAD: Buffer overflow
267
+ char buffer[10];
268
+ strcpy(buffer, "This is too long"); // BUFFER OVERFLOW!
269
+
270
+ // ✅ GOOD: Bounds checking
271
+ char buffer[10];
272
+ strncpy(buffer, "Safe", sizeof(buffer) - 1);
273
+ buffer[sizeof(buffer) - 1] = '\0';
274
+ ```
275
+
276
+ ## Best Practices
277
+
278
+ ### DO's ✅
279
+
280
+ - **CHECK** return values from all functions
281
+ - **VALIDATE** all pointer arguments for NULL
282
+ - **FREE** all allocated memory
283
+ - **USE** const for immutable pointers
284
+ - **LIMIT** variable scope
285
+ - **ZERO** memory after free for security
286
+ - **BOUNDS** check all array accesses
287
+ - **SANITIZE** all inputs
288
+
289
+ ### DON'Ts ❌
290
+
291
+ - **NEVER** ignore compiler warnings
292
+ - **NEVER** assume malloc succeeds
293
+ - **NEVER** use gets() (use fgets())
294
+ - **NEVER** use strcpy() (use strncpy() or strlcpy())
295
+ - **NEVER** use sprintf() (use snprintf())
296
+ - **NEVER** dereference NULL pointers
297
+ - **NEVER** return pointers to stack variables
298
+ - **NEVER** skip sanitizer checks
299
+
300
+ ## Security Guidelines
301
+
302
+ 1. **Input Validation**: Validate all external inputs
303
+ 2. **Buffer Safety**: Always check bounds
304
+ 3. **Integer Overflow**: Check arithmetic operations
305
+ 4. **Format String**: Never use user input as format string
306
+ 5. **Memory Zeroization**: Zero sensitive data after use
307
+
308
+ Example secure code:
309
+ ```c
310
+ #include <string.h>
311
+ #include <stdlib.h>
312
+ #include <stdio.h>
313
+
314
+ // Secure string copy with bounds checking
315
+ int safe_strcpy(char *dest, size_t dest_size, const char *src) {
316
+ if (dest == NULL || src == NULL || dest_size == 0) {
317
+ return -1;
318
+ }
319
+
320
+ size_t src_len = strlen(src);
321
+ if (src_len >= dest_size) {
322
+ return -1; // Not enough space
323
+ }
324
+
325
+ strncpy(dest, src, dest_size - 1);
326
+ dest[dest_size - 1] = '\0';
327
+ return 0;
328
+ }
329
+
330
+ // Secure memory cleanup
331
+ void secure_free(void **ptr, size_t size) {
332
+ if (ptr == NULL || *ptr == NULL) {
333
+ return;
334
+ }
335
+
336
+ // Zero memory before free
337
+ memset(*ptr, 0, size);
338
+ free(*ptr);
339
+ *ptr = NULL;
340
+ }
341
+ ```
342
+
343
+ <!-- C:END -->