prevent-c-cpp-security-bugs

A coding guide for writing and changing C and C++ programs with common security risks in mind. It covers issues involving memory, input, files, queries, commands, and multiple threads.

In plain words
What is it for?
Use it when creating, fixing, refactoring, or optimizing C/C++ code, especially code using pointers, memory allocation, file access, network parsing, user input, or thread synchronization.
Why use it?
It helps prevent vulnerabilities such as buffer overflows, use-after-free errors, double frees, unsafe input handling, and race conditions while the code is being written.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/antgroup/adversarial-ai-coding-plugin/prevent-c-cpp-security-bugs
Any agent
npx skills add antgroup/adversarial-ai-coding-plugin --skill prevent-c-cpp-security-bugs
Clone the repo
git clone --depth 1 https://github.com/antgroup/adversarial-ai-coding-plugin

Made for: Claude Code, Codex.

Per session 163 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,304 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00163 $0.01304
Opus 5 $0.00081 $0.00652
Sonnet 5 $0.00033 $0.00261
Haiku 4.5 $0.00016 $0.00130

Measured 2d ago against content hash c82a33948f18, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

prevent-c-cpp-security-bugs scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

plugin/skills/prevent-c-cpp-security-bugs/SKILL.md · 77 lines

What it actually says

C/C++ 安全代码生成规范

技能目标

在代码生成阶段消除安全漏洞,而非依赖事后审查。

代码生成是拦截安全风险成本最低的时机——一旦漏洞代码进入生产环境,修复代价急剧上升。因此,在生成代码前完成威胁识别和规范查阅,能从根本上降低安全风险。

工作流程

按以下三步执行安全代码生成。每一步都为后续步骤提供安全保障。

第一步:识别安全风险

分析用户需求,判断哪些风险类型适用于当前场景。识别出的每一个风险类型都需要处理。

风险类型 典型场景 参考文档
缓冲区溢出 对缓冲区读写时未对用户输入进行长度校验 references/prevent-buffer-overflow.md
UAF 指针指向的内存在 free/delete 后未置空,后续继续使用 references/prevent-use-after-free.md
Double free 同一块内存被释放两次,破坏堆管理器数据结构 references/prevent-double-free.md
格式化字符串漏洞 不受信任的输入直接作为格式化函数参数 references/prevent-format-string-vuln.md
整数溢出/下溢 计算结果超出整数范围,常触发缓冲区溢出 references/prevent-integer-overflow-underflow.md
符号扩展/类型转换 有符号数和无符号数混合运算出错 references/prevent-signedness-bugs.md
条件竞争 多线程/进程对共享资源访问未正确同步 references/prevent-race-condition.md
符号链接攻击 TOCTOU 时间间隙中被替换文件路径 references/prevent-race-condition.md
危险函数调用 调用设计上存在缺陷的标准库函数 references/prevent-potential-dangerous-function.md
QL 注入 拼接 SQL/HQL/NoSQL 查询语句 references/prevent-ql-injection.md
路径遍历 文件路径由用户输入拼接 references/prevent-path-traversal.md
OS 命令执行 调用 system、popen、execve 执行系统命令 references/prevent-os-command-execution.md

第二步:查阅安全编码规范

对第一步中识别出的每一个风险类型,读取对应的参考文档。

参考文档详细说明了该类漏洞的根因、攻击模式和安全 API 用法,帮助理解如何在代码层面消除风险。

第三步:完成用户需求

在理解安全规范后,遵循以下核心原则生成代码:

  • 默认安全:优先使用参数化查询、白名单校验、安全 API 等内置防护机制
  • 零信任输入:来自 HTTP 请求、外部接口的数据一律视为不可信
  • 失败安全:权限校验或输入验证失败时默认拒绝操作
  • 边界防御:所有内存读写、字符串复制、数组遍历必须有显式长度约束
  • 初始化与清理:变量诞生时有确定值,敏感数据消亡时彻底擦除
  • 算数安全:内存分配、数组索引计算前确保不会溢出、截断或符号错误
  • 编译器权限:不需要修改的数据、指针或对象状态用 const 禁止修改
  • 并发安全:共享资源访问保证原子性或被正确同步

参考资源

参考文档

  • references/prevent-buffer-overflow.md — 缓冲区溢出防范详解
  • references/prevent-use-after-free.md — 释放后使用(UAF)防范详解
  • references/prevent-double-free.md — 二次释放防范详解
  • references/prevent-format-string-vuln.md — 格式化字符串漏洞防范详解
  • references/prevent-integer-overflow-underflow.md — 整数溢出/下溢防范详解
  • references/prevent-signedness-bugs.md — 符号扩展/类型转换漏洞防范详解
  • references/prevent-race-condition.md — 条件竞争防范详解
  • references/prevent-potential-dangerous-function.md — 危险函数替换指南
  • references/prevent-ql-injection.md — Query Language 注入防范详解
  • references/prevent-path-traversal.md — 路径遍历防范详解
  • references/prevent-os-command-execution.md — OS 命令执行防范详解
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 77 lines · 163 tokens per session scan A c82a33948f18

Subscribe to this mod's changes

prevent-c-cpp-security-bugs is a skill published in the GitHub repository antgroup/adversarial-ai-coding-plugin (7 stars, last pushed 4mo ago), licensed Apache-2.0. It adds 163 tokens to every session and 1,304 once invoked, about $0.0008 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

spanify-buffers

Find and fix unsafe buffer operations in a C++ file by removing UNSAFETODO markers and replacing unsafe raw pointers/C-style functions with base::span and standard safe containers. Use when the user asks to fix unsafe buffer warnings or -Wunsafe-buffer-usage errors. Don't use for other types of memory safety bugs like…

chromium/chromium · 86 tokens

jni-type-conversion

How to use @JniType annotations for ergonomic JNI. Relevant for Java files that use @NativeMethods or @CalledByNative.

chromium/chromium · 32 tokens

platform-port

Guide porting FastLED to new MCU platforms, including int.h types, clockless drivers, SPI implementations, and platform detection. Use when adding support for a new microcontroller family or board.

FastLED/FastLED · 42 tokens

qt-cpp-review

Invoke when the user asks to review, check, audit, or look over Qt6 C++ code — or suggest before committing. Runs deterministic linting (60+ rules) then six parallel deep- analysis agents covering model contracts, ownership, threading, API correctness, error handling, and performance. Reports only high-confidence…

mavlink/qgroundcontrol · 86 tokens

cpp-pro

Writes, optimizes, and debugs C++ applications using modern C++20/23 features, template metaprogramming, and high-performance systems techniques. Use when building or refactoring C++ code requiring concepts, ranges, coroutines, SIMD optimization, or careful memory management — or when addressing performance…

Jeffallan/claude-skills · 79 tokens

ax-cpp-ai

Use when writing C++ code with axllm for named deployment profiles, generic provider clients, model selection, OpenAI-compatible calls, Responses, Gemini, Anthropic, routers, and balancers.

ax-llm/ax · 47 tokens