跳到正文
Code Review

← 官方规则来源

C · 官方规则原文

OpenCodeReview v1.13.8 · 4236c871 · Apache-2.0。保留固定版本英文原文;item 编号表示原文件中的检查项顺序。实际扫描以当前规则开关和扫描快照为准。

查看当前规则与开关 → · 下载原始 Markdown

版本与文件信息

提交:4236c871fe8130aa840a2a1c0922f469e87e0416

源码路径:internal/config/rules/rule_docs/c.md

SHA-256:bc24216e887e782d18b41448ba70ec149db60fca69ce270e69fe0285a4dd1996

Apache-2.0 许可证 · 基线说明

Obvious Typos or Spelling Errors

  • Spelling errors in variable names, constant names, or function names at their declaration sites; do not report spelling errors at call sites
  • Strings in log messages or exception messages containing spelling errors that affect readability

Proper malloc/free Pairing

Key checks:

  • Every malloc() has a corresponding free()
  • Avoid double-freeing the same memory block
  • Set the pointer to NULL after freeing

Example:

// Bad
char* buffer = malloc(1024);
// use buffer...
// forgot to free memory

// Good
char* buffer = malloc(1024);
if (buffer != NULL) {
    // use buffer...
    free(buffer);
    buffer = NULL;
}

Memory Leak Detection

Key checks:

  • All allocated memory is freed before function exit
  • Memory is also freed in error handling paths
  • Use tools such as Valgrind for detection

Buffer Overflow Protection

Key checks:

  • Check bounds before array access
  • Use safe functions for string operations
  • Ensure correct loop boundary conditions

Example:

// Bad: dangerous operation
char buffer[100];
strcpy(buffer, user_input); // may overflow

// Good: safe operation
char buffer[100];
strncpy(buffer, user_input, sizeof(buffer) - 1);
buffer[sizeof(buffer) - 1] = '\0';

Safe String Operations

Recommended safe functions:

  • strncpy() instead of strcpy()
  • strncat() instead of strcat()
  • snprintf() instead of sprintf()

Naming Conventions

Requirements:

  • Use snake_case naming style
  • Variable names should be meaningful
  • Constants should use UPPER_CASE