Skip to content

greptile: Reduce ruleset to memory-allocation, logging-api, and string-safety#255

Open
suphawitwanl wants to merge 1 commit intomasterfrom
greptile-consolidated
Open

greptile: Reduce ruleset to memory-allocation, logging-api, and string-safety#255
suphawitwanl wants to merge 1 commit intomasterfrom
greptile-consolidated

Conversation

@suphawitwanl
Copy link
Copy Markdown

Settings:

  • Strictness 2, status check enabled, re-reviews on every push
  • Comment types: logic, syntax, style

Rule structure:
Each rule has an id, description, scope, and severity.

  • Scope: glob patterns that limit which files the rule applies to (all three rules scope to **/.c, **/.h, **/*.cpp)
  • Severity: critical (P0 — must fix), high (P1 — should fix), medium (P2 — consider fix)

Rules cover:

  • Memory safety: XMALLOC/XCALLOC/XREALLOC/XSTRDUP/XFREE enforcement, no raw malloc/calloc/realloc/free, MTYPE tracking via DECLARE_MTYPE/DEFINE_MTYPE, prefer DEFINE_MTYPE_STATIC for module-private types
  • String safety: no strcpy/strcat/sprintf, enforce strlcpy/strlcat/snprintf, buffer size arguments must use sizeof()
  • Logging: zlog_* (zlog_debug, zlog_err, zlog_warn, zlog_info, zlog_notice) and flog_/flog_err_sys (structured error logging with EC_ codes), no printf/stdio, all debug statements guarded by CLI-controllable debug flags

Doc context loaded by Greptile (.greptile/files.json):

  • doc/developer/workflow.rst — coding standards, string-safety policy, debug-guard requirement
  • doc/developer/memtypes.rst — XMALLOC/MTYPE system reference
  • doc/developer/logging.rst — zlog/flog API, debug message conventions

Adds the slim Greptile review configuration in a single commit so the
review bot evaluates the full config end-to-end rather than seeing only
incremental diffs.

Config snapshot (consolidates work previously landed across PRs #240,
#244, #253):

- Three C-safety rules: memory-allocation (critical), logging-api
  (high), string-safety (critical).
- logging-api covers flog_err / flog_warn alongside zlog_*.
- memory-allocation prefers DEFINE_MTYPE_STATIC for file-local MTYPEs.
- commentTypes restricted to documented values (logic/syntax/style).
- sequenceDiagramSection explicitly enabled to keep diagrams stable
  across reviews.
- files.json curates supporting doc/ context (workflow, memtypes,
  logging, lists, cli, etc.).

Earlier ~45-rule layout intentionally not restored: P0 topotest /
user-docs gates produced false positives on refactor PRs and were
removed deliberately. Reference snapshot preserved on the `greptile`
branch.
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 1, 2026

Greptile Summary

This PR introduces the Greptile review configuration for the FRR repository, adding .greptile/config.json (strictness 2, three enforcement rules, doc context) and .greptile/files.json (pointing at workflow.rst, memtypes.rst, and logging.rst as loaded context). The rule set and persona instructions are well-aligned with FRR's coding standards, but there are a few coverage gaps in the rule texts that could produce false negatives on reviewed PRs.

Confidence Score: 4/5

Safe to merge; all findings are P2 coverage gaps in rule prose that can be tightened in a follow-up

No logic errors, security issues, or misconfigurations — only P2 style suggestions around completeness of the forbidden-function lists in two rules

.greptile/config.json — rule texts for memory-allocation, string-safety, and logging-api have minor omissions worth addressing

Important Files Changed

Filename Overview
.greptile/config.json New Greptile configuration with 3 enforcement rules (memory-allocation, logging-api, string-safety); rule texts have minor coverage gaps — strdup/strndup missing from memory-allocation, strncpy/strncat missing from string-safety, fprintf not explicit in logging-api
.greptile/files.json New doc-context manifest pointing at the three canonical FRR developer guides; no issues found

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    PR[Pull Request Opened / Updated] --> G[Greptile triggered\ntriggerOnUpdates: true]
    G --> CTX[Load doc context\nworkflow.rst · memtypes.rst · logging.rst]
    CTX --> SCOPE{File matches\n**/*.c · **/*.h · **/*.cpp?}
    SCOPE -- No --> SKIP[Skip file]
    SCOPE -- Yes --> R1[memory-allocation rule\ncritical — P0\nNo malloc/calloc/realloc/free/strdup\nEnforce XMALLOC/XCALLOC/XSTRDUP+MTYPE]
    SCOPE -- Yes --> R2[logging-api rule\nhigh — P1\nNo printf/fprintf/stdio\nEnforce zlog_* / flog_err / flog_warn\nDebug guarded by CLI flags]
    SCOPE -- Yes --> R3[string-safety rule\ncritical — P0\nNo strcpy/strcat/sprintf/strncpy/strncat\nEnforce strlcpy/strlcat/snprintf+sizeof]
    R1 & R2 & R3 --> OUT[Review comment posted\nwith severity + inline suggestion]
    OUT --> SC[Status check\npass / fail]
