[scripts] introduce static analysis tool - coccicheck#519
Open
TzengKyle wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a Coccinelle-based static analysis workflow (“coccicheck”) to LK by introducing a runner script and a small initial set of .cocci rules, aiming to help detect and optionally refactor common C pitfalls and style issues across LK sources.
Changes:
- Add
scripts/do-coccicheckto runspatchagainst a built project’ssrcfiles.txt/include_paths.txtwith optional filtering and parallelism. - Introduce initial Coccinelle rules under
scripts/coccinelle/(API usage, misc cleanups, free-related patterns).
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| scripts/do-coccicheck | Adds the coccicheck runner script (argument parsing, script selection, spatch invocation, output filtering). |
| scripts/coccinelle/misc/semicolon.cocci | Rule to detect/remove redundant semicolons and ;; patterns. |
| scripts/coccinelle/misc/noderef.cocci | Rule to report/patch sizeof(ptr)-style mistakes. |
| scripts/coccinelle/misc/flexible_array.cocci | Rule to report (and partially patch) deprecated trailing array patterns. |
| scripts/coccinelle/misc/countof.cocci | Rule to report/patch opencoded array-length division into countof(...). |
| scripts/coccinelle/free/ifnullfree.cocci | Rule to remove redundant NULL checks before free(). |
| scripts/coccinelle/api/strdup.cocci | Rule to report/patch manual malloc+copy into strdup(). |
| scripts/coccinelle/api/alloc_cast.cocci | Rule to remove redundant allocation return casts in C code. |
Contributor
Author
|
@copilot review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introdution
Currently, the Little Kernel ecosystem is devoid of robust static analysis utilities for the efficient identification of prospective defects.
Considering that the Linux kernel utilizes various static analysis infrastructures to assist contributors in validating source code against contemporary C standards and neutralizing typical anti-patterns linked to system instability, I suggest the incorporation of
coccicheckinto the Little Kernel codebase.Usage
New Files Introduction
This update introduces
scripts/do-coccicheck, a script that enables developers to executecoccicheckvia thespatchcommand, with terminal output filtered for improved readability. Additionally, a new directory,scripts/coccinelle, has been added to the codebase. This directory includes subfolders such asmisc,free, andapi, which categorize different.coccifiles, mirroring the structure used in the Linux kernel.Rationale
Coccicheck provides powerful static analysis capabilities. Below is an overview of the issues it effectively identifies:
countof.cocciidentifies patterns likesizeof(x) / sizeof(x[0]), which should be replaced with thecountof(x)macro. Similarly,strdup.coccirefactors manual string duplication (e.g.,malloc(strlen(s) + 1)followed bystrcpy/memcpy) to usestrdup().ifnullfree.cocci: Removes redundant null checks, asfree()safely acceptsNULLpointers.alloc_cast.cocci: Removes unnecessary pointer casts on memory allocation return values (common in C++ but redundant in C).flexible_array.cocci: Replaces deprecated zero-length (arr[0]) or one-element (arr[1]) trailing arrays with standard C99 flexible-array members (arr[]).noderef.cocciidentifies errors wheresizeof(x)is applied to a pointer variablex. (This fix has been merged: #517)semicolon.coccicleans up code by removing redundant double semicolons (;;) and unnecessary empty statement semicolons following code blocks or switch cases.