scripts/coccinelle/api/alloc/alloc_cast.cocci

Source file repositories/reference/linux-study-clean/scripts/coccinelle/api/alloc/alloc_cast.cocci

File Facts

System
Linux kernel
Corpus path
scripts/coccinelle/api/alloc/alloc_cast.cocci
Extension
.cocci
Size
3642 bytes
Lines
124
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
/// Remove casting the values returned by memory allocation functions
/// like kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc.
///
//# This makes an effort to find cases of casting of values returned by
//# kmalloc, kzalloc, kcalloc, kmem_cache_alloc, kmem_cache_zalloc,
//# kmem_cache_alloc_node, kmalloc_node and kzalloc_node and removes
//# the casting as it is not required. The result in the patch case may
//# need some reformatting.
//
// Confidence: High
// Copyright: (C) 2014 Himangi Saraogi
// Copyright: (C) 2017 Himanshu Jha
// Comments:
// Options: --no-includes --include-headers
//

virtual context
virtual patch
virtual org
virtual report

@initialize:python@
@@
import re
pattern = '__'
m = re.compile(pattern)

@r1 depends on context || patch@
type T;
@@

  (T *)
  \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
   dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
   kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
   pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
   kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)

//----------------------------------------------------------
//  For context mode
//----------------------------------------------------------

@script:python depends on context@
t << r1.T;
@@

if m.search(t) != None:
        cocci.include_match(False)

@depends on context && r1@
type r1.T;
@@

* (T *)
  \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
   kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
   dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
   kvmalloc\|kvzalloc\|kvmalloc_node\|kvzalloc_node\|pci_alloc_consistent\|
   pci_zalloc_consistent\|kmem_alloc\|kmem_zalloc\|kmem_zone_alloc\|
   kmem_zone_zalloc\|vmalloc_node\|vzalloc_node\)(...)

//----------------------------------------------------------
//  For patch mode
//----------------------------------------------------------

@script:python depends on patch@
t << r1.T;
@@

Annotation

Implementation Notes