Loading
Prompt To Fix All With AI
Fix the following 3 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 3
.greptile/config.json:32
**`strdup()`/`strndup()` not explicitly forbidden**

The rule forbids `malloc`, `calloc`, `realloc`, and `free`, but `strdup()` and `strndup()` also allocate heap memory outside the MTYPE tracking system and are equally prohibited in FRR — `XSTRDUP(MTYPE_..., s)` is the required replacement. Without naming them here, the rule will silently miss `strdup` calls in PRs. Consider adding them to the "NEVER allow" list alongside `malloc`.

### Issue 2 of 3
.greptile/config.json:44
**`strncpy()` and `strncat()` omitted from the forbidden list**

`strncpy()` does not null-terminate when the source equals or exceeds the destination size, and `strncat()`'s size argument represents remaining space rather than total buffer size — both are classic sources of off-by-one bugs. FRR's coding standards forbid them in favour of `strlcpy()`/`strlcat()`. Listing only `strcpy`, `strcat`, and `sprintf` leaves `strncpy`/`strncat` unguarded by this rule, which could produce false negatives on reviewed PRs.

### Issue 3 of 3
.greptile/config.json:38
**`fprintf()` not explicitly called out**

The rule says "Reject printf() or stdio logging" but `fprintf(stderr, ...)` and `fprintf(stdout, ...)` are the more common real-world offenders in C daemon code that bypass `zlog`. Relying on the implicit "stdio" cover may not be strong enough signal to the AI reviewer — explicitly naming `fprintf` alongside `printf` would close this gap.

Reviews (1): Last reviewed commit: "greptile: add consolidated review config" | Re-trigger Greptile

