Communitygithub.com

trailofbits/modern-cpp

C++ 코드를 최신 관용구(C++20/23/26)로 안내합니다. 새 C++ 코드를 작성하거나, 레거시 패턴을 현대화하거나, 보안에 민감한 C++ 작업 시 사용하세요. 원시 포인터를 스마트 포인터로, SFINAE를 개념(concepts)으로, printf를 std::print로, 오류 코드를 std::expected로 대체합니다.

modern-cpp란 무엇인가요?

이 스킬은 Trail of Bits의 보안 중심 접근법을 바탕으로, C++20/23/26 관용구를 사용하여 현대적인 C++ 코드를 작성하도록 안내합니다. 새 코드 작성, 레거시 코드 현대화, 보안 중요 코드 작업 시 적합하며, 메모리 안전성과 타입 안전성을 강화하는 패턴(예: std::unique_ptr로 자동 메모리 관리, 개념으로 템플릿 제약, std::expected로 오류 처리)을 권장합니다. 기능을 사용 가능성에 따라 4단계(즉시 사용 가능, 즉시 배포, 계획, 관찰)로 분류하고, 컴파일러 강화 플래그와 샌드타이저를 CI에 포함시키는 실용적인 조언을 제공합니다. 원시 포인터, SFINAE, printf 등 30가지 이상의 안티 패턴을 식별하고 대체 방법을 제시하는 표가 포함되어 있습니다.

지원 대상~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/trailofbits/skills/tree/main/plugins/modern-cpp/skills/modern-cpp

즐겨 사용하는 AI에게 물어보기

이 에이전트 스킬이 미리 로드된 새 채팅을 엽니다.

문서

Modern C++

C++20, C++23, C++26 관용구를 사용한 현대 C++ 작성을 위한 가이드입니다. Trail of Bits의 보안 강조와 함께 취약점 클래스를 제거하고 상용구를 줄이는 패턴에 중점을 둡니다.

이 스킬을 사용해야 하는 경우

  • 새 C++ 함수, 클래스 또는 라이브러리 작성 시
  • 기존 C++ 코드 현대화(Pre-C++20 패턴)
  • 레거시와 현대적 접근 방식 간 선택이 필요할 때
  • 보안 중요 또는 안전 중요 C++ 작업 시
  • C++ 코드 리뷰에서 현대 관용구 채택 여부 확인 시

이 스킬을 사용하지 말아야 하는 경우

  • 사용자가 명시적으로 이전 표준을 요구하는 경우: 제약 조건(임베디드, 레거시 ABI)을 존중하세요.
  • 순수 C 코드: 이 스킬은 C++ 전용입니다.
  • 빌드 시스템 질문: CMake, Meson, Bazel 구성은 범위를 벗어납니다.
  • C++ 이외의 프로젝트: C++이 주가 아닌 혼합 코드베이스.

피해야 할 안티 패턴

피할 것대신 사용할 것이유
new/deletestd::make_unique, std::make_shared누수, 이중 해제 제거
원시 소유 포인터std::unique_ptr, std::shared_ptrRAII 소유권 의미론
C 배열 (int arr[N])std::array<int, N>경계 인식, 값 의미론
포인터 + 길이 매개변수std::span<T>비소유, 경계 검사 가능
printf / sprintfstd::format, std::print타입 안전, 버퍼 오버플로우 없음
C 스타일 캐스트 (int)xstatic_cast<int>(x)명시적 의도, 감사 가능
#define 상수constexpr 변수범위 지정, 타입 지정, 디버깅 가능
SFINAE / enable_ifConcepts + requires읽기 쉬운 제약 조건과 오류
오류 코드 + 출력 매개변수std::expected<T, E>구성 가능, 타입 안전 오류
unionstd::variant타입 안전, 무음 UB 없음
원시 mutex.lock()/unlock()std::scoped_lock예외 안전, 교착 상태 없음
std::threadstd::jthread자동 조인, 중지 토큰 지원
assert() 매크로contract_assert (C++26)도구에 표시, 구성 가능
수동 CRTPDeducing this (C++23)더 간단, 템플릿 상용구 없음
매크로 코드 생성Reflection (C++26)제로 오버헤드, 구성 가능

