@cubis/foundry 0.3.70 → 0.3.72

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 (304) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/cli/core.js +4 -18
  3. package/dist/cli/core.js.map +1 -1
  4. package/package.json +1 -1
  5. package/src/cli/core.ts +4 -18
  6. package/workflows/powers/accessibility/POWER.md +83 -94
  7. package/workflows/powers/accessibility/SKILL.md +82 -94
  8. package/workflows/powers/agent-design/POWER.md +201 -0
  9. package/workflows/powers/agent-design/SKILL.md +198 -0
  10. package/workflows/powers/agent-design/references/clarification-patterns.md +153 -0
  11. package/workflows/powers/agent-design/references/skill-testing.md +164 -0
  12. package/workflows/powers/agent-design/references/workflow-patterns.md +226 -0
  13. package/workflows/powers/agentic-eval/POWER.md +62 -0
  14. package/workflows/powers/agentic-eval/SKILL.md +59 -0
  15. package/workflows/powers/agentic-eval/references/rubric-and-regression-checklist.md +11 -0
  16. package/workflows/powers/api-designer/POWER.md +43 -71
  17. package/workflows/powers/api-designer/SKILL.md +43 -71
  18. package/workflows/powers/api-patterns/POWER.md +42 -56
  19. package/workflows/powers/api-patterns/SKILL.md +42 -57
  20. package/workflows/powers/architecture-designer/POWER.md +43 -60
  21. package/workflows/powers/architecture-designer/SKILL.md +43 -60
  22. package/workflows/powers/ask-questions-if-underspecified/POWER.md +51 -3
  23. package/workflows/powers/ask-questions-if-underspecified/SKILL.md +51 -3
  24. package/workflows/powers/auth-architect/POWER.md +69 -0
  25. package/workflows/powers/auth-architect/SKILL.md +66 -0
  26. package/workflows/powers/auth-architect/references/session-token-policy-checklist.md +45 -0
  27. package/workflows/powers/behavioral-modes/POWER.md +100 -9
  28. package/workflows/powers/behavioral-modes/SKILL.md +100 -9
  29. package/workflows/powers/c-pro/POWER.md +105 -0
  30. package/workflows/powers/c-pro/SKILL.md +102 -0
  31. package/workflows/powers/c-pro/references/build-systems-and-toolchains.md +148 -0
  32. package/workflows/powers/c-pro/references/common-ub-and-portability.md +166 -0
  33. package/workflows/powers/c-pro/references/debugging-with-sanitizers.md +205 -0
  34. package/workflows/powers/c-pro/references/memory-safety-and-build-checklist.md +60 -0
  35. package/workflows/powers/c-pro/references/posix-and-platform-apis.md +244 -0
  36. package/workflows/powers/changelog-generator/POWER.md +127 -63
  37. package/workflows/powers/changelog-generator/SKILL.md +126 -63
  38. package/workflows/powers/ci-cd-pipelines/POWER.md +156 -0
  39. package/workflows/powers/ci-cd-pipelines/SKILL.md +153 -0
  40. package/workflows/powers/ci-cd-pipelines/references/github-actions-patterns.md +160 -0
  41. package/workflows/powers/ci-cd-pipelines/references/pipeline-security-checklist.md +57 -0
  42. package/workflows/powers/cli-developer/POWER.md +152 -95
  43. package/workflows/powers/cli-developer/SKILL.md +152 -95
  44. package/workflows/powers/cpp-pro/POWER.md +111 -0
  45. package/workflows/powers/cpp-pro/SKILL.md +108 -0
  46. package/workflows/powers/cpp-pro/references/concurrency-primitives.md +266 -0
  47. package/workflows/powers/cpp-pro/references/move-semantics-and-value-types.md +149 -0
  48. package/workflows/powers/cpp-pro/references/performance-and-profiling.md +191 -0
  49. package/workflows/powers/cpp-pro/references/raii-and-modern-cpp-checklist.md +87 -0
  50. package/workflows/powers/cpp-pro/references/template-and-concepts-patterns.md +205 -0
  51. package/workflows/powers/csharp-pro/POWER.md +47 -22
  52. package/workflows/powers/csharp-pro/SKILL.md +47 -22
  53. package/workflows/powers/dart-pro/POWER.md +68 -0
  54. package/workflows/powers/dart-pro/SKILL.md +65 -0
  55. package/workflows/powers/dart-pro/references/isolate-and-concurrency.md +180 -0
  56. package/workflows/powers/dart-pro/references/null-safety-and-async-patterns.md +133 -0
  57. package/workflows/powers/dart-pro/references/package-structure-and-linting.md +193 -0
  58. package/workflows/powers/dart-pro/references/sealed-records-patterns.md +173 -0
  59. package/workflows/powers/dart-pro/references/testing-and-mocking.md +235 -0
  60. package/workflows/powers/database-design/POWER.md +47 -33
  61. package/workflows/powers/database-design/SKILL.md +47 -33
  62. package/workflows/powers/database-optimizer/POWER.md +43 -64
  63. package/workflows/powers/database-optimizer/SKILL.md +43 -64
  64. package/workflows/powers/database-skills/POWER.md +59 -93
  65. package/workflows/powers/database-skills/SKILL.md +59 -93
  66. package/workflows/powers/debugging-strategies/POWER.md +69 -0
  67. package/workflows/powers/debugging-strategies/SKILL.md +66 -0
  68. package/workflows/powers/debugging-strategies/references/reproduce-isolate-verify-checklist.md +42 -0
  69. package/workflows/powers/deep-research/POWER.md +67 -0
  70. package/workflows/powers/deep-research/SKILL.md +64 -0
  71. package/workflows/powers/deep-research/references/multi-round-research-loop.md +80 -0
  72. package/workflows/powers/design-system-builder/POWER.md +130 -116
  73. package/workflows/powers/design-system-builder/SKILL.md +130 -116
  74. package/workflows/powers/devops-engineer/POWER.md +120 -57
  75. package/workflows/powers/devops-engineer/SKILL.md +120 -57
  76. package/workflows/powers/docker-kubernetes/POWER.md +94 -0
  77. package/workflows/powers/docker-kubernetes/SKILL.md +91 -0
  78. package/workflows/powers/docker-kubernetes/references/dockerfile-optimization-checklist.md +35 -0
  79. package/workflows/powers/docker-kubernetes/references/kubernetes-deployment-patterns.md +59 -0
  80. package/workflows/powers/documentation-templates/POWER.md +158 -127
  81. package/workflows/powers/documentation-templates/SKILL.md +158 -127
  82. package/workflows/powers/drizzle-expert/POWER.md +66 -0
  83. package/workflows/powers/drizzle-expert/SKILL.md +63 -0
  84. package/workflows/powers/drizzle-expert/references/runtime-pairing-matrix.md +16 -0
  85. package/workflows/powers/drizzle-expert/references/schema-and-migration-playbook.md +18 -0
  86. package/workflows/powers/error-ux-observability/POWER.md +144 -131
  87. package/workflows/powers/error-ux-observability/SKILL.md +143 -131
  88. package/workflows/powers/fastapi-expert/POWER.md +46 -60
  89. package/workflows/powers/fastapi-expert/SKILL.md +46 -60
  90. package/workflows/powers/firebase/POWER.md +65 -0
  91. package/workflows/powers/firebase/SKILL.md +62 -0
  92. package/workflows/powers/firebase/references/platform-routing.md +16 -0
  93. package/workflows/powers/firebase/references/rules-and-indexes-checklist.md +11 -0
  94. package/workflows/powers/flutter-design-system/POWER.md +63 -0
  95. package/workflows/powers/flutter-design-system/SKILL.md +60 -0
  96. package/workflows/powers/flutter-design-system/references/shared-widgets.md +29 -0
  97. package/workflows/powers/flutter-design-system/references/tokens-and-theme.md +34 -0
  98. package/workflows/powers/flutter-drift/POWER.md +65 -0
  99. package/workflows/powers/flutter-drift/SKILL.md +62 -0
  100. package/workflows/powers/flutter-drift/references/migrations.md +22 -0
  101. package/workflows/powers/flutter-drift/references/query-patterns.md +26 -0
  102. package/workflows/powers/flutter-feature/POWER.md +65 -0
  103. package/workflows/powers/flutter-feature/SKILL.md +62 -0
  104. package/workflows/powers/flutter-feature/references/architecture-rules.md +85 -0
  105. package/workflows/powers/flutter-feature/references/composite-provider.md +58 -0
  106. package/workflows/powers/flutter-feature/references/outbox-pattern.md +87 -0
  107. package/workflows/powers/flutter-feature/references/testing-patterns.md +218 -0
  108. package/workflows/powers/flutter-go-router/POWER.md +64 -0
  109. package/workflows/powers/flutter-go-router/SKILL.md +61 -0
  110. package/workflows/powers/flutter-go-router/references/guards-and-deeplinks.md +20 -0
  111. package/workflows/powers/flutter-go-router/references/typed-routes.md +27 -0
  112. package/workflows/powers/flutter-offline-sync/POWER.md +62 -0
  113. package/workflows/powers/flutter-offline-sync/SKILL.md +59 -0
  114. package/workflows/powers/flutter-offline-sync/references/outbox-full.md +44 -0
  115. package/workflows/powers/flutter-repository/POWER.md +64 -0
  116. package/workflows/powers/flutter-repository/SKILL.md +61 -0
  117. package/workflows/powers/flutter-repository/references/drift-patterns.md +21 -0
  118. package/workflows/powers/flutter-repository/references/retrofit-patterns.md +20 -0
  119. package/workflows/powers/flutter-riverpod/POWER.md +70 -0
  120. package/workflows/powers/flutter-riverpod/SKILL.md +67 -0
  121. package/workflows/powers/flutter-riverpod/references/async-and-mutations.md +19 -0
  122. package/workflows/powers/flutter-riverpod/references/async-lifecycle.md +19 -0
  123. package/workflows/powers/flutter-riverpod/references/provider-selection.md +20 -0
  124. package/workflows/powers/flutter-riverpod/references/testing.md +21 -0
  125. package/workflows/powers/flutter-riverpod/references/version-matrix.md +24 -0
  126. package/workflows/powers/flutter-state-machine/POWER.md +62 -0
  127. package/workflows/powers/flutter-state-machine/SKILL.md +59 -0
  128. package/workflows/powers/flutter-state-machine/references/app-state-contract.md +23 -0
  129. package/workflows/powers/flutter-state-machine/references/ui-rendering.md +14 -0
  130. package/workflows/powers/flutter-testing/POWER.md +64 -0
  131. package/workflows/powers/flutter-testing/SKILL.md +61 -0
  132. package/workflows/powers/flutter-testing/references/offline-sync-tests.md +16 -0
  133. package/workflows/powers/flutter-testing/references/test-layers.md +33 -0
  134. package/workflows/powers/frontend-code-review/POWER.md +137 -0
  135. package/workflows/powers/frontend-code-review/SKILL.md +134 -0
  136. package/workflows/powers/frontend-code-review/references/common-antipatterns.md +86 -0
  137. package/workflows/powers/frontend-code-review/references/performance-budgets.md +56 -0
  138. package/workflows/powers/frontend-code-review/references/review-checklists.md +47 -0
  139. package/workflows/powers/frontend-design/POWER.md +163 -362
  140. package/workflows/powers/frontend-design/SKILL.md +163 -362
  141. package/workflows/powers/game-development/POWER.md +57 -140
  142. package/workflows/powers/game-development/SKILL.md +57 -140
  143. package/workflows/powers/geo-fundamentals/POWER.md +64 -126
  144. package/workflows/powers/geo-fundamentals/SKILL.md +64 -127
  145. package/workflows/powers/git-workflow/POWER.md +135 -0
  146. package/workflows/powers/git-workflow/SKILL.md +132 -0
  147. package/workflows/powers/git-workflow/references/pr-review-checklist.md +63 -0
  148. package/workflows/powers/golang-pro/POWER.md +46 -35
  149. package/workflows/powers/golang-pro/SKILL.md +46 -35
  150. package/workflows/powers/graphql-architect/POWER.md +44 -62
  151. package/workflows/powers/graphql-architect/SKILL.md +44 -62
  152. package/workflows/powers/i18n-localization/POWER.md +118 -103
  153. package/workflows/powers/i18n-localization/SKILL.md +118 -103
  154. package/workflows/powers/java-pro/POWER.md +47 -22
  155. package/workflows/powers/java-pro/SKILL.md +47 -22
  156. package/workflows/powers/javascript-pro/POWER.md +47 -34
  157. package/workflows/powers/javascript-pro/SKILL.md +47 -34
  158. package/workflows/powers/kotlin-pro/POWER.md +46 -23
  159. package/workflows/powers/kotlin-pro/SKILL.md +46 -23
  160. package/workflows/powers/legacy-modernizer/POWER.md +43 -60
  161. package/workflows/powers/legacy-modernizer/SKILL.md +43 -60
  162. package/workflows/powers/mcp-builder/POWER.md +65 -0
  163. package/workflows/powers/mcp-builder/SKILL.md +62 -0
  164. package/workflows/powers/mcp-builder/references/testing-and-evals.md +17 -0
  165. package/workflows/powers/mcp-builder/references/transport-and-tool-design.md +17 -0
  166. package/workflows/powers/microservices-architect/POWER.md +43 -70
  167. package/workflows/powers/microservices-architect/SKILL.md +43 -70
  168. package/workflows/powers/mobile-design/POWER.md +110 -345
  169. package/workflows/powers/mobile-design/SKILL.md +110 -345
  170. package/workflows/powers/mongodb/POWER.md +67 -0
  171. package/workflows/powers/mongodb/SKILL.md +64 -0
  172. package/workflows/powers/mongodb/references/mongodb-checklist.md +20 -0
  173. package/workflows/powers/mysql/POWER.md +67 -0
  174. package/workflows/powers/mysql/SKILL.md +64 -0
  175. package/workflows/powers/mysql/references/mysql-checklist.md +20 -0
  176. package/workflows/powers/neki/POWER.md +67 -0
  177. package/workflows/powers/neki/SKILL.md +64 -0
  178. package/workflows/powers/neki/references/neki-checklist.md +18 -0
  179. package/workflows/powers/nestjs-expert/POWER.md +45 -91
  180. package/workflows/powers/nestjs-expert/SKILL.md +45 -91
  181. package/workflows/powers/nextjs-developer/POWER.md +51 -44
  182. package/workflows/powers/nextjs-developer/SKILL.md +51 -44
  183. package/workflows/powers/nodejs-best-practices/POWER.md +48 -29
  184. package/workflows/powers/nodejs-best-practices/SKILL.md +48 -29
  185. package/workflows/powers/observability/POWER.md +109 -0
  186. package/workflows/powers/observability/SKILL.md +106 -0
  187. package/workflows/powers/observability/references/alerting-and-slo-checklist.md +87 -0
  188. package/workflows/powers/observability/references/opentelemetry-setup-guide.md +121 -0
  189. package/workflows/powers/openai-docs/POWER.md +61 -0
  190. package/workflows/powers/openai-docs/SKILL.md +58 -0
  191. package/workflows/powers/openai-docs/references/official-source-playbook.md +10 -0
  192. package/workflows/powers/performance-profiling/POWER.md +61 -114
  193. package/workflows/powers/performance-profiling/SKILL.md +61 -114
  194. package/workflows/powers/php-pro/POWER.md +116 -0
  195. package/workflows/powers/php-pro/SKILL.md +113 -0
  196. package/workflows/powers/php-pro/references/architecture-and-di.md +239 -0
  197. package/workflows/powers/php-pro/references/modern-php-features.md +189 -0
  198. package/workflows/powers/php-pro/references/performance-and-deployment.md +197 -0
  199. package/workflows/powers/php-pro/references/php84-strict-typing-checklist.md +161 -0
  200. package/workflows/powers/php-pro/references/testing-and-static-analysis.md +235 -0
  201. package/workflows/powers/playwright-e2e/POWER.md +85 -0
  202. package/workflows/powers/playwright-e2e/SKILL.md +82 -0
  203. package/workflows/powers/playwright-e2e/references/locator-trace-flake-checklist.md +80 -0
  204. package/workflows/powers/postgres/POWER.md +67 -0
  205. package/workflows/powers/postgres/SKILL.md +64 -0
  206. package/workflows/powers/postgres/references/postgres-checklist.md +20 -0
  207. package/workflows/powers/prompt-engineer/POWER.md +47 -30
  208. package/workflows/powers/prompt-engineer/SKILL.md +47 -30
  209. package/workflows/powers/python-pro/POWER.md +47 -36
  210. package/workflows/powers/python-pro/SKILL.md +47 -36
  211. package/workflows/powers/react-best-practices/POWER.md +56 -33
  212. package/workflows/powers/react-best-practices/SKILL.md +56 -33
  213. package/workflows/powers/react-expert/POWER.md +47 -37
  214. package/workflows/powers/react-expert/SKILL.md +47 -37
  215. package/workflows/powers/redis/POWER.md +67 -0
  216. package/workflows/powers/redis/SKILL.md +64 -0
  217. package/workflows/powers/redis/references/redis-checklist.md +19 -0
  218. package/workflows/powers/ruby-pro/POWER.md +118 -0
  219. package/workflows/powers/ruby-pro/SKILL.md +115 -0
  220. package/workflows/powers/ruby-pro/references/modern-ruby-features.md +189 -0
  221. package/workflows/powers/ruby-pro/references/object-design-patterns.md +220 -0
  222. package/workflows/powers/ruby-pro/references/performance-and-profiling.md +224 -0
  223. package/workflows/powers/ruby-pro/references/ruby-concurrency-and-testing.md +190 -0
  224. package/workflows/powers/ruby-pro/references/testing-and-rspec.md +236 -0
  225. package/workflows/powers/rust-pro/POWER.md +45 -31
  226. package/workflows/powers/rust-pro/SKILL.md +45 -31
  227. package/workflows/powers/security-engineer/POWER.md +129 -0
  228. package/workflows/powers/security-engineer/SKILL.md +126 -0
  229. package/workflows/powers/seo-fundamentals/POWER.md +59 -102
  230. package/workflows/powers/seo-fundamentals/SKILL.md +59 -102
  231. package/workflows/powers/serverless-patterns/POWER.md +171 -0
  232. package/workflows/powers/serverless-patterns/SKILL.md +168 -0
  233. package/workflows/powers/skill-creator/POWER.md +90 -0
  234. package/workflows/powers/skill-creator/SKILL.md +87 -0
  235. package/workflows/powers/skill-creator/references/platform-formats.md +181 -0
  236. package/workflows/powers/skill-creator/references/schemas.md +430 -0
  237. package/workflows/powers/spec-miner/POWER.md +49 -57
  238. package/workflows/powers/spec-miner/SKILL.md +49 -57
  239. package/workflows/powers/sqlite/POWER.md +67 -0
  240. package/workflows/powers/sqlite/SKILL.md +64 -0
  241. package/workflows/powers/sqlite/references/sqlite-checklist.md +19 -0
  242. package/workflows/powers/sre-engineer/POWER.md +123 -64
  243. package/workflows/powers/sre-engineer/SKILL.md +123 -64
  244. package/workflows/powers/static-analysis/POWER.md +121 -77
  245. package/workflows/powers/static-analysis/SKILL.md +121 -77
  246. package/workflows/powers/stripe-best-practices/POWER.md +140 -17
  247. package/workflows/powers/stripe-best-practices/SKILL.md +139 -17
  248. package/workflows/powers/supabase/POWER.md +67 -0
  249. package/workflows/powers/supabase/SKILL.md +64 -0
  250. package/workflows/powers/supabase/references/supabase-checklist.md +19 -0
  251. package/workflows/powers/swift-pro/POWER.md +118 -0
  252. package/workflows/powers/swift-pro/SKILL.md +115 -0
  253. package/workflows/powers/swift-pro/references/concurrency-patterns.md +165 -0
  254. package/workflows/powers/swift-pro/references/protocol-and-generics.md +172 -0
  255. package/workflows/powers/swift-pro/references/sendable-and-isolation.md +116 -0
  256. package/workflows/powers/swift-pro/references/swift-concurrency-and-protocols.md +260 -0
  257. package/workflows/powers/swift-pro/references/testing-and-packages.md +192 -0
  258. package/workflows/powers/tailwind-patterns/POWER.md +71 -240
  259. package/workflows/powers/tailwind-patterns/SKILL.md +71 -240
  260. package/workflows/powers/testing-patterns/POWER.md +155 -10
  261. package/workflows/powers/testing-patterns/SKILL.md +155 -10
  262. package/workflows/powers/typescript-pro/POWER.md +47 -38
  263. package/workflows/powers/typescript-pro/SKILL.md +47 -38
  264. package/workflows/powers/vitess/POWER.md +67 -0
  265. package/workflows/powers/vitess/SKILL.md +64 -0
  266. package/workflows/powers/vitess/references/vitess-checklist.md +19 -0
  267. package/workflows/powers/vulnerability-scanner/POWER.md +146 -10
  268. package/workflows/powers/vulnerability-scanner/SKILL.md +146 -10
  269. package/workflows/powers/web-perf/POWER.md +43 -170
  270. package/workflows/powers/web-perf/SKILL.md +43 -170
  271. package/workflows/powers/webapp-testing/POWER.md +43 -164
  272. package/workflows/powers/webapp-testing/SKILL.md +43 -164
  273. package/workflows/skills/agent-design/SKILL.md +198 -0
  274. package/workflows/skills/agent-design/references/clarification-patterns.md +153 -0
  275. package/workflows/skills/agent-design/references/skill-testing.md +164 -0
  276. package/workflows/skills/agent-design/references/workflow-patterns.md +226 -0
  277. package/workflows/skills/deep-research/SKILL.md +25 -20
  278. package/workflows/skills/deep-research/references/multi-round-research-loop.md +73 -8
  279. package/workflows/skills/frontend-design/SKILL.md +37 -32
  280. package/workflows/skills/frontend-design/commands/brand.md +167 -0
  281. package/workflows/skills/frontend-design/references/brand-presets.md +228 -0
  282. package/workflows/skills/generated/skill-audit.json +11 -2
  283. package/workflows/skills/generated/skill-catalog.json +37 -5
  284. package/workflows/skills/skills_index.json +1 -1
  285. package/workflows/workflows/agent-environment-setup/platforms/claude/skills/agent-design/SKILL.md +198 -0
  286. package/workflows/workflows/agent-environment-setup/platforms/claude/skills/agent-design/references/clarification-patterns.md +153 -0
  287. package/workflows/workflows/agent-environment-setup/platforms/claude/skills/agent-design/references/skill-testing.md +164 -0
  288. package/workflows/workflows/agent-environment-setup/platforms/claude/skills/agent-design/references/workflow-patterns.md +226 -0
  289. package/workflows/workflows/agent-environment-setup/platforms/claude/skills/deep-research/SKILL.md +25 -20
  290. package/workflows/workflows/agent-environment-setup/platforms/claude/skills/deep-research/references/multi-round-research-loop.md +73 -8
  291. package/workflows/workflows/agent-environment-setup/platforms/claude/skills/frontend-design/SKILL.md +37 -32
  292. package/workflows/workflows/agent-environment-setup/platforms/claude/skills/frontend-design/commands/brand.md +167 -0
  293. package/workflows/workflows/agent-environment-setup/platforms/claude/skills/frontend-design/references/brand-presets.md +228 -0
  294. package/workflows/workflows/agent-environment-setup/platforms/claude/skills/skills_index.json +1 -1
  295. package/workflows/workflows/agent-environment-setup/platforms/copilot/skills/agent-design/SKILL.md +197 -0
  296. package/workflows/workflows/agent-environment-setup/platforms/copilot/skills/agent-design/references/clarification-patterns.md +153 -0
  297. package/workflows/workflows/agent-environment-setup/platforms/copilot/skills/agent-design/references/skill-testing.md +164 -0
  298. package/workflows/workflows/agent-environment-setup/platforms/copilot/skills/agent-design/references/workflow-patterns.md +226 -0
  299. package/workflows/workflows/agent-environment-setup/platforms/copilot/skills/deep-research/SKILL.md +25 -20
  300. package/workflows/workflows/agent-environment-setup/platforms/copilot/skills/deep-research/references/multi-round-research-loop.md +73 -8
  301. package/workflows/workflows/agent-environment-setup/platforms/copilot/skills/frontend-design/SKILL.md +37 -32
  302. package/workflows/workflows/agent-environment-setup/platforms/copilot/skills/frontend-design/commands/brand.md +167 -0
  303. package/workflows/workflows/agent-environment-setup/platforms/copilot/skills/frontend-design/references/brand-presets.md +228 -0
  304. package/workflows/workflows/agent-environment-setup/platforms/copilot/skills/skills_index.json +1 -1
@@ -0,0 +1,166 @@
1
+ # Common Undefined Behavior and Portability
2
+
3
+ ## Undefined Behavior Catalog
4
+
5
+ These are the most common sources of UB in C code. Sanitizers catch many at runtime, but prevention through coding discipline is the primary defense.
6
+
7
+ ### Signed Integer Overflow
8
+
9
+ ```c
10
+ // UB — signed overflow is undefined in C
11
+ int x = INT_MAX;
12
+ x += 1; // UB: compiler may assume this never happens and optimize accordingly
13
+
14
+ // SAFE — check before arithmetic
15
+ if (x <= INT_MAX - 1) {
16
+ x += 1;
17
+ }
18
+
19
+ // SAFE — use unsigned for wrapping arithmetic
20
+ unsigned int y = UINT_MAX;
21
+ y += 1; // defined: wraps to 0
22
+ ```
23
+
24
+ ### Null Pointer Dereference
25
+
26
+ ```c
27
+ // UB — dereferencing NULL
28
+ int *p = NULL;
29
+ int val = *p; // UB
30
+
31
+ // SAFE — check before dereference
32
+ if (p != NULL) {
33
+ int val = *p;
34
+ }
35
+
36
+ // Gotcha: compiler may remove NULL checks after dereference
37
+ int val = *p; // if this executes, compiler assumes p != NULL
38
+ if (p == NULL) { ... } // compiler may optimize this away as "unreachable"
39
+ ```
40
+
41
+ ### Buffer Overflow and Out-of-Bounds Access
42
+
43
+ ```c
44
+ // UB — reading/writing past allocation
45
+ int arr[10];
46
+ arr[10] = 42; // UB: index 10 is one past the end
47
+
48
+ // UB — string function overflow
49
+ char buf[8];
50
+ strcpy(buf, "this string is too long"); // UB: writes past buf
51
+
52
+ // SAFE — use bounded functions
53
+ char buf[8];
54
+ strncpy(buf, source, sizeof(buf) - 1);
55
+ buf[sizeof(buf) - 1] = '\0'; // ensure null termination
56
+
57
+ // Better: use snprintf for formatted strings
58
+ snprintf(buf, sizeof(buf), "%s", source);
59
+ ```
60
+
61
+ ### Use After Free and Double Free
62
+
63
+ ```c
64
+ // UB — use after free
65
+ char *data = malloc(100);
66
+ free(data);
67
+ data[0] = 'x'; // UB: data is freed
68
+
69
+ // UB — double free
70
+ free(data);
71
+ free(data); // UB: second free on already-freed pointer
72
+
73
+ // SAFE — NULL after free
74
+ free(data);
75
+ data = NULL; // prevents use-after-free (dereference NULL → crash, not silent corruption)
76
+ ```
77
+
78
+ ### Strict Aliasing Violations
79
+
80
+ ```c
81
+ // UB — accessing memory through incompatible pointer type
82
+ float f = 3.14f;
83
+ int *ip = (int *)&f;
84
+ int bits = *ip; // UB: strict aliasing violation
85
+
86
+ // SAFE — use memcpy for type punning
87
+ float f = 3.14f;
88
+ int bits;
89
+ memcpy(&bits, &f, sizeof(bits)); // defined behavior
90
+
91
+ // SAFE — union-based type punning (C99+, implementation-defined but widely supported)
92
+ union { float f; int i; } u;
93
+ u.f = 3.14f;
94
+ int bits = u.i;
95
+ ```
96
+
97
+ ### Uninitialized Variable Reads
98
+
99
+ ```c
100
+ // UB — reading uninitialized automatic variable
101
+ int x;
102
+ printf("%d\n", x); // UB: x has indeterminate value
103
+
104
+ // SAFE — always initialize
105
+ int x = 0;
106
+ ```
107
+
108
+ ## Implementation-Defined Behavior Traps
109
+
110
+ These are defined by the compiler/platform but differ across implementations:
111
+
112
+ | Behavior | Varies by | Recommendation |
113
+ | ----------------------------- | ------------------- | ----------------------------------------------------------------- |
114
+ | `sizeof(int)` | Platform | Use fixed-width types (`int32_t`, `uint64_t`) for data structures |
115
+ | Bit-shift of negative values | Compiler | Avoid shifting signed integers; use unsigned |
116
+ | Struct padding and alignment | Compiler + platform | Use `offsetof`, `_Alignof`; pack with attributes only when needed |
117
+ | Char signedness (`char`) | Compiler | Use `signed char` or `unsigned char` explicitly for arithmetic |
118
+ | Evaluation order of arguments | Compiler | Avoid side effects in function argument expressions |
119
+
120
+ ## Cross-Platform Portability
121
+
122
+ ```c
123
+ // Use fixed-width integers for portable data structures
124
+ #include <stdint.h>
125
+
126
+ typedef struct {
127
+ uint32_t id;
128
+ int64_t timestamp;
129
+ uint16_t flags;
130
+ } __attribute__((packed)) WireMessage; // packed for network protocol
131
+
132
+ // Endianness-safe serialization
133
+ #include <arpa/inet.h> // or use manual byte swapping
134
+
135
+ void serialize(const WireMessage *msg, uint8_t *buf) {
136
+ uint32_t id_be = htonl(msg->id);
137
+ memcpy(buf, &id_be, sizeof(id_be));
138
+ // ... continue for other fields
139
+ }
140
+
141
+ // Feature detection with preprocessor
142
+ #if defined(__linux__)
143
+ #include <sys/epoll.h>
144
+ #elif defined(__APPLE__)
145
+ #include <sys/event.h>
146
+ #elif defined(_WIN32)
147
+ #include <winsock2.h>
148
+ #endif
149
+ ```
150
+
151
+ ## Compiler Warning Flags for UB Detection
152
+
153
+ ```bash
154
+ # GCC — maximum diagnostic coverage
155
+ gcc -Wall -Wextra -Wpedantic -Werror \
156
+ -Wformat=2 -Wformat-overflow -Wformat-truncation \
157
+ -Wconversion -Wsign-conversion \
158
+ -Wshadow -Wdouble-promotion \
159
+ -Wnull-dereference -Wuninitialized \
160
+ -Wstrict-aliasing=2 \
161
+ -fstack-protector-strong
162
+
163
+ # Clang — additional checks
164
+ clang -Weverything -Wno-padded -Wno-disabled-macro-expansion \
165
+ -fsanitize=undefined,address
166
+ ```
@@ -0,0 +1,205 @@
1
+ # Debugging with Sanitizers
2
+
3
+ ## AddressSanitizer (ASan)
4
+
5
+ Detects: buffer overflow, use-after-free, use-after-return, double-free, memory leaks.
6
+
7
+ ```bash
8
+ # Compile with ASan
9
+ gcc -fsanitize=address -fno-omit-frame-pointer -g -O1 -o myapp myapp.c
10
+ clang -fsanitize=address -fno-omit-frame-pointer -g -O1 -o myapp myapp.c
11
+
12
+ # Run — crashes with detailed report on first error
13
+ ./myapp
14
+ # Output shows: error type, stack trace, shadow memory state
15
+ ```
16
+
17
+ ```c
18
+ // ASan catches this at runtime
19
+ void trigger_heap_overflow(void) {
20
+ int *arr = malloc(10 * sizeof(int));
21
+ arr[10] = 42; // heap-buffer-overflow detected
22
+ free(arr);
23
+ }
24
+
25
+ // ASan catches use-after-free
26
+ void trigger_uaf(void) {
27
+ int *p = malloc(sizeof(int));
28
+ free(p);
29
+ *p = 42; // heap-use-after-free detected
30
+ }
31
+ ```
32
+
33
+ ### ASan Options
34
+
35
+ ```bash
36
+ # Environment variable to control ASan behavior
37
+ export ASAN_OPTIONS="detect_leaks=1:halt_on_error=0:print_stats=1"
38
+
39
+ # detect_leaks=1 — also report memory leaks at exit
40
+ # halt_on_error=0 — continue after first error (find multiple issues)
41
+ # print_stats=1 — show memory allocation statistics
42
+ # suppressions=file — suppress known false positives
43
+ ```
44
+
45
+ ## UndefinedBehaviorSanitizer (UBSan)
46
+
47
+ Detects: signed integer overflow, null pointer dereference, misaligned access, shift overflow, division by zero.
48
+
49
+ ```bash
50
+ # Compile with UBSan
51
+ gcc -fsanitize=undefined -fno-omit-frame-pointer -g -o myapp myapp.c
52
+
53
+ # Common sub-sanitizers (can be selected individually)
54
+ gcc -fsanitize=signed-integer-overflow,null,alignment,shift -g -o myapp myapp.c
55
+ ```
56
+
57
+ ```c
58
+ // UBSan detects signed overflow
59
+ int overflow(void) {
60
+ int x = INT_MAX;
61
+ return x + 1; // runtime error: signed integer overflow
62
+ }
63
+
64
+ // UBSan detects misaligned access
65
+ void misaligned(void) {
66
+ char buf[8] = {0};
67
+ int *p = (int *)(buf + 1); // misaligned pointer
68
+ *p = 42; // runtime error: misaligned access
69
+ }
70
+ ```
71
+
72
+ ## ThreadSanitizer (TSan)
73
+
74
+ Detects: data races, lock-order violations, deadlocks.
75
+
76
+ ```bash
77
+ # Compile with TSan (cannot combine with ASan)
78
+ gcc -fsanitize=thread -fno-omit-frame-pointer -g -o myapp myapp.c -lpthread
79
+ ```
80
+
81
+ ```c
82
+ // TSan detects this data race
83
+ static int counter = 0;
84
+
85
+ void *increment(void *arg) {
86
+ (void)arg;
87
+ for (int i = 0; i < 1000; i++) {
88
+ counter++; // data race: unsynchronized access
89
+ }
90
+ return NULL;
91
+ }
92
+
93
+ // Fix: use mutex or atomic
94
+ #include <stdatomic.h>
95
+ static atomic_int counter = 0;
96
+
97
+ void *increment_safe(void *arg) {
98
+ (void)arg;
99
+ for (int i = 0; i < 1000; i++) {
100
+ atomic_fetch_add(&counter, 1); // no race
101
+ }
102
+ return NULL;
103
+ }
104
+ ```
105
+
106
+ ## MemorySanitizer (MSan) — Clang Only
107
+
108
+ Detects: reads of uninitialized memory.
109
+
110
+ ```bash
111
+ # Clang-only (not available in GCC)
112
+ clang -fsanitize=memory -fno-omit-frame-pointer -g -O1 -o myapp myapp.c
113
+
114
+ # All linked libraries must also be compiled with MSan
115
+ # Use with libc++ compiled with MSan for best results
116
+ ```
117
+
118
+ ## GDB / LLDB Debugging Workflow
119
+
120
+ ```bash
121
+ # Compile for debugging
122
+ gcc -g -O0 -o myapp myapp.c
123
+
124
+ # GDB basics
125
+ gdb ./myapp
126
+ (gdb) break main # set breakpoint
127
+ (gdb) run # start execution
128
+ (gdb) next # step over
129
+ (gdb) step # step into
130
+ (gdb) print variable # inspect variable
131
+ (gdb) backtrace # show call stack
132
+ (gdb) watch *ptr # break when memory changes
133
+ (gdb) info threads # list threads
134
+ (gdb) thread 2 # switch to thread 2
135
+
136
+ # LLDB equivalents
137
+ lldb ./myapp
138
+ (lldb) breakpoint set -n main
139
+ (lldb) run
140
+ (lldb) thread step-over
141
+ (lldb) thread step-in
142
+ (lldb) frame variable
143
+ (lldb) thread backtrace
144
+ (lldb) watchpoint set variable counter
145
+ ```
146
+
147
+ ## Core Dump Analysis
148
+
149
+ ```bash
150
+ # Enable core dumps
151
+ ulimit -c unlimited
152
+
153
+ # Set core dump pattern (Linux)
154
+ echo "/tmp/core.%e.%p" | sudo tee /proc/sys/kernel/core_pattern
155
+
156
+ # Analyze core dump
157
+ gdb ./myapp /tmp/core.myapp.12345
158
+ (gdb) backtrace # see where it crashed
159
+ (gdb) frame 3 # examine specific frame
160
+ (gdb) info locals # see local variables
161
+ ```
162
+
163
+ ## Valgrind (Alternative to ASan)
164
+
165
+ ```bash
166
+ # Memory error detection
167
+ valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all ./myapp
168
+
169
+ # Cache profiling
170
+ valgrind --tool=cachegrind ./myapp
171
+ cg_annotate cachegrind.out.<pid>
172
+
173
+ # Call graph profiling
174
+ valgrind --tool=callgrind ./myapp
175
+ kcachegrind callgrind.out.<pid>
176
+ ```
177
+
178
+ ## Sanitizer Compatibility Matrix
179
+
180
+ | Sanitizer | GCC | Clang | Combinable with |
181
+ | ---------------- | --- | ----- | ----------------------- |
182
+ | AddressSanitizer | Yes | Yes | UBSan |
183
+ | UBSan | Yes | Yes | ASan, MSan, TSan |
184
+ | ThreadSanitizer | Yes | Yes | UBSan |
185
+ | MemorySanitizer | No | Yes | UBSan |
186
+ | LeakSanitizer | Yes | Yes | ASan (often integrated) |
187
+
188
+ Cannot combine: ASan + TSan, ASan + MSan, TSan + MSan.
189
+
190
+ ## CI Integration
191
+
192
+ ```bash
193
+ # Run multiple sanitizer builds in CI
194
+ # Build 1: ASan + UBSan
195
+ cmake -B build-asan -DCMAKE_C_FLAGS="-fsanitize=address,undefined -g -O1"
196
+ cmake --build build-asan && ctest --test-dir build-asan
197
+
198
+ # Build 2: TSan (separate because incompatible with ASan)
199
+ cmake -B build-tsan -DCMAKE_C_FLAGS="-fsanitize=thread -g -O1"
200
+ cmake --build build-tsan && ctest --test-dir build-tsan
201
+
202
+ # Build 3: Regular optimized build (for performance tests)
203
+ cmake -B build-release -DCMAKE_BUILD_TYPE=Release
204
+ cmake --build build-release && ctest --test-dir build-release
205
+ ```
@@ -0,0 +1,60 @@
1
+ # Memory Safety and Build Checklist
2
+
3
+ ## Ownership rules
4
+
5
+ - Every heap allocation has exactly one owner. Document who allocates and who frees.
6
+ - Functions that return allocated memory must document whether the caller owns the result.
7
+ - Functions that receive pointers must document whether they borrow or take ownership.
8
+ - Use naming conventions to signal ownership: `create_*` allocates (caller frees), `get_*` borrows (caller must not free).
9
+
10
+ ## Buffer safety
11
+
12
+ - Always pass buffer size alongside buffer pointer. Never rely on null terminators for binary data.
13
+ - Check return values of `snprintf`, `read`, `recv` — they may write fewer bytes than requested.
14
+ - Use `sizeof(array)` only on stack-allocated arrays, never on decayed pointers.
15
+ - Prefer bounded variants: `strnlen` over `strlen`, `snprintf` over `sprintf`, `strncpy` with explicit termination.
16
+
17
+ ## Initialization
18
+
19
+ - Initialize all stack variables at declaration. Uninitialized reads are undefined behavior.
20
+ - Zero-initialize structs with `= {0}` or `memset` before populating fields.
21
+ - Use compound literals `(struct Foo){.field = val}` for partial initialization — remaining fields are zero.
22
+
23
+ ## Arena allocator pattern
24
+
25
+ ```c
26
+ // Simple arena: bump allocator with single free at scope end
27
+ typedef struct { char *base; size_t offset; size_t capacity; } Arena;
28
+ void *arena_alloc(Arena *a, size_t size); // bump offset, return pointer
29
+ void arena_reset(Arena *a); // reset offset to 0
30
+ ```
31
+
32
+ - Allocate arena at scope entry, reset or free at scope exit. No per-object free needed.
33
+ - Useful for request-scoped or frame-scoped allocations (servers, games, parsers).
34
+
35
+ ## Sanitizer configuration
36
+
37
+ | Sanitizer | Flag | Catches |
38
+ | -------------------------- | -------------------------------- | ------------------------------------------------------------- |
39
+ | AddressSanitizer | `-fsanitize=address` | Use-after-free, buffer overflow, stack overflow, memory leaks |
40
+ | UndefinedBehaviorSanitizer | `-fsanitize=undefined` | Signed overflow, null deref, alignment, shift out of range |
41
+ | ThreadSanitizer | `-fsanitize=thread` | Data races, lock order violations |
42
+ | MemorySanitizer | `-fsanitize=memory` (Clang only) | Reads of uninitialized memory |
43
+
44
+ - Run ASAN + UBSAN together in CI debug builds. TSAN requires a separate build (incompatible with ASAN).
45
+ - Set `ASAN_OPTIONS=detect_leaks=1:halt_on_error=1` for strict leak detection.
46
+
47
+ ## Build system hygiene
48
+
49
+ - Pin compiler version in CI (e.g., `gcc-13`, `clang-17`). Document minimum required version.
50
+ - Use `-Wall -Wextra -Werror -Wpedantic -Wconversion -Wshadow` for maximum diagnostic coverage.
51
+ - Run builds on at least GCC + Clang. They catch different issues.
52
+ - Use `compile_commands.json` (CMake: `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`) for IDE and static analyzer integration.
53
+ - Enable LTO (`-flto`) for release builds. Verify with tests — LTO can expose bugs that per-TU compilation hides.
54
+
55
+ ## ABI boundary review
56
+
57
+ - Export only the minimum necessary symbols. Use `__attribute__((visibility("default")))` or export maps.
58
+ - Keep struct layouts stable across versions. Add fields at the end, never reorder.
59
+ - Use opaque pointers (`typedef struct Foo Foo;`) for types whose layout callers should not depend on.
60
+ - Version-check shared libraries at load time when ABI stability is critical.
@@ -0,0 +1,244 @@
1
+ # POSIX and Platform APIs
2
+
3
+ ## File I/O
4
+
5
+ ```c
6
+ #include <fcntl.h>
7
+ #include <unistd.h>
8
+ #include <sys/stat.h>
9
+
10
+ // Open with explicit permissions
11
+ int fd = open("data.bin", O_RDWR | O_CREAT | O_TRUNC, 0644);
12
+ if (fd == -1) {
13
+ perror("open");
14
+ return -1;
15
+ }
16
+
17
+ // Read with retry on EINTR
18
+ ssize_t safe_read(int fd, void *buf, size_t count) {
19
+ ssize_t n;
20
+ do {
21
+ n = read(fd, buf, count);
22
+ } while (n == -1 && errno == EINTR);
23
+ return n;
24
+ }
25
+
26
+ // Write all bytes (handle partial writes)
27
+ ssize_t write_all(int fd, const void *buf, size_t count) {
28
+ const uint8_t *p = buf;
29
+ size_t remaining = count;
30
+ while (remaining > 0) {
31
+ ssize_t n = write(fd, p, remaining);
32
+ if (n == -1) {
33
+ if (errno == EINTR) continue;
34
+ return -1;
35
+ }
36
+ p += n;
37
+ remaining -= n;
38
+ }
39
+ return count;
40
+ }
41
+
42
+ // Always close file descriptors
43
+ close(fd);
44
+ ```
45
+
46
+ ## Memory-Mapped I/O
47
+
48
+ ```c
49
+ #include <sys/mman.h>
50
+ #include <sys/stat.h>
51
+ #include <fcntl.h>
52
+
53
+ // Map file for read-only access
54
+ void *map_file_readonly(const char *path, size_t *out_size) {
55
+ int fd = open(path, O_RDONLY);
56
+ if (fd == -1) return NULL;
57
+
58
+ struct stat st;
59
+ if (fstat(fd, &st) == -1) { close(fd); return NULL; }
60
+
61
+ void *addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
62
+ close(fd); // fd can be closed after mmap
63
+ if (addr == MAP_FAILED) return NULL;
64
+
65
+ *out_size = st.st_size;
66
+ return addr;
67
+ }
68
+
69
+ // Clean up
70
+ munmap(addr, size);
71
+ ```
72
+
73
+ ## Signal Handling
74
+
75
+ ```c
76
+ #include <signal.h>
77
+ #include <stdatomic.h>
78
+
79
+ // Use sig_atomic_t or atomics for signal flags
80
+ static volatile sig_atomic_t shutdown_requested = 0;
81
+
82
+ void handle_signal(int sig) {
83
+ (void)sig;
84
+ shutdown_requested = 1;
85
+ // ONLY async-signal-safe functions are allowed here:
86
+ // write(), _exit(), signal(), abort()
87
+ // DO NOT use printf, malloc, free, etc.
88
+ }
89
+
90
+ void setup_signals(void) {
91
+ struct sigaction sa = {
92
+ .sa_handler = handle_signal,
93
+ .sa_flags = 0,
94
+ };
95
+ sigemptyset(&sa.sa_mask);
96
+ sigaction(SIGTERM, &sa, NULL);
97
+ sigaction(SIGINT, &sa, NULL);
98
+ }
99
+
100
+ // Main loop checks the flag
101
+ while (!shutdown_requested) {
102
+ process_next_item();
103
+ }
104
+ ```
105
+
106
+ ## Process and Fork
107
+
108
+ ```c
109
+ #include <sys/wait.h>
110
+
111
+ // Fork with proper error handling
112
+ pid_t pid = fork();
113
+ if (pid == -1) {
114
+ perror("fork");
115
+ exit(EXIT_FAILURE);
116
+ } else if (pid == 0) {
117
+ // Child process
118
+ execvp(argv[0], argv); // replaces child image
119
+ perror("exec"); // only reached on error
120
+ _exit(127); // use _exit, not exit, in child after fork
121
+ } else {
122
+ // Parent process
123
+ int status;
124
+ waitpid(pid, &status, 0);
125
+ if (WIFEXITED(status)) {
126
+ int exit_code = WEXITSTATUS(status);
127
+ }
128
+ }
129
+ ```
130
+
131
+ ## Socket Programming
132
+
133
+ ```c
134
+ #include <sys/socket.h>
135
+ #include <netinet/in.h>
136
+ #include <arpa/inet.h>
137
+
138
+ // TCP server setup
139
+ int create_server(uint16_t port) {
140
+ int fd = socket(AF_INET, SOCK_STREAM, 0);
141
+ if (fd == -1) return -1;
142
+
143
+ int opt = 1;
144
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
145
+
146
+ struct sockaddr_in addr = {
147
+ .sin_family = AF_INET,
148
+ .sin_port = htons(port),
149
+ .sin_addr.s_addr = INADDR_ANY,
150
+ };
151
+
152
+ if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
153
+ close(fd);
154
+ return -1;
155
+ }
156
+
157
+ if (listen(fd, SOMAXCONN) == -1) {
158
+ close(fd);
159
+ return -1;
160
+ }
161
+
162
+ return fd;
163
+ }
164
+ ```
165
+
166
+ ## Platform Abstraction Layer Pattern
167
+
168
+ ```c
169
+ // platform.h — unified interface
170
+ #pragma once
171
+
172
+ typedef struct platform_file platform_file;
173
+
174
+ platform_file *platform_open(const char *path, int flags);
175
+ ssize_t platform_read(platform_file *f, void *buf, size_t count);
176
+ void platform_close(platform_file *f);
177
+ uint64_t platform_monotonic_time_ns(void);
178
+
179
+ // platform_posix.c — POSIX implementation
180
+ #if defined(__linux__) || defined(__APPLE__)
181
+ #include <time.h>
182
+
183
+ uint64_t platform_monotonic_time_ns(void) {
184
+ struct timespec ts;
185
+ clock_gettime(CLOCK_MONOTONIC, &ts);
186
+ return (uint64_t)ts.tv_sec * 1000000000ULL + ts.tv_nsec;
187
+ }
188
+ #endif
189
+
190
+ // platform_win32.c — Windows implementation
191
+ #if defined(_WIN32)
192
+ #include <windows.h>
193
+
194
+ uint64_t platform_monotonic_time_ns(void) {
195
+ LARGE_INTEGER freq, count;
196
+ QueryPerformanceFrequency(&freq);
197
+ QueryPerformanceCounter(&count);
198
+ return (uint64_t)(count.QuadPart * 1000000000LL / freq.QuadPart);
199
+ }
200
+ #endif
201
+ ```
202
+
203
+ ## Error Handling Patterns
204
+
205
+ ```c
206
+ // Return error codes with context struct
207
+ typedef struct {
208
+ int code;
209
+ const char *message;
210
+ const char *file;
211
+ int line;
212
+ } error_t;
213
+
214
+ #define ERROR(code, msg) ((error_t){ (code), (msg), __FILE__, __LINE__ })
215
+ #define OK ((error_t){ 0, NULL, NULL, 0 })
216
+
217
+ error_t process_file(const char *path) {
218
+ int fd = open(path, O_RDONLY);
219
+ if (fd == -1) return ERROR(errno, "failed to open file");
220
+
221
+ // ...
222
+ close(fd);
223
+ return OK;
224
+ }
225
+
226
+ // Goto-based cleanup (idiomatic C error handling)
227
+ int init_system(void) {
228
+ int ret = -1;
229
+ void *buf = malloc(BUF_SIZE);
230
+ if (!buf) goto out;
231
+
232
+ int fd = open("config", O_RDONLY);
233
+ if (fd == -1) goto cleanup_buf;
234
+
235
+ // ... use buf and fd ...
236
+ ret = 0;
237
+
238
+ close(fd);
239
+ cleanup_buf:
240
+ free(buf);
241
+ out:
242
+ return ret;
243
+ }
244
+ ```