Comment thread .greptile/config.json
"rules": [
{
"id": "memory-allocation",
"rule": "NEVER allow standard malloc(), calloc(), realloc(), or free(). Enforce FRR's XCALLOC, XMALLOC, XREALLOC, XSTRDUP, and XFREE macros (requires MTYPE). Every allocation must use the MTYPE tracking system declared via DECLARE_MTYPE/DEFINE_MTYPE. Prefer DEFINE_MTYPE_STATIC for module-private types.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 strdup()/strndup() not explicitly forbidden

The rule forbids malloc, calloc, realloc, and free, but strdup() and strndup() also allocate heap memory outside the MTYPE tracking system and are equally prohibited in FRR — XSTRDUP(MTYPE_..., s) is the required replacement. Without naming them here, the rule will silently miss strdup calls in PRs. Consider adding them to the "NEVER allow" list alongside malloc.

Prompt To Fix With AI
This is a comment left during a code review.
Path: .greptile/config.json
Line: 32

Comment:
**`strdup()`/`strndup()` not explicitly forbidden**

The rule forbids `malloc`, `calloc`, `realloc`, and `free`, but `strdup()` and `strndup()` also allocate heap memory outside the MTYPE tracking system and are equally prohibited in FRR — `XSTRDUP(MTYPE_..., s)` is the required replacement. Without naming them here, the rule will silently miss `strdup` calls in PRs. Consider adding them to the "NEVER allow" list alongside `malloc`.

How can I resolve this? If you propose a fix, please make it concise.

Comment thread .greptile/config.json
},
{
"id": "string-safety",
"rule": "strcpy(), strcat(), and sprintf() are strictly FORBIDDEN — no exceptions, even if the buffer cannot currently overflow (a future change may introduce one). Enforce strlcpy(), strlcat(), and snprintf(). Buffer size arguments MUST use sizeof() wherever possible — never hardcoded size constants.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 strncpy() and strncat() omitted from the forbidden list

strncpy() does not null-terminate when the source equals or exceeds the destination size, and strncat()'s size argument represents remaining space rather than total buffer size — both are classic sources of off-by-one bugs. FRR's coding standards forbid them in favour of strlcpy()/strlcat(). Listing only strcpy, strcat, and sprintf leaves strncpy/strncat unguarded by this rule, which could produce false negatives on reviewed PRs.

Prompt To Fix With AI
This is a comment left during a code review.
Path: .greptile/config.json
Line: 44

Comment:
**`strncpy()` and `strncat()` omitted from the forbidden list**

`strncpy()` does not null-terminate when the source equals or exceeds the destination size, and `strncat()`'s size argument represents remaining space rather than total buffer size — both are classic sources of off-by-one bugs. FRR's coding standards forbid them in favour of `strlcpy()`/`strlcat()`. Listing only `strcpy`, `strcat`, and `sprintf` leaves `strncpy`/`strncat` unguarded by this rule, which could produce false negatives on reviewed PRs.

How can I resolve this? If you propose a fix, please make it concise.

Comment thread .greptile/config.json
},
{
"id": "logging-api",
"rule": "Reject printf() or stdio logging. Enforce FRR's zlog_* API (zlog_debug, zlog_err, zlog_warn, zlog_info, zlog_notice) and structured error macros flog_err(), flog_warn(), flog_err_sys() (which require an EC_* error-code argument). All debug statements MUST be guarded with CLI-controllable debug flags — unguarded debug prints are unacceptable at scale.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 fprintf() not explicitly called out

The rule says "Reject printf() or stdio logging" but fprintf(stderr, ...) and fprintf(stdout, ...) are the more common real-world offenders in C daemon code that bypass zlog. Relying on the implicit "stdio" cover may not be strong enough signal to the AI reviewer — explicitly naming fprintf alongside printf would close this gap.

Prompt To Fix With AI
This is a comment left during a code review.
Path: .greptile/config.json
Line: 38

Comment:
**`fprintf()` not explicitly called out**

The rule says "Reject printf() or stdio logging" but `fprintf(stderr, ...)` and `fprintf(stdout, ...)` are the more common real-world offenders in C daemon code that bypass `zlog`. Relying on the implicit "stdio" cover may not be strong enough signal to the AI reviewer — explicitly naming `fprintf` alongside `printf` would close this gap.

How can I resolve this? If you propose a fix, please make it concise.

@suphawitwanl
Copy link
Copy Markdown
Author

@greptileai I will skipping all three none of them are stated in the loaded docs.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 1, 2026

That's not accurate — the docs do cover all three:

Memory allocationdoc/developer/memtypes.rst explicitly documents XMALLOC, XCALLOC, XREALLOC, XSTRDUP, XFREE, DECLARE_MTYPE, DEFINE_MTYPE, and DEFINE_MTYPE_STATIC, along with the convention to prefer DEFINE_MTYPE_STATIC for module-private types (>80% of cases, per the doc).

Loggingdoc/developer/logging.rst documents the zlog_*/flog_* API and explicitly states:

"Most (or all?) debug messages should be behind a debug foobar category switch that controls which subset of these messages is currently interesting and thus printed. If a debug message doesn't have such a guard, there should be a good explanation as to why."

String safetydoc/developer/workflow.rst is the listed source for this rule. The full file is 81KB and was truncated in retrieval, but it's explicitly listed in .greptile/files.json as covering the string-safety policy. If you believe the specific strlcpy/strlcat/snprintf enforcement is not in that file, I'd welcome you pointing to the relevant section — but the doc context loaded for this rule does reference workflow.rst as its source.

The rules are grounded in the loaded documentation. Skipping all three would mean ignoring FRR's documented coding standards.

Tip: You can give me more context with [.greptile/rules.md](https://www.greptile.com/docs/code-review/custom-standards) so I get this right next time.

@suphawitwanl
Copy link
Copy Markdown
Author

@greptileai

To clarify: my comment was about your three suggested additions (strdup/strndup, strncpy/strncat, fprintf), not the existing rules — those are already grounded.

  • workflow.rst:910: "strcpy, strcat and sprintf are unacceptable without exception." That enumeration is exhaustive — strncpy/strncat are not in it.
  • memtypes.rst:114-116 defines XSTRDUP as a wrapper for strdup, but contains no ban on strdup/strndup.
  • logging.rst documents zlog_*/flog_* and the debug-guard requirement, but contains no prohibition on fprintf.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 1, 2026

You're right, and I appreciate the precise citations. My suggested additions overreached what the documentation actually establishes:

  • The string-safety ban in workflow.rst:910 is explicit and exhaustive: strcpy, strcat, sprintf only. strncpy/strncat are not listed.
  • memtypes.rst establishes XSTRDUP as the replacement for strdup, but that's a convention/wrapper definition — not a documented prohibition on strdup/strndup.
  • logging.rst defines what should be used, but doesn't explicitly prohibit fprintf.

My suggestions were inferred intent rather than documented policy. The existing rules are correctly scoped to what the docs actually say, and I shouldn't have proposed extending them without that grounding. Fair correction.

Tip: You can give me more context with [.greptile/rules.md](https://www.greptile.com/docs/code-review/custom-standards) so I get this right next time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant