redhatproductsecurity/prodsec-skills/safe-c-functions
Claude Code and agent skill by redhatproductsecurity/prodsec-skills
- community
- ext:[{"type":"hook","description":"Pre/post lifecycle hooks"}]
Checking that the SKILL.md file is still in the repository.
Apply when reviewing or writing C/C++ code that uses memory or string operations. Identifies banned functions, provides safe replacements, compiler hardening flags, and refactoring examples.
Install
npx vskill@latest install redhatproductsecurity/prodsec-skills/safe-c-functionsSemi-extensibleMentions customization, but needs skill-specific knowledge. Read the extensible skills standard
SKILL.md
The same SKILL.md was published earlier as redhatproductsecurity/prodsec-skills/safe-c-functions.
C/C++ Memory and String Safety
Actively identify, flag, and provide secure refactoring options for insecure memory and string functions. When generating new code, always default to the safest function available.
Insecure Functions and Safe Alternatives
Critical / High Risk
`gets()` -- Critical. No bounds checking whatsoever. Always replace with:
fgets(char *str, int n, FILE *stream)
`strcpy()` -- High risk. Copies until null terminator with no bounds check. Replace with:
snprintf(),strcpy_s()(C11 Annex K), orstrncpy()(with manual null termination)
`strcat()` -- High risk. Appends with no bounds check. Replace with:
snprintf(),strcat_s()(C11 Annex K), orstrncat()(with careful handling)
`sprintf()` / `vsprintf()` -- High risk. No output buffer bounds check. Replace with:
snprintf(),vsprintf_s()(C11 Annex K)
Medium Risk
`scanf()` family -- %s without width limit causes buffer overflows.
- Use width specifiers:
scanf("%127s", buffer) - Better: read with
fgets()then parse withsscanf()
`strtok()` -- Not reentrant or thread-safe (static internal buffer). Replace with:
strtok_r()(POSIX) orstrtok_s()(C11 Annex K)
`memcpy()` / `memmove()` -- Not inherently insecure but common source of bugs with miscalculated sizes. Prefer memcpy_s() / memmove_s() when available.
Banned Memory Functions
| Banned | Safe Replacement | |
|---|---|---|
memcpy() | memcpy_s() | |
memset() | memset_s() | |
memmove() | memmove_s() | |
memcmp() | memcmp_s() | |
bzero() | memset_s() | |
memzero() | memset_s() |
Banned String Functions
| Banned | Safe Replacement | |
|---|---|---|
strstr() | strstr_s() | |
strtok() | strtok_s() | |
strcpy() | strcpy_s() | |
strcmp() | strcmp_s() | |
strlen() | strnlen_s() | |
strcat() | strcat_s() | |
sprintf() | snprintf() |
New Code Guidelines
- NEVER generate code using
gets(),strcpy(),strcat(), orsprintf(). - DEFAULT to
snprintf()for string formatting and concatenation. - DEFAULT to
fgets()for reading string input from files or stdin.
Compiler Hardening Flags
- Stack protection:
-fstack-protector-allor-fstack-protector-strong - Address sanitizer:
-fsanitize=address(development builds) - Object size checking:
-D_FORTIFY_SOURCE=2(runtime bounds checks for unsafe functions) - Format string protection:
-Wformat -Wformat-security
Refactoring Examples
Replacing strcpy
Correcting strncpy Usage
Securing scanf
Safe String Copy Pattern
Safe String Concatenation Pattern
Safe Memory Copy Pattern
Safe String Tokenization Pattern
Common Pitfalls
Pitfall 1: Wrong Size Parameter
Pitfall 2: Ignoring Return Values
Pitfall 3: Using sizeof() on Pointers
Code Review Checklist
Developer Pre-Review
- [ ] No unsafe memory functions (
memcpy,memset,memmove,memcmp,bzero) - [ ] No unsafe string functions (
strcpy,strcat,strcmp,strlen,sprintf,strstr,strtok) - [ ] All memory operations use
*_s()variants with proper size parameters - [ ] Buffer sizes calculated using
sizeof()or known limits - [ ] No hardcoded buffer sizes that could change
Reviewer Checks
- [ ] All memory operations use safe variants
- [ ] Destination buffer sizes match the allocated size of the destination buffer
- [ ] All
errno_treturn values handled - [ ]
rsize_t dmaxparameters are correct - [ ] Strings explicitly null-terminated after every write operation
- [ ] Source string lengths validated before operations
Static Analysis Integration
- [ ] Compiler warnings enabled for unsafe function usage
- [ ] Static analysis tools configured to detect unsafe function calls
- [ ] Build system treats unsafe function warnings as errors
- [ ] Pre-commit hooks scan for banned functions
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingmodule/skills/safe-c-functions/SKILL.md
Popularity
- Repository stars
- 49
- Repository forks
- 6
- 7-day trend score
- 20
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 19 Aug 2026, 3 months ago.
Extension points
hookPre/post lifecycle hooks
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/redhatproductsecurity/prodsec-skills/safe-c-functions)