@cubis/foundry 0.3.71 → 0.3.73
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/CHANGELOG.md +23 -2
- package/dist/cli/core.js +9 -22
- package/dist/cli/core.js.map +1 -1
- package/package.json +1 -1
- package/src/cli/core.ts +13 -22
- package/workflows/powers/accessibility/POWER.md +83 -94
- package/workflows/powers/accessibility/SKILL.md +82 -94
- package/workflows/powers/agent-design/POWER.md +201 -0
- package/workflows/powers/agent-design/SKILL.md +198 -0
- package/workflows/powers/agent-design/references/clarification-patterns.md +153 -0
- package/workflows/powers/agent-design/references/skill-testing.md +164 -0
- package/workflows/powers/agent-design/references/workflow-patterns.md +226 -0
- package/workflows/powers/agentic-eval/POWER.md +62 -0
- package/workflows/powers/agentic-eval/SKILL.md +59 -0
- package/workflows/powers/agentic-eval/references/rubric-and-regression-checklist.md +11 -0
- package/workflows/powers/api-designer/POWER.md +43 -71
- package/workflows/powers/api-designer/SKILL.md +43 -71
- package/workflows/powers/api-patterns/POWER.md +42 -56
- package/workflows/powers/api-patterns/SKILL.md +42 -57
- package/workflows/powers/architecture-designer/POWER.md +43 -60
- package/workflows/powers/architecture-designer/SKILL.md +43 -60
- package/workflows/powers/ask-questions-if-underspecified/POWER.md +51 -3
- package/workflows/powers/auth-architect/POWER.md +69 -0
- package/workflows/powers/auth-architect/SKILL.md +66 -0
- package/workflows/powers/auth-architect/references/session-token-policy-checklist.md +45 -0
- package/workflows/powers/behavioral-modes/POWER.md +100 -9
- package/workflows/powers/c-pro/POWER.md +105 -0
- package/workflows/powers/c-pro/SKILL.md +102 -0
- package/workflows/powers/c-pro/references/build-systems-and-toolchains.md +148 -0
- package/workflows/powers/c-pro/references/common-ub-and-portability.md +166 -0
- package/workflows/powers/c-pro/references/debugging-with-sanitizers.md +205 -0
- package/workflows/powers/c-pro/references/memory-safety-and-build-checklist.md +60 -0
- package/workflows/powers/c-pro/references/posix-and-platform-apis.md +244 -0
- package/workflows/powers/changelog-generator/POWER.md +127 -63
- package/workflows/powers/changelog-generator/SKILL.md +126 -63
- package/workflows/powers/ci-cd-pipelines/POWER.md +156 -0
- package/workflows/powers/ci-cd-pipelines/SKILL.md +153 -0
- package/workflows/powers/ci-cd-pipelines/references/github-actions-patterns.md +160 -0
- package/workflows/powers/ci-cd-pipelines/references/pipeline-security-checklist.md +57 -0
- package/workflows/powers/cli-developer/POWER.md +152 -95
- package/workflows/powers/cli-developer/SKILL.md +152 -95
- package/workflows/powers/cpp-pro/POWER.md +111 -0
- package/workflows/powers/cpp-pro/SKILL.md +108 -0
- package/workflows/powers/cpp-pro/references/concurrency-primitives.md +266 -0
- package/workflows/powers/cpp-pro/references/move-semantics-and-value-types.md +149 -0
- package/workflows/powers/cpp-pro/references/performance-and-profiling.md +191 -0
- package/workflows/powers/cpp-pro/references/raii-and-modern-cpp-checklist.md +87 -0
- package/workflows/powers/cpp-pro/references/template-and-concepts-patterns.md +205 -0
- package/workflows/powers/csharp-pro/POWER.md +47 -22
- package/workflows/powers/csharp-pro/SKILL.md +47 -22
- package/workflows/powers/dart-pro/POWER.md +68 -0
- package/workflows/powers/dart-pro/SKILL.md +65 -0
- package/workflows/powers/dart-pro/references/isolate-and-concurrency.md +180 -0
- package/workflows/powers/dart-pro/references/null-safety-and-async-patterns.md +133 -0
- package/workflows/powers/dart-pro/references/package-structure-and-linting.md +193 -0
- package/workflows/powers/dart-pro/references/sealed-records-patterns.md +173 -0
- package/workflows/powers/dart-pro/references/testing-and-mocking.md +235 -0
- package/workflows/powers/database-design/POWER.md +47 -33
- package/workflows/powers/database-design/SKILL.md +47 -33
- package/workflows/powers/database-optimizer/POWER.md +43 -64
- package/workflows/powers/database-optimizer/SKILL.md +43 -64
- package/workflows/powers/database-skills/POWER.md +59 -93
- package/workflows/powers/database-skills/SKILL.md +59 -93
- package/workflows/powers/debugging-strategies/POWER.md +69 -0
- package/workflows/powers/debugging-strategies/SKILL.md +66 -0
- package/workflows/powers/debugging-strategies/references/reproduce-isolate-verify-checklist.md +42 -0
- package/workflows/powers/deep-research/POWER.md +67 -0
- package/workflows/powers/deep-research/SKILL.md +64 -0
- package/workflows/powers/deep-research/references/multi-round-research-loop.md +80 -0
- package/workflows/powers/design-system-builder/POWER.md +130 -116
- package/workflows/powers/design-system-builder/SKILL.md +130 -116
- package/workflows/powers/devops-engineer/POWER.md +120 -57
- package/workflows/powers/devops-engineer/SKILL.md +120 -57
- package/workflows/powers/docker-kubernetes/POWER.md +94 -0
- package/workflows/powers/docker-kubernetes/SKILL.md +91 -0
- package/workflows/powers/docker-kubernetes/references/dockerfile-optimization-checklist.md +35 -0
- package/workflows/powers/docker-kubernetes/references/kubernetes-deployment-patterns.md +59 -0
- package/workflows/powers/documentation-templates/POWER.md +158 -127
- package/workflows/powers/documentation-templates/SKILL.md +158 -127
- package/workflows/powers/drizzle-expert/POWER.md +66 -0
- package/workflows/powers/drizzle-expert/SKILL.md +63 -0
- package/workflows/powers/drizzle-expert/references/runtime-pairing-matrix.md +16 -0
- package/workflows/powers/drizzle-expert/references/schema-and-migration-playbook.md +18 -0
- package/workflows/powers/error-ux-observability/POWER.md +144 -131
- package/workflows/powers/error-ux-observability/SKILL.md +143 -131
- package/workflows/powers/fastapi-expert/POWER.md +46 -60
- package/workflows/powers/fastapi-expert/SKILL.md +46 -60
- package/workflows/powers/firebase/POWER.md +65 -0
- package/workflows/powers/firebase/SKILL.md +62 -0
- package/workflows/powers/firebase/references/platform-routing.md +16 -0
- package/workflows/powers/firebase/references/rules-and-indexes-checklist.md +11 -0
- package/workflows/powers/flutter-design-system/POWER.md +63 -0
- package/workflows/powers/flutter-design-system/SKILL.md +60 -0
- package/workflows/powers/flutter-design-system/references/shared-widgets.md +29 -0
- package/workflows/powers/flutter-design-system/references/tokens-and-theme.md +34 -0
- package/workflows/powers/flutter-drift/POWER.md +65 -0
- package/workflows/powers/flutter-drift/SKILL.md +62 -0
- package/workflows/powers/flutter-drift/references/migrations.md +22 -0
- package/workflows/powers/flutter-drift/references/query-patterns.md +26 -0
- package/workflows/powers/flutter-feature/POWER.md +65 -0
- package/workflows/powers/flutter-feature/SKILL.md +62 -0
- package/workflows/powers/flutter-feature/references/architecture-rules.md +85 -0
- package/workflows/powers/flutter-feature/references/composite-provider.md +58 -0
- package/workflows/powers/flutter-feature/references/outbox-pattern.md +87 -0
- package/workflows/powers/flutter-feature/references/testing-patterns.md +218 -0
- package/workflows/powers/flutter-go-router/POWER.md +64 -0
- package/workflows/powers/flutter-go-router/SKILL.md +61 -0
- package/workflows/powers/flutter-go-router/references/guards-and-deeplinks.md +20 -0
- package/workflows/powers/flutter-go-router/references/typed-routes.md +27 -0
- package/workflows/powers/flutter-offline-sync/POWER.md +62 -0
- package/workflows/powers/flutter-offline-sync/SKILL.md +59 -0
- package/workflows/powers/flutter-offline-sync/references/outbox-full.md +44 -0
- package/workflows/powers/flutter-repository/POWER.md +64 -0
- package/workflows/powers/flutter-repository/SKILL.md +61 -0
- package/workflows/powers/flutter-repository/references/drift-patterns.md +21 -0
- package/workflows/powers/flutter-repository/references/retrofit-patterns.md +20 -0
- package/workflows/powers/flutter-riverpod/POWER.md +70 -0
- package/workflows/powers/flutter-riverpod/SKILL.md +67 -0
- package/workflows/powers/flutter-riverpod/references/async-and-mutations.md +19 -0
- package/workflows/powers/flutter-riverpod/references/async-lifecycle.md +19 -0
- package/workflows/powers/flutter-riverpod/references/provider-selection.md +20 -0
- package/workflows/powers/flutter-riverpod/references/testing.md +21 -0
- package/workflows/powers/flutter-riverpod/references/version-matrix.md +24 -0
- package/workflows/powers/flutter-state-machine/POWER.md +62 -0
- package/workflows/powers/flutter-state-machine/SKILL.md +59 -0
- package/workflows/powers/flutter-state-machine/references/app-state-contract.md +23 -0
- package/workflows/powers/flutter-state-machine/references/ui-rendering.md +14 -0
- package/workflows/powers/flutter-testing/POWER.md +64 -0
- package/workflows/powers/flutter-testing/SKILL.md +61 -0
- package/workflows/powers/flutter-testing/references/offline-sync-tests.md +16 -0
- package/workflows/powers/flutter-testing/references/test-layers.md +33 -0
- package/workflows/powers/frontend-code-review/POWER.md +137 -0
- package/workflows/powers/frontend-code-review/SKILL.md +134 -0
- package/workflows/powers/frontend-code-review/references/common-antipatterns.md +86 -0
- package/workflows/powers/frontend-code-review/references/performance-budgets.md +56 -0
- package/workflows/powers/frontend-code-review/references/review-checklists.md +47 -0
- package/workflows/powers/frontend-design/POWER.md +163 -362
- package/workflows/powers/frontend-design/SKILL.md +163 -362
- package/workflows/powers/game-development/POWER.md +57 -140
- package/workflows/powers/game-development/SKILL.md +57 -140
- package/workflows/powers/geo-fundamentals/POWER.md +64 -126
- package/workflows/powers/geo-fundamentals/SKILL.md +64 -127
- package/workflows/powers/git-workflow/POWER.md +135 -0
- package/workflows/powers/git-workflow/SKILL.md +132 -0
- package/workflows/powers/git-workflow/references/pr-review-checklist.md +63 -0
- package/workflows/powers/golang-pro/POWER.md +46 -35
- package/workflows/powers/golang-pro/SKILL.md +46 -35
- package/workflows/powers/graphql-architect/POWER.md +44 -62
- package/workflows/powers/graphql-architect/SKILL.md +44 -62
- package/workflows/powers/i18n-localization/POWER.md +118 -103
- package/workflows/powers/i18n-localization/SKILL.md +118 -103
- package/workflows/powers/java-pro/POWER.md +47 -22
- package/workflows/powers/java-pro/SKILL.md +47 -22
- package/workflows/powers/javascript-pro/POWER.md +47 -34
- package/workflows/powers/javascript-pro/SKILL.md +47 -34
- package/workflows/powers/kotlin-pro/POWER.md +46 -23
- package/workflows/powers/kotlin-pro/SKILL.md +46 -23
- package/workflows/powers/legacy-modernizer/POWER.md +43 -60
- package/workflows/powers/legacy-modernizer/SKILL.md +43 -60
- package/workflows/powers/mcp-builder/POWER.md +65 -0
- package/workflows/powers/mcp-builder/SKILL.md +62 -0
- package/workflows/powers/mcp-builder/references/testing-and-evals.md +17 -0
- package/workflows/powers/mcp-builder/references/transport-and-tool-design.md +17 -0
- package/workflows/powers/microservices-architect/POWER.md +43 -70
- package/workflows/powers/microservices-architect/SKILL.md +43 -70
- package/workflows/powers/mobile-design/POWER.md +110 -345
- package/workflows/powers/mobile-design/SKILL.md +110 -345
- package/workflows/powers/mongodb/POWER.md +67 -0
- package/workflows/powers/mongodb/SKILL.md +64 -0
- package/workflows/powers/mongodb/references/mongodb-checklist.md +20 -0
- package/workflows/powers/mysql/POWER.md +67 -0
- package/workflows/powers/mysql/SKILL.md +64 -0
- package/workflows/powers/mysql/references/mysql-checklist.md +20 -0
- package/workflows/powers/neki/POWER.md +67 -0
- package/workflows/powers/neki/SKILL.md +64 -0
- package/workflows/powers/neki/references/neki-checklist.md +18 -0
- package/workflows/powers/nestjs-expert/POWER.md +45 -91
- package/workflows/powers/nestjs-expert/SKILL.md +45 -91
- package/workflows/powers/nextjs-developer/POWER.md +51 -44
- package/workflows/powers/nextjs-developer/SKILL.md +51 -44
- package/workflows/powers/nodejs-best-practices/POWER.md +48 -29
- package/workflows/powers/nodejs-best-practices/SKILL.md +48 -29
- package/workflows/powers/observability/POWER.md +109 -0
- package/workflows/powers/observability/SKILL.md +106 -0
- package/workflows/powers/observability/references/alerting-and-slo-checklist.md +87 -0
- package/workflows/powers/observability/references/opentelemetry-setup-guide.md +121 -0
- package/workflows/powers/openai-docs/POWER.md +61 -0
- package/workflows/powers/openai-docs/SKILL.md +58 -0
- package/workflows/powers/openai-docs/references/official-source-playbook.md +10 -0
- package/workflows/powers/performance-profiling/POWER.md +61 -114
- package/workflows/powers/performance-profiling/SKILL.md +61 -114
- package/workflows/powers/php-pro/POWER.md +116 -0
- package/workflows/powers/php-pro/SKILL.md +113 -0
- package/workflows/powers/php-pro/references/architecture-and-di.md +239 -0
- package/workflows/powers/php-pro/references/modern-php-features.md +189 -0
- package/workflows/powers/php-pro/references/performance-and-deployment.md +197 -0
- package/workflows/powers/php-pro/references/php84-strict-typing-checklist.md +161 -0
- package/workflows/powers/php-pro/references/testing-and-static-analysis.md +235 -0
- package/workflows/powers/playwright-e2e/POWER.md +85 -0
- package/workflows/powers/playwright-e2e/SKILL.md +82 -0
- package/workflows/powers/playwright-e2e/references/locator-trace-flake-checklist.md +80 -0
- package/workflows/powers/postgres/POWER.md +67 -0
- package/workflows/powers/postgres/SKILL.md +64 -0
- package/workflows/powers/postgres/references/postgres-checklist.md +20 -0
- package/workflows/powers/prompt-engineer/POWER.md +47 -30
- package/workflows/powers/prompt-engineer/SKILL.md +47 -30
- package/workflows/powers/python-pro/POWER.md +47 -36
- package/workflows/powers/python-pro/SKILL.md +47 -36
- package/workflows/powers/react-best-practices/POWER.md +56 -33
- package/workflows/powers/react-best-practices/SKILL.md +56 -33
- package/workflows/powers/react-expert/POWER.md +47 -37
- package/workflows/powers/react-expert/SKILL.md +47 -37
- package/workflows/powers/redis/POWER.md +67 -0
- package/workflows/powers/redis/SKILL.md +64 -0
- package/workflows/powers/redis/references/redis-checklist.md +19 -0
- package/workflows/powers/ruby-pro/POWER.md +118 -0
- package/workflows/powers/ruby-pro/SKILL.md +115 -0
- package/workflows/powers/ruby-pro/references/modern-ruby-features.md +189 -0
- package/workflows/powers/ruby-pro/references/object-design-patterns.md +220 -0
- package/workflows/powers/ruby-pro/references/performance-and-profiling.md +224 -0
- package/workflows/powers/ruby-pro/references/ruby-concurrency-and-testing.md +190 -0
- package/workflows/powers/ruby-pro/references/testing-and-rspec.md +236 -0
- package/workflows/powers/rust-pro/POWER.md +45 -31
- package/workflows/powers/rust-pro/SKILL.md +45 -31
- package/workflows/powers/security-engineer/POWER.md +129 -0
- package/workflows/powers/security-engineer/SKILL.md +126 -0
- package/workflows/powers/seo-fundamentals/POWER.md +59 -102
- package/workflows/powers/seo-fundamentals/SKILL.md +59 -102
- package/workflows/powers/serverless-patterns/POWER.md +171 -0
- package/workflows/powers/serverless-patterns/SKILL.md +168 -0
- package/workflows/powers/skill-creator/POWER.md +90 -0
- package/workflows/powers/skill-creator/SKILL.md +87 -0
- package/workflows/powers/skill-creator/references/platform-formats.md +181 -0
- package/workflows/powers/skill-creator/references/schemas.md +430 -0
- package/workflows/powers/spec-miner/POWER.md +49 -57
- package/workflows/powers/spec-miner/SKILL.md +49 -57
- package/workflows/powers/sqlite/POWER.md +67 -0
- package/workflows/powers/sqlite/SKILL.md +64 -0
- package/workflows/powers/sqlite/references/sqlite-checklist.md +19 -0
- package/workflows/powers/sre-engineer/POWER.md +123 -64
- package/workflows/powers/sre-engineer/SKILL.md +123 -64
- package/workflows/powers/static-analysis/POWER.md +121 -77
- package/workflows/powers/static-analysis/SKILL.md +121 -77
- package/workflows/powers/stripe-best-practices/POWER.md +140 -17
- package/workflows/powers/stripe-best-practices/SKILL.md +139 -17
- package/workflows/powers/supabase/POWER.md +67 -0
- package/workflows/powers/supabase/SKILL.md +64 -0
- package/workflows/powers/supabase/references/supabase-checklist.md +19 -0
- package/workflows/powers/swift-pro/POWER.md +118 -0
- package/workflows/powers/swift-pro/SKILL.md +115 -0
- package/workflows/powers/swift-pro/references/concurrency-patterns.md +165 -0
- package/workflows/powers/swift-pro/references/protocol-and-generics.md +172 -0
- package/workflows/powers/swift-pro/references/sendable-and-isolation.md +116 -0
- package/workflows/powers/swift-pro/references/swift-concurrency-and-protocols.md +260 -0
- package/workflows/powers/swift-pro/references/testing-and-packages.md +192 -0
- package/workflows/powers/tailwind-patterns/POWER.md +71 -240
- package/workflows/powers/tailwind-patterns/SKILL.md +71 -240
- package/workflows/powers/testing-patterns/POWER.md +155 -10
- package/workflows/powers/testing-patterns/SKILL.md +155 -10
- package/workflows/powers/typescript-pro/POWER.md +47 -38
- package/workflows/powers/typescript-pro/SKILL.md +47 -38
- package/workflows/powers/vitess/POWER.md +67 -0
- package/workflows/powers/vitess/SKILL.md +64 -0
- package/workflows/powers/vitess/references/vitess-checklist.md +19 -0
- package/workflows/powers/vulnerability-scanner/POWER.md +146 -10
- package/workflows/powers/vulnerability-scanner/SKILL.md +146 -10
- package/workflows/powers/web-perf/POWER.md +43 -170
- package/workflows/powers/web-perf/SKILL.md +43 -170
- package/workflows/powers/webapp-testing/POWER.md +43 -164
- package/workflows/powers/webapp-testing/SKILL.md +43 -164
- package/workflows/workflows/agent-environment-setup/platforms/antigravity/rules/GEMINI.md +65 -42
- package/workflows/workflows/agent-environment-setup/platforms/claude/rules/CLAUDE.md +8 -6
- package/workflows/workflows/agent-environment-setup/platforms/codex/rules/AGENTS.md +65 -41
- package/workflows/workflows/agent-environment-setup/platforms/copilot/rules/copilot-instructions.md +8 -6
- package/workflows/workflows/agent-environment-setup/shared/rules/STEERING.md +9 -8
- package/workflows/workflows/agent-environment-setup/shared/rules/overrides/codex.md +1 -1
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# Build Systems and Toolchains
|
|
2
|
+
|
|
3
|
+
## CMake Modern Practices
|
|
4
|
+
|
|
5
|
+
```cmake
|
|
6
|
+
# CMakeLists.txt — modern CMake (3.21+)
|
|
7
|
+
cmake_minimum_required(VERSION 3.21)
|
|
8
|
+
project(mylib VERSION 1.0.0 LANGUAGES C)
|
|
9
|
+
|
|
10
|
+
# Set C standard project-wide
|
|
11
|
+
set(CMAKE_C_STANDARD 23)
|
|
12
|
+
set(CMAKE_C_STANDARD_REQUIRED ON)
|
|
13
|
+
set(CMAKE_C_EXTENSIONS OFF) # disable GNU extensions for portability
|
|
14
|
+
|
|
15
|
+
# Library target
|
|
16
|
+
add_library(mylib
|
|
17
|
+
src/core.c
|
|
18
|
+
src/parser.c
|
|
19
|
+
src/util.c
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
target_include_directories(mylib
|
|
23
|
+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
|
24
|
+
PRIVATE src/
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
# Compiler warnings as errors
|
|
28
|
+
target_compile_options(mylib PRIVATE
|
|
29
|
+
$<$<C_COMPILER_ID:GNU,Clang>:-Wall -Wextra -Wpedantic -Werror>
|
|
30
|
+
$<$<C_COMPILER_ID:MSVC>:/W4 /WX>
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# Sanitizers for debug builds
|
|
34
|
+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
|
35
|
+
target_compile_options(mylib PRIVATE -fsanitize=address,undefined -fno-omit-frame-pointer)
|
|
36
|
+
target_link_options(mylib PRIVATE -fsanitize=address,undefined)
|
|
37
|
+
endif()
|
|
38
|
+
|
|
39
|
+
# Tests
|
|
40
|
+
enable_testing()
|
|
41
|
+
add_executable(test_core tests/test_core.c)
|
|
42
|
+
target_link_libraries(test_core PRIVATE mylib)
|
|
43
|
+
add_test(NAME test_core COMMAND test_core)
|
|
44
|
+
|
|
45
|
+
# Install rules
|
|
46
|
+
install(TARGETS mylib EXPORT mylibTargets)
|
|
47
|
+
install(DIRECTORY include/ DESTINATION include)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Meson Build System
|
|
51
|
+
|
|
52
|
+
```meson
|
|
53
|
+
# meson.build — lightweight alternative to CMake
|
|
54
|
+
project('mylib', 'c',
|
|
55
|
+
version: '1.0.0',
|
|
56
|
+
default_options: ['c_std=c23', 'warning_level=3', 'werror=true']
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
src = files('src/core.c', 'src/parser.c', 'src/util.c')
|
|
60
|
+
inc = include_directories('include')
|
|
61
|
+
|
|
62
|
+
mylib = library('mylib', src, include_directories: inc)
|
|
63
|
+
mylib_dep = declare_dependency(link_with: mylib, include_directories: inc)
|
|
64
|
+
|
|
65
|
+
# Tests
|
|
66
|
+
test_core = executable('test_core', 'tests/test_core.c', dependencies: mylib_dep)
|
|
67
|
+
test('core', test_core)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Cross-Compilation
|
|
71
|
+
|
|
72
|
+
```cmake
|
|
73
|
+
# toolchain-arm.cmake — cross-compile for ARM
|
|
74
|
+
set(CMAKE_SYSTEM_NAME Linux)
|
|
75
|
+
set(CMAKE_SYSTEM_PROCESSOR arm)
|
|
76
|
+
|
|
77
|
+
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
|
|
78
|
+
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
|
|
79
|
+
|
|
80
|
+
set(CMAKE_FIND_ROOT_PATH /usr/arm-linux-gnueabihf)
|
|
81
|
+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
|
82
|
+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
|
83
|
+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
# Build with cross-compilation toolchain
|
|
88
|
+
cmake -B build-arm -DCMAKE_TOOLCHAIN_FILE=toolchain-arm.cmake
|
|
89
|
+
cmake --build build-arm
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## CI Matrix Configuration
|
|
93
|
+
|
|
94
|
+
```yaml
|
|
95
|
+
# GitHub Actions — multi-compiler, multi-platform
|
|
96
|
+
jobs:
|
|
97
|
+
build:
|
|
98
|
+
strategy:
|
|
99
|
+
matrix:
|
|
100
|
+
os: [ubuntu-latest, macos-latest]
|
|
101
|
+
compiler: [gcc-13, clang-17]
|
|
102
|
+
runs-on: ${{ matrix.os }}
|
|
103
|
+
steps:
|
|
104
|
+
- uses: actions/checkout@v4
|
|
105
|
+
- name: Configure
|
|
106
|
+
run: |
|
|
107
|
+
cmake -B build \
|
|
108
|
+
-DCMAKE_C_COMPILER=${{ matrix.compiler }} \
|
|
109
|
+
-DCMAKE_BUILD_TYPE=Debug
|
|
110
|
+
- name: Build
|
|
111
|
+
run: cmake --build build
|
|
112
|
+
- name: Test
|
|
113
|
+
run: ctest --test-dir build --output-on-failure
|
|
114
|
+
- name: Sanitizer build
|
|
115
|
+
run: |
|
|
116
|
+
cmake -B build-san \
|
|
117
|
+
-DCMAKE_C_COMPILER=${{ matrix.compiler }} \
|
|
118
|
+
-DCMAKE_BUILD_TYPE=Debug \
|
|
119
|
+
-DENABLE_SANITIZERS=ON
|
|
120
|
+
cmake --build build-san
|
|
121
|
+
ctest --test-dir build-san
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Dependency Management
|
|
125
|
+
|
|
126
|
+
```cmake
|
|
127
|
+
# FetchContent for pinned dependencies
|
|
128
|
+
include(FetchContent)
|
|
129
|
+
|
|
130
|
+
FetchContent_Declare(cjson
|
|
131
|
+
GIT_REPOSITORY https://github.com/DaveGamble/cJSON.git
|
|
132
|
+
GIT_TAG v1.7.17 # pin exact version
|
|
133
|
+
)
|
|
134
|
+
FetchContent_MakeAvailable(cjson)
|
|
135
|
+
target_link_libraries(mylib PRIVATE cjson)
|
|
136
|
+
|
|
137
|
+
# vcpkg for larger dependency sets
|
|
138
|
+
# Install: cmake -B build -DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Reproducible Builds Checklist
|
|
142
|
+
|
|
143
|
+
1. Pin compiler version in CI (e.g., `gcc-13`, not `gcc`).
|
|
144
|
+
2. Pin all external dependencies to exact versions or commit hashes.
|
|
145
|
+
3. Use deterministic build flags (`-frandom-seed=`, `-ffile-prefix-map=`).
|
|
146
|
+
4. Commit `CMakePresets.json` or equivalent for consistent local/CI builds.
|
|
147
|
+
5. Disable compiler plugins and extensions that vary by environment.
|
|
148
|
+
6. Verify with two independent builds and `diffoscope` or binary hash comparison.
|
|
@@ -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.
|