scripts/coccinelle/misc/array_size_dup.cocci

Source file repositories/reference/linux-study-clean/scripts/coccinelle/misc/array_size_dup.cocci

File Facts

System
Linux kernel
Corpus path
scripts/coccinelle/misc/array_size_dup.cocci
Extension
.cocci
Size
4894 bytes
Lines
210
Domain
Support Tooling And Documentation
Bucket
scripts
Inferred role
Support Tooling And Documentation: scripts
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0-only
///
/// Check for array_size(), array3_size(), struct_size() duplicates.
/// These patterns are detected:
///  1. An opencoded expression is used before array_size() to compute the same size
///  2. An opencoded expression is used after array_size() to compute the same size
/// From security point of view only first case is relevant. These functions
/// perform arithmetic overflow check. Thus, if we use an opencoded expression
/// before a call to the *_size() function we can miss an overflow.
///
// Confidence: High
// Copyright: (C) 2020 Denis Efremov ISPRAS
// Options: --no-includes --include-headers --no-loops

virtual context
virtual report
virtual org

@as@
expression E1, E2;
@@

array_size(E1, E2)

@as_next@
expression subE1 <= as.E1;
expression subE2 <= as.E2;
expression as.E1, as.E2, E3;
assignment operator aop;
position p1, p2;
@@

* E1 * E2@p1
  ... when != \(subE1\|subE2\) aop E3
      when != &\(subE1\|subE2\)
* array_size(E1, E2)@p2

@script:python depends on report@
p1 << as_next.p1;
p2 << as_next.p2;
@@

msg = "WARNING: array_size is used later (line %s) to compute the same size" % (p2[0].line)
coccilib.report.print_report(p1[0], msg)

@script:python depends on org@
p1 << as_next.p1;
p2 << as_next.p2;
@@

msg = "WARNING: array_size is used later (line %s) to compute the same size" % (p2[0].line)
coccilib.org.print_todo(p1[0], msg)

@as_prev@
expression subE1 <= as.E1;
expression subE2 <= as.E2;
expression as.E1, as.E2, E3;
assignment operator aop;
position p1, p2;
@@

* array_size(E1, E2)@p1
  ... when != \(subE1\|subE2\) aop E3
      when != &\(subE1\|subE2\)
* E1 * E2@p2

@script:python depends on report@
p1 << as_prev.p1;
p2 << as_prev.p2;
@@

Annotation

Implementation Notes