전체 표(30개 이상 패턴)는 anti-patterns.md를 참조하세요.

결정 트리

무엇을 하고 있나요?
|
+-- 새 C++ 코드를 작성 중인가요?
|   +-- 기본적으로 현대 관용구 사용 (C++20/23)
|   +-- 컴파일러가 지원하는 최신 표준 선택
|   +-- 아래 기능 등급(Feature Tiers) 참조
|
+-- 기존 코드를 현대화 중인가요?
|   +-- Tier 1 (C++20/23) 대체부터 시작
|   +-- 보안 영향 순으로 우선순위 지정 (메모리 > 타입 > 스타일)
|   +-- 마이그레이션 표는 anti-patterns.md 참조
|
+-- 보안 중요 코드인가요?
|   +-- 컴파일러 강화 플래그 활성화 (아래 참조)
|   +-- 강화된 libc++ 모드 활성화
|   +-- CI에서 샌드타이저 실행
|   +-- safe-idioms.md 및 compiler-hardening.md 참조
|
+-- C++26 기능을 사용 중인가요?
    +-- Reflection: 예, 계획하세요 (GCC 16+)
    +-- Contracts: 새로운 API 경계에 대해 신중히 사용
    +-- std::execution: 생태계 성숙도 대기
    +-- cpp26-features.md 참조

기능 등급

기능은 표준 버전이 아닌 오늘날의 실용적 사용 가능성에 따라 순위가 매겨집니다.

Tier 1: 오늘 사용 가능 (C++20/23, 견고한 컴파일러 지원)

기능대체 대상표준
Concepts + requiresSFINAE, enable_ifC++20
Ranges + views원시 반복자 루프C++20
std::span<T>포인터 + 길이C++20
std::formatsprintf, iostream 체인C++20
삼자 비교 <=>수동 비교 연산자C++20
std::jthreadstd::thread + 수동 조인C++20
지정 초기화자위치 기반 구조체 초기화C++20
std::expected<T,E>오류 코드, 경계에서의 예외C++23
std::print / std::printlnprintf, std::cout <<C++23
Deducing thisCRTP, const/non-const 중복C++23
std::flat_map읽기 중심 사용을 위한 std::mapC++23
모나딕 std::optionaloptional에 대한 중첩 if 검사C++23

cpp20-features.md 및 cpp23-features.md를 참조하세요.

Tier 2: 지금 배포 가능 (표준 버전 변경 불필요)

이 기능들은 C++ 표준 버전을 변경하지 않고도 안전성을 향상시킵니다:

  • 컴파일러 강화 플래그 — -D_FORTIFY_SOURCE=3, -fstack-protector-strong, -ftrivial-auto-var-init=zero
  • 강화된 libc++ — 약 0.3% 오버헤드의 경계 검사를 위해 -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST 사용
  • CI의 샌드타이저 — 최소 ASan + UBSan; 동시 코드의 경우 TSan
  • 경고 플래그 — -Wall -Wextra -Wpedantic -Werror

compiler-hardening.md를 참조하세요.

Tier 3: 계획 필요 (C++26, 주변 재구성 가치 있음)

Reflection은 가장 혁신적인 C++26 기능입니다. 다음을 제거합니다:

  • 직렬화 상용구 (일반 함수 하나가 구조체별 to_json을 대체)
  • 코드 생성기 (protobuf 코드 생성, Qt MOC)
  • 매크로 기반 등록 및 enum-to-string 해킹

GCC 16 (2026년 4월)에 reflection이 병합되었습니다. 혜택을 받을 수 있도록 새 코드를 계획하세요.

Tier 4: 관찰 필요 (C++26, 성숙도 필요)

  • Contracts (pre/post/contract_assert) — assert()보다 낫지만, 가상 함수 지원이 없고 컴파일러 지원이 제한적입니다. 새로운 API 경계에 대해 신중히 채택하세요.
  • std::execution (senders/receivers) — 강력한 비동기 프레임워크이지만, 가파른 학습 곡선, 스케줄러 미포함, 문서화 부족 등이 있습니다. 생태계 성숙도를 기다리세요.

cpp26-features.md를 참조하세요.

컴파일러 강화 빠른 참조

필수 플래그 (GCC + Clang)

