ams-c-pro
暂无简介
- License 许可
- MIT
- 最近更新
- 2026-11-07
- Stars
- ★ 0
- Forks
- 1
03 SKILL.md 正文
C Pro
Overview
C gives you absolute control and zero safety net: every allocation, every bound, every lifetime is your responsibility. Professional C is therefore a discipline of explicit contracts — who owns this memory, who frees it, what are the valid inputs — enforced by convention, assertions, sanitizers, and relentless simplicity.
This skill covers writing C that's correct, readable, and maintainable: ownership discipline, defensive interfaces, build hygiene, and the tooling that compensates for the language's lack of guardrails.
When to use
- Writing or reviewing C for correctness and safety.
- Designing C APIs (ownership, error reporting, lifetimes).
- Debugging memory corruption, leaks, or undefined behavior.
- Structuring C projects (build systems, modularization).
- Maintaining or hardening legacy C codebases.
Core concepts
- Ownership is a documented contract. Every allocation site answers: who frees this, and when?
Encode it in naming (
create_/destroy_,_new/_freepairs) and document it on every function that transfers ownership. Consistent conventions beat clever ones. - Defensive interfaces. Validate inputs at API boundaries (
NULLchecks, range checks, buffer sizes passed explicitly — never truststrlenon untrusted input). Return error codes or use out-params for errors; never fail silently.assertfor internal invariants (programmer errors), runtime checks for external data. - Bounded everything.
snprintfnotsprintf,strncpy-with-care (or better,snprintf), explicit lengths on every buffer operation. Buffer overflows remain the classic C vulnerability — the fix is boring and total: never write without knowing the bound. - Simple control flow, small functions. C punishes cleverness disproportionately. Flat
functions, early returns, one exit path for cleanup (
goto cleanupis idiomatic C — used consistently, it's clearer than nested conditionals for resource teardown). - Opaque types for encapsulation.
typedef struct Foo Foo;in the header, full definition in the.cfile — clients can't poke internals, and you're free to change representation. C's answer to private members. - The build is part of the program.
-Wall -Wextra -Werror -Wpedantic, sanitizers (ASan/UBSan) in test builds,-fstack-protector, and warnings reviewed — not suppressed. The compiler is your only static analyzer unless you add more (clang-tidy, cppcheck).
Practical workflow
- Set compiler flags first.
-std=c17 -Wall -Wextra -Werror -Wpedantic -gfor dev; sanitizer builds (-fsanitize=address,undefined) for tests; hardened flags for release. - Design the API before the implementation. Header first: types, ownership comments, error conventions. If the header needs a paragraph to explain, simplify the API.
- Write with the cleanup pattern. Single exit with
goto cleanupfor functions acquiring multiple resources — consistent, reviewable, exception-free RAII:int process(const char *path) { FILE *f = NULL; char *buf = NULL; int rc = -1; f = fopen(path, "r"); if (!f) goto cleanup; buf = malloc(BUF_SIZE); if (!buf) goto cleanup; /* ... work ... */ rc = 0; cleanup: free(buf); if (f) fclose(f); return rc; } - Test with sanitizers. Unit tests (cmocka/unity/greatest) run under ASan+UBSan in CI; fuzz anything parsing untrusted input (AFL++/libFuzzer).
- Check resources systematically. Every
mallochas afreeon all paths; everyfopenafclose; every lock an unlock. Review diffs specifically for the error paths — that's where leaks live. - Document the non-obvious. Thread-safety guarantees, reentrancy, signal-safety, and ownership — in the header, where callers will see it.
Common pitfalls
- Use-after-free / double-free. The classic. Clear pointers after free in complex teardown
(
free(p); p = NULL;), keep ownership single and obvious, and let ASan prove it. - Off-by-one and missing NUL.
char buf[16]; strncpywithout termination,<=vs<in loops over buffers. Write the bound, then re-read it — or better, usesnprintfand check its return. - Ignoring return values.
malloccan return NULL;freadcan short-read;snprintfreports truncation. Unchecked returns are latent crashes —-Wunused-resulthelps for annotated functions. - Undefined behavior rationalized. "It works" with signed overflow, strict-aliasing violations,
or data races. UBSan +
-fstrict-aliasingwarnings exist because the optimizer will exploit UB. - Global mutable state. Hidden coupling, untestable functions, thread-unsafety. Pass context
structs explicitly; keep globals
constor don't have them. - Clever macros. Multi-statement macros without
do { } while(0), macros with side-effecting arguments evaluated twice, macro "generics" hiding type errors. Preferstatic inlinefunctions. - No bounds on input parsing.
scanf("%s"),gets(removed from the language for a reason), hand-rolled parsers without length discipline. Parse defensively or use a tested library.
04 同仓库更多 Skills
web-scrape
Turn web pages into LLM-ready markdown via the Firecrawl API (renders JS, anti-bot handling). Use when browser_open/fetch fails on JS-heavy pages, or when you need clean markdown/structured extraction from a site. Requires FIRECRAWL_API_KEY.
design-taste-frontend
tasteskill v2, the anti-slop frontend skill. Art direction for websites, distilled from a study of 247 curated gallery sites (One Page Love, Awwwards Sites of the Day). Deals researched directions (idea, composition, type, palette, photographs, section blueprints) from a library, enforces a strict no-slop house style with a real-browser inspector, and runs a look-and-fix loop on screenshots. Use this whenever you build, design, redesign, restyle, polish or review a landing page, marketing site, portfolio, product page, one-pager, hero or any public-facing web page, in any stack (HTML/CSS, React, Next.js, Vue, Svelte, Astro, Tailwind) — also when the user says a page looks generic, templated, "AI-made", sloppy, or asks to make it look designed, premium, award-level or less like every other site. Not for dashboards or app screens.
test-driven-development
Use when implementing any feature or bugfix, before writing implementation code
systematic-debugging
Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes
requesting-code-review
Use when completing tasks, implementing major features, or before merging to verify work meets requirements
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).