scripts/coccinelle/misc/do_div.cocci
Source file repositories/reference/linux-study-clean/scripts/coccinelle/misc/do_div.cocci
File Facts
- System
- Linux kernel
- Corpus path
scripts/coccinelle/misc/do_div.cocci- Extension
.cocci- Size
- 3560 bytes
- Lines
- 156
- 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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-only
/// do_div() does a 64-by-32 division.
/// When the divisor is long, unsigned long, u64, or s64,
/// do_div() truncates it to 32 bits, this means it can test
/// non-zero and be truncated to 0 for division on 64bit platforms.
///
//# This makes an effort to find those inappropriate do_div() calls.
//
// Confidence: Moderate
// Copyright: (C) 2020 Wen Yang, Alibaba.
// Comments:
// Options: --no-includes --include-headers
virtual context
virtual org
virtual report
@initialize:python@
@@
def get_digit_type_and_value(str):
is_digit = False
value = 0
try:
if (str.isdigit()):
is_digit = True
value = int(str, 0)
elif (str.upper().endswith('ULL')):
is_digit = True
value = int(str[:-3], 0)
elif (str.upper().endswith('LL')):
is_digit = True
value = int(str[:-2], 0)
elif (str.upper().endswith('UL')):
is_digit = True
value = int(str[:-2], 0)
elif (str.upper().endswith('L')):
is_digit = True
value = int(str[:-1], 0)
elif (str.upper().endswith('U')):
is_digit = True
value = int(str[:-1], 0)
except Exception as e:
print('Error:',e)
is_digit = False
value = 0
finally:
return is_digit, value
def filter_out_safe_constants(str):
is_digit, value = get_digit_type_and_value(str)
if (is_digit):
if (value >= 0x100000000):
return True
else:
return False
else:
return True
def construct_warnings(suggested_fun):
msg="WARNING: do_div() does a 64-by-32 division, please consider using %s instead."
return msg % suggested_fun
@depends on context@
expression f;
long l: script:python() { filter_out_safe_constants(l) };
unsigned long ul : script:python() { filter_out_safe_constants(ul) };
u64 ul64 : script:python() { filter_out_safe_constants(ul64) };
s64 sl64 : script:python() { filter_out_safe_constants(sl64) };
Annotation
- Atlas domain: Support Tooling And Documentation / scripts.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.