# Ported from alibaba/open-code-review (Apache-2.0):
#   internal/config/rules/rule_docs/c.md
#   at commit 89ec55b14442c9f2601fb55b5f554fb6fabbe2c7.
# OCR tool names were replaced with this workflow's read-only
# exploration language. See the NOTICE.md and LICENSE files
# in rules/builtin/ for attribution and license details.
version: 1

rules:
  - id: language.c
    match:
      paths:
        - "**/*.c"
    checks:
      - id: obvious-typos-or-spelling-errors
        category: conventions
        guidance: |
          - 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
      - id: proper-malloc-free-pairing
        category: correctness
        guidance: |
          **Key checks:**
          - Every `malloc()` has a corresponding `free()`
          - Avoid double-freeing the same memory block
          - Set the pointer to NULL after freeing

          **Example:**
          ```c
          // 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;
          }
          ```
      - id: memory-leak-detection
        category: correctness
        guidance: |
          **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
      - id: buffer-overflow-protection
        category: correctness
        guidance: |
          **Key checks:**
          - Check bounds before array access
          - Use safe functions for string operations
          - Ensure correct loop boundary conditions

          **Example:**
          ```c
          // 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';
          ```
      - id: safe-string-operations
        category: correctness
        guidance: |
          **Recommended safe functions:**
          - `strncpy()` instead of `strcpy()`
          - `strncat()` instead of `strcat()`
          - `snprintf()` instead of `sprintf()`
      - id: naming-conventions
        category: conventions
        guidance: |
          **Requirements:**
          - Use snake_case naming style
          - Variable names should be meaningful
          - Constants should use UPPER_CASE
