360 Secure SkillHub
360 安全技能中心

C

该 Skill 汇总 malloc/free、指针、字符串、整数类型、预处理器和未定义行为等常见陷阱,给出关键规则与参考文档索引,用于辅助编写和检查更安全的 C 代码。它主要提供文字规则,不包含编译或运行工具。

ivangdavila
ivangdavila
热度 87.58
最近更新 2026-05-11 15:57:58
分类:开发依赖:纯提示词应用场景:代码生成 / 代码审查 / 调试排错 / 概念解释

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add c
Metadata
name
C
slug
c
version
1.0.1
description
Write safe C avoiding memory corruption, buffer overflows, and undefined behavior traps.
metadata
{"clawdbot":{"emoji":"⚙️","requires":{"bins":["gcc","clang"]},"os":["linux","darwin","win32"]}}

Quick Reference

Topic File
malloc/free, leaks, double free memory.md
Null, dangling, pointer arithmetic pointers.md
Null terminator, buffer overflow strings.md
Integer overflow, signed/unsigned types.md
Macro traps, include guards preprocessor.md
Common undefined behavior undefined.md

Critical Rules

  • malloc returns void* — cast required in C++, optional in C but check for NULL
  • free(ptr); ptr = NULL; — always null after free to prevent double-free
  • sizeof(array) in function gives pointer size, not array size — pass length separately
  • char str[5] = "hello"; — no room for null terminator, UB when used as string
  • strcpy doesn't check bounds — use strncpy and manually null-terminate
  • Signed overflow is UB — compiler can optimize assuming it never happens
  • i++ + i++ is UB — no sequence point between modifications
  • Returning pointer to local variable — dangling pointer, UB on use
  • #define SQUARE(x) x*x — SQUARE(1+2) = 1+2*1+2 = 5, not 9
  • memcpy with overlapping regions — use memmove instead
  • Uninitialized variables — contain garbage, UB if used
  • Array out of bounds — no runtime check, silent corruption or crash