-Wall -Wextra -Wpedantic -Werror
-D_FORTIFY_SOURCE=3
-fstack-protector-strong
-fstack-clash-protection
-ftrivial-auto-var-init=zero
-fPIE -pie
-Wl,-z,relro,-z,now

Clang 전용

-Wunsafe-buffer-usage

강화된 libc++ (Clang/libc++ 전용)

-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST

Google은 이를 Chrome 및 서버 팜 전체에 배포하여 약 0.3% 오버헤드, 1000개 이상의 버그 발견, 프로덕션 세그폴트 30% 감소를 달성했습니다.

전체 가이드는 compiler-hardening.md를 참조하세요.

거부해야 할 합리화

합리화잘못된 이유
"경고 없이 컴파일됩니다"경고는 활성화한 플래그에 따라 다릅니다. -Wall -Wextra -Wpedantic을 추가하세요.
"ASan은 프로덕션에 너무 느립니다"샘플링 기반 프로덕션 탐지를 위해 GWP-ASan을 사용하세요 (약 0% 오버헤드).
"안전한 컨테이너만 사용합니다"반복자 무효화 및 확인되지 않은 optional 접근은 여전히 악용 가능합니다.
"스마트 포인터는 더 느립니다"std::unique_ptr은 원시 포인터 대비 제로 오버헤드입니다. 주장하기 전에 측정하세요.
"우리 코드에는 메모리 버그가 없습니다"Google은 강화된 libc++를 활성화했을 때 1000개 이상의 버그를 발견했습니다. 다른 곳도 마찬가지입니다.
"C++26 기능을 아직 사용할 수 없습니다"C++20/23 기능은 사용 가능합니다. 강화 플래그는 모든 표준에서 작동합니다. 거기서 시작하세요.
"현대 C++은 읽기 더 어렵습니다"std::expected는 5개의 출력 매개변수에서 오류 코드를 확인하는 것보다 읽기 쉽습니다.

모범 사례 체크리스트

  • 소유권에는 스마트 포인터를 사용하고, 비소유 관찰에는 원시 포인터만 사용하세요.
  • 함수 매개변수에는 포인터 + 길이보다 std::span을 선호하세요.
  • 타입화된 오류로 실패할 수 있는 함수에는 std::expected를 사용하세요.
  • 템플릿을 SFINAE 대신 개념으로 제약하세요.
  • 모든 빌드에서 컴파일러 강화 플래그와 강화된 libc++를 활성화하세요.
  • CI에서 ASan + UBSan을 실행하고, 동시 코드에는 TSan을 추가하세요.
  • 가능한 경우 constexpr / consteval을 사용하세요 (설계상 UB 없음).
  • 반환 값을 무시하는 것이 버그일 가능성이 있는 함수에 [[nodiscard]]를 표시하세요.
  • 값 의미론을 선호하고, union 대신 std::variant, enum 대신 enum class를 사용하세요.
  • 모든 변수를 선언 시 초기화하세요.

다음 읽을거리

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

trailofbits/address-sanitizer

AddressSanitizer detects memory errors during fuzzing. Use when fuzzing C/C++ code to find buffer overflows and use-after-free bugs.

trailofbits/aflpp

AFL++ is a fork of AFL with better fuzzing performance and advanced features. Use for multi-core fuzzing of C/C++ projects.

trailofbits/agentic-actions-auditor

Audits GitHub Actions workflows for security vulnerabilities in AI agent integrations including Claude Code Action, Gemini CLI, OpenAI Codex, and GitHub AI Inference. Detects attack vectors where attacker-controlled input reaches AI agents running in CI/CD pipelines, including env var intermediary patterns, direct expression injection, dangerous sandbox configurations, and wildcard user allowlists. Use when reviewing workflow files that invoke AI coding agents, auditing CI/CD pipeline security for prompt injection risks, or evaluating agentic action configurations.

trailofbits/algorand-vulnerability-scanner

Scans Algorand smart contracts for 11 common vulnerabilities including rekeying attacks, unchecked transaction fees, missing field validations, and access control issues. Use when auditing Algorand projects (TEAL/PyTeal).

trailofbits/ask-questions-if-underspecified

Clarify requirements before implementing. Use when serious doubts arise.

trailofbits/atheris

