lang-c
暂无简介
- License 许可
- MIT
- 所属仓库
- poppyTM/oride
- 最近更新
- 2026-09-30
- Stars
- ★ 1
- Forks
- 0
02 资源文件 6
- 参考文档checks/bounded-apis.md518 B
- 其他examples/safe-buffer.c3.2 KB
- 参考文档manifest.json1.8 KB
- 参考文档references/c-memory-safety.md3.1 KB
- 脚本scripts/verify.sh2.8 KB
- 参考文档templates/c-verification-report.md345 B
03 SKILL.md 正文
ISO C Systems Programming & Memory Safety Contract
1. Purpose
Author, refactor, and audit systems software written in modern ISO C (C11, C17, or C23). This skill mandates strict bounded buffer operations, elimination of banned and dangerous standard library functions, pointer provenance discipline, zero unchecked allocations, automatic scope cleanup via __attribute__((cleanup)) where supported, zero-warning compilation with -Werror, and runtime verification with AddressSanitizer (ASan) and UndefinedBehaviorSanitizer (UBSan).
2. Use When
- Developing low-level systems libraries, operating system interfaces, embedded modules, or runtime runloops in C.
- Authoring C-ABI foreign function interface (FFI) bindings for Go, Rust, Python, or WebAssembly.
- Writing high-throughput data processing algorithms requiring raw memory access with zero compiler-induced overhead.
- Auditing legacy C codebases for memory corruption vulnerabilities, buffer overflows, or integer wrap-around defects.
- Operating in mode(s):
implementation,review,testing,audit.
3. Do Not Use When
- Developing in C++ with RAII, templates, and classes (use
lang-cpp). - Writing memory-safe systems code where Rust's compiler borrow checker is available and mandated (use
lang-rust). - Working purely on web frontend UI or scripting environments.
4. Required Context
Before implementing or modifying C code, determine:
- Standard Dialect: ISO C11 (
-std=c11), C17 (-std=c17), or C23 (-std=c2x/-std=c23). - Platform ABI & Alignment Constraints: Data model (LP64 on 64-bit Linux/macOS vs LLP64 on Windows), pointer size, and structure alignment requirements.
- Buffer Safety Protocol: Use of explicit
(void *buf, size_t cap, size_t *out_len)parameter tuples or structured slice types (struct BufferSlice { uint8_t *data; size_t len; size_t cap; }). - Fortification & Compiler Hardening: Presence of
_FORTIFY_SOURCE=2or_FORTIFY_SOURCE=3, stack canaries (-fstack-protector-strong).
5. Procedure
[API Design & Type Definition]
|
v
[1. Buffer Tuple Specification] ----> (ptr, cap, out_len) mandatory on all writes
|
v
[2. Dangerous API Ban & Scan] -------> Eradicate gets, strcpy, strcat, sprintf
|
v
[3. Allocation & Pointer Discipline]-> Immediate NULL checks, zero after free
|
v
[4. Static Analysis & Fortify] ------> -Wall -Wextra -Wpedantic -Werror -D_FORTIFY_SOURCE=3
|
v
[5. Dynamic ASan/UBSan Gauntlet] ----> Zero leaks, zero out-of-bounds, zero UB
Step 1: Bounded Buffer Operations
- Every function accepting or populating a buffer must accept explicit capacity constraints:
int safe_copy_string(char *dest, size_t dest_cap, const char *src); - Use structured buffer descriptors for binary I/O:
typedef struct { uint8_t *data; size_t len; size_t cap; } ByteBuffer; - Variable Length Arrays (VLAs, e.g.
char buf[n]) are strictly prohibited. Use fixed-size stack buffers with overflow checks, or heap/arena allocation.
Step 2: Elimination of Banned APIs
The following legacy C standard library functions are strictly forbidden:
gets()$\rightarrow$ usefgets()with bounded length.strcpy(),strcat()$\rightarrow$ usesnprintf()or explicit bounded loops with null termination.sprintf(),vsprintf()$\rightarrow$ usesnprintf(),vsnprintf().atoi(),atol()$\rightarrow$ usestrtol(),strtoll()withendptranderrnovalidation.
Step 3: Allocation Safety & Lifecycle Hygiene
- Check every allocation immediately:
void *ptr = malloc(size); if (ptr == NULL) { return ERR_OUT_OF_MEMORY; } - Free and nullify:
free(ptr); ptr = NULL; // Prevent Use-After-Free - When targeting GCC or Clang on Linux/POSIX, utilize
__attribute__((cleanup))to implement deterministic RAII-like scope cleanups for dynamic resources.
Step 4: Strict Aliasing & Provenance Invariants
- Never cast between incompatible pointer types (e.g.,
float *toint *). - To inspect or reinterpret binary representation, use
memcpy()or copy intounsigned char *:uint32_t raw_bits; memcpy(&raw_bits, &float_val, sizeof(raw_bits)); - Obey pointer provenance: arithmetic on a pointer must remain within the bounds of the original allocated object.
Step 5: Static Hardening & Dynamic Sanitizers
- Compile with strict flags:
-std=c17 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wformat=2 -Wstrict-prototypes -Wmissing-prototypes -Werror -D_FORTIFY_SOURCE=3 -fstack-protector-strong. - Compile and run the test suite under AddressSanitizer and UndefinedBehaviorSanitizer:
-fsanitize=address,undefined -fno-omit-frame-pointer.
6. Decision Rules & Invariants
- RULE 1 (Explicit Buffer Bounds): Any function writing to a pointer without receiving an explicit capacity parameter is rejected immediately.
- RULE 2 (No Unchecked Heap Operations): Any dereference of a pointer returned by
malloc,calloc, orreallocwithout an antecedentNULLcheck is a fatal quality defect. - RULE 3 (No VLAs on Stack): Variable-length arrays (
type arr[n]) are forbidden; they risk unbounded stack growth and remote code execution vulnerabilities. - RULE 4 (Explicit Return Codes): Every fallible function must return an explicit status code (enum or negative error integer) or error struct. Functions must never rely on side effects to communicate failure.
7. Evidence Required
- Compiler Evidence: Clean compilation under
-std=c17or-std=c2xwith-Werrorand-D_FORTIFY_SOURCE=3. - Sanitizer Evidence: Zero memory leaks, zero buffer overflows, and zero undefined behavior warnings under ASan/UBSan execution.
- Boundary Verification: Unit test evidence verifying that buffer truncated conditions return expected error codes without heap or stack corruption.
8. Output Contract
- Hardened C source (
.c) and header (.h) files with full prototype signatures and header guards. - Makefile or CMake configuration specifying strict flags and sanitizer profiles.
- C Verification Report (
templates/c-verification-report.md).
9. Stop Conditions
- All tests pass with zero findings under ASan and UBSan.
- Static analysis and compiler run with
-Werrorand report zero diagnostics. - Token budget exhausted.
- Blocked on external dependency.
10. Escalation Rules
- Escalate immediately if an external legacy C library with known buffer overflow risks must be linked without a memory-safe wrapper boundary.
- Escalate if hardware-specific memory-mapped I/O requires disabling strict aliasing flags (
-fno-strict-aliasing).
04 同仓库更多 Skills
zoom-reflow
暂无简介
wireframe-styleguide
暂无简介
visual-regression
Pixel-by-pixel snapshot testing, Playwright integration, dynamic content masking, deterministic font rendering, and cross-browser CI gates
ui-implementation
Component-driven UI implementation, multi-state matrix (default, hover, focus-visible, active, disabled, loading, empty, error), DTCG design tokens, responsive layout contracts, and accessibility tree mapping.
tui-editor-engine
Text buffer and editing semantics for a terminal code editor — index conversion, transaction-first editing, multi-cursor invariants, undo grouping, and large-file behaviour.
tree-sitter-context-indexing
暂无简介