Atheris is a coverage-guided Python fuzzer based on libFuzzer. Use for fuzzing pure Python code and Python C extensions.

trailofbits/audit-augmentation

Augments Trailmark code graphs with external audit findings from SARIF static analysis results and weAudit annotation files. Maps findings to graph nodes by file and line overlap, creates severity-based subgraphs, and enables cross-referencing findings with pre-analysis data (blast radius, taint, etc.). Use when projecting SARIF results onto a code graph, overlaying weAudit annotations, cross-referencing Semgrep or CodeQL findings with call graph data, or visualizing audit findings in the context of code structure.

trailofbits/audit-context-building

Enables ultra-granular, line-by-line code analysis to build deep architectural context before vulnerability or bug finding.

trailofbits/audit-prep-assistant

Prepares codebases for security review using Trail of Bits' checklist. Helps set review goals, runs static analysis tools, increases test coverage, removes dead code, ensures accessibility, and generates documentation (flowcharts, user stories, inline comments).

trailofbits/burpsuite-project-parser

Searches and explores Burp Suite project files (.burp) from the command line. Use when searching response headers or bodies with regex patterns, extracting security audit findings, dumping proxy history or site map data, or analyzing HTTP traffic captured in a Burp project.

trailofbits/cairo-vulnerability-scanner

Scans Cairo/StarkNet smart contracts for 6 critical vulnerabilities including felt252 arithmetic overflow, L1-L2 messaging issues, address conversion problems, and signature replay. Use when auditing StarkNet projects.

trailofbits/cargo-fuzz

cargo-fuzz is the de facto fuzzing tool for Rust projects using Cargo. Use for fuzzing Rust code with libFuzzer backend.

trailofbits/chrome-mcp-troubleshooting

Diagnose and fix Claude in Chrome MCP extension connectivity issues. Use when mcp__claude-in-chrome__* tools fail, return "Browser extension is not connected", or behave erratically.

trailofbits/claude-in-chrome-troubleshooting

Diagnose and fix Claude in Chrome MCP extension connectivity issues. Use when mcp__claude-in-chrome__* tools fail, return "Browser extension is not connected", or behave erratically.

trailofbits/code-improver

Runs an autonomous review-and-fix improvement loop over any code target — a skill, plugin, module, or directory — using a reviewer the user names: any installed skill or agent. Keeps a cross-round findings ledger, escalates when fixes stop converging, and guards scope mechanically. Use when asked to 'improve this code until review passes', 'run an improvement loop with <reviewer>', or to iterate review-and-fix with a specific reviewer. For skills prefer the skill-improver entry; for a branch prefer pr-improver.

trailofbits/code-maturity-assessor

Systematic code maturity assessment using Trail of Bits' 9-category framework. Analyzes codebase for arithmetic safety, auditing practices, access controls, complexity, decentralization, documentation, MEV risks, low-level code, and testing. Produces professional scorecard with evidence-based ratings and actionable recommendations.

trailofbits/codeql

Scans a codebase for security vulnerabilities using CodeQL's interprocedural data flow and taint tracking analysis. Triggers on "run codeql", "codeql scan", "codeql analysis", "build codeql database", or "find vulnerabilities with codeql". Supports "run all" (security-and-quality + security-experimental suites) and "important only" (high-precision security findings) scan modes. Also handles creating data extension models and processing CodeQL SARIF output.

trailofbits/constant-time-analysis

Detects timing side-channel vulnerabilities in cryptographic code. Use when implementing or reviewing crypto code, encountering division on secrets, secret-dependent branches, or constant-time programming questions in C, C++, Go, Rust, Swift, Java, Kotlin, C#, PHP, JavaScript, TypeScript, Python, or Ruby.

trailofbits/constant-time-testing

Constant-time testing detects timing side channels in cryptographic code. Use when auditing crypto implementations for timing vulnerabilities.

trailofbits/cosmos-vulnerability-scanner

Scans Cosmos SDK blockchain modules and CosmWasm contracts for consensus-critical vulnerabilities — chain halts, fund loss, state divergence. 25 core + 16 IBC + 10 EVM + 3 CosmWasm patterns. Use when auditing custom x/ modules, reviewing IBC integrations, or assessing pre-launch chain security. Updated for SDK v0.53.x.

관련 스킬