Skip to content

Commit cf40a64

Browse files
committed
y2038: eliminate false positives with automatic build system detection
The Y2038 addon currently generates false positive warnings when scanning codebases that are properly configured for Y2038 safety through build system flags, making it impractical for comprehensive codebase analysis. This prevents teams from running Y2038 checks across entire projects in CI/CD pipelines due to noise from correctly configured code. Add automatic build system detection to discover Y2038-related compiler flags (_TIME_BITS=64, _FILE_OFFSET_BITS=64, _USE_TIME_BITS64) from: - Makefile variants (Makefile, makefile, GNUmakefile, *.mk) - CMake files (CMakeLists.txt, *.cmake) - Meson build files (meson.build) - Autotools scripts (configure, configure.ac, configure.in) - Compiler flags passed via cppcheck -D options When proper Y2038 configuration is detected (both _TIME_BITS=64 AND _FILE_OFFSET_BITS=64), suppress Y2038 warnings and display an informational message indicating the configuration source. Implement hierarchical directory search up to 5 levels from source files to locate relevant build files, with flag precedence: build system > compiler flags > source code #define directives. Add performance optimizations: - Intelligent file caching with TTL-based invalidation - UTF-8 BOM handling for cross-platform compatibility - Robust import fallback system Extend test suite with comprehensive coverage: - Compiler flag parsing edge cases (18 test scenarios) - Build system detection for all supported formats - Caching behavior and performance validation - Cross-platform file encoding handling This enables organizations to run comprehensive Y2038 analysis on entire codebases without false positives from properly configured projects, while maintaining detection of actual Y2038 safety issues.
1 parent 4780cd2 commit cf40a64

File tree

12 files changed

+1077
-165
lines changed

12 files changed

+1077
-165
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ Ludvig Gunne Lindström
238238
Luis Díaz Más
239239
Luís Pereira
240240
Lukas Grützmacher
241+
Lukas Hiesmayr
241242
Lukasz Czajczyk
242243
Łukasz Jankowski
243244
Luxon Jean-Pierre

addons/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Addons are scripts that analyses Cppcheck dump files to check compatibility with
1616
Enforces naming conventions across the code. Enhanced version with support for type prefixes in variable and function names.
1717
+ [findcasts.py](https://github.com/danmar/cppcheck/blob/main/addons/findcasts.py)
1818
Locates casts in the code.
19+
+ [y2038_buildsystem.py](https://github.com/danmar/cppcheck/blob/main/addons/y2038_buildsystem.py)
20+
Detects and parses build system files to extract compiler flags for Y2038 analysis. Supports multiple build systems including Make, CMake, Meson, Autotools, and Bazel.
1921
+ [misc.py](https://github.com/danmar/cppcheck/blob/main/addons/misc.py)
2022
Performs miscellaneous checks.
2123

addons/doc/y2038.md

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# README of the Y2038 cppcheck addon
2+
3+
## Contents
4+
5+
- [README of the Y2038 cppcheck addon](#readme-of-the-y2038-cppcheck-addon)
6+
- [Contents](#contents)
7+
- [What is Y2038?](#what-is-y2038)
8+
- [What is the Y2038 cppcheck addon?](#what-is-the-y2038-cppcheck-addon)
9+
- [How does the Y2038 cppcheck addon work?](#how-does-the-y2038-cppcheck-addon-work)
10+
- [Requirements](#requirements)
11+
- [How to use the Y2038 cppcheck addon](#how-to-use-the-y2038-cppcheck-addon)
12+
- [**Auditing Your Project for Y2038 Compliance**](#auditing-your-project-for-y2038-compliance)
13+
- [**CI/CD Integration**](#cicd-integration)
14+
15+
---
16+
17+
## What is Y2038?
18+
19+
In a few words:
20+
21+
In Linux, the current date and time is kept as the number of seconds elapsed
22+
since the Unix epoch, that is, since January 1st, 1970 at 00:00:00 GMT.
23+
24+
Most of the time, this representation is stored as a 32-bit signed quantity.
25+
26+
On January 19th, 2038 at 03:14:07 GMT, such 32-bit representations will reach
27+
their maximum positive value.
28+
29+
What happens then is unpredictable: system time might roll back to December
30+
13th, 1901 at 19:55:13, or it might keep running on until February 7th, 2106
31+
at 06:28:15 GMT, or the computer may freeze, or just about anything you can
32+
think of, plus a few ones you can't.
33+
34+
The workaround for this is to switch to a 64-bit signed representation of time
35+
as seconds from the Unix epoch. This representation will work for more than 250
36+
billion years.
37+
38+
Working around Y2038 requires fixing the Linux kernel, the C libraries, and
39+
any user code around which uses 32-bit epoch representations.
40+
41+
There is Y2038-proofing work in progress on the Linux and GNU glibc front.
42+
43+
## What is the Y2038 cppcheck addon?
44+
45+
The Y2038 cppcheck addon is a tool to help detect code which might need fixing
46+
because it is Y2038-unsafe. This may be because it uses types or functions from
47+
GNU libc or from the Linux kernel which are known not to be Y2038-proof.
48+
49+
## How does the Y2038 cppcheck addon work?
50+
51+
The Y2038 addon is a comprehensive tool designed to audit your project for Y2038 compliance. It provides a streamlined, intelligent approach to Y2038 analysis.
52+
53+
### Primary Usage: Cppcheck Addon Integration (`y2038.py`)
54+
55+
The main addon `addons/y2038.py` is designed to be used directly with cppcheck using the command:
56+
57+
```bash
58+
cppcheck --addon=addons/y2038.py source_file.c
59+
```
60+
61+
The addon implements intelligent flag detection with a simplified 2-tier priority system:
62+
63+
1. **Build system flags** (highest priority) - Extracted from `compile_commands.json` when available
64+
2. **Source code directives** (fallback) - `#define` statements in the source code
65+
66+
#### Implementation Details
67+
68+
The addon uses an intelligent, automated approach:
69+
70+
- **Automatic Build System Integration**: When analyzing a source file, the addon automatically detects if the project uses a build system (Make, CMake, Meson, Autotools) and generates `compile_commands.json` if needed using the helper library `y2038_buildsystem.py`
71+
- **Flag Extraction**: Parses compilation commands to extract Y2038-relevant flags (`_TIME_BITS`, `_FILE_OFFSET_BITS`, `_USE_TIME_BITS64`)
72+
- **Priority Logic**: If build system flags are found, they take complete precedence over any source code directives
73+
- **Source Fallback**: Only when no build system configuration is available, the addon analyzes source code `#define` statements
74+
75+
This architecture ensures seamless integration with any build system while maintaining the simplicity of direct cppcheck addon usage. The build system detection and `compile_commands.json` generation happens automatically behind the scenes when needed.
76+
77+
The output is the standard Cppcheck analysis report, focused on Y2038-related issues.
78+
79+
## Requirements
80+
81+
For Make-based and Autotools-based projects, the `y2038_buildsystem.py` script requires the `bear` utility to be installed and available in the system's `PATH`.
82+
83+
`bear` is used to intercept compiler calls during the build process and generate the `compile_commands.json` file, which is essential for Cppcheck to analyze your project correctly.
84+
85+
You can typically install `bear` using your system's package manager:
86+
87+
```
88+
# On Debian/Ubuntu
89+
sudo apt-get install bear
90+
91+
# On Fedora
92+
sudo dnf install bear
93+
94+
# On macOS (using Homebrew)
95+
brew install bear
96+
```
97+
98+
## How to use the Y2038 cppcheck addon
99+
100+
### **Auditing Your Project for Y2038 Compliance**
101+
102+
The Y2038 addon seamlessly integrates with your existing cppcheck workflow. Simply use the addon flag with cppcheck:
103+
104+
```bash
105+
cppcheck --addon=addons/y2038.py source_file.c
106+
```
107+
108+
**For project-wide analysis:**
109+
110+
```bash
111+
cppcheck --addon=addons/y2038.py src/
112+
```
113+
114+
The addon automatically:
115+
116+
1. **Detects your build system** (e.g., Make, CMake, Meson, Autotools) if present
117+
2. **Generates `compile_commands.json`** when needed for accurate analysis
118+
3. **Extracts Y2038-relevant compilation flags** from your build configuration
119+
4. **Analyzes source code** with proper Y2038 context
120+
121+
**Alternative: Direct build system script usage:**
122+
123+
For standalone build system analysis, you can still use the helper script directly:
124+
125+
```bash
126+
python3 addons/y2038_buildsystem.py /path/to/your/project
127+
```
128+
129+
### **CI/CD Integration**
130+
131+
For CI/CD integration, you can use the Y2038 addon directly with cppcheck:
132+
133+
```sh
134+
# Example CI script
135+
#!/bin/bash
136+
cppcheck --addon=addons/y2038.py --error-exitcode=1 src/
137+
138+
# The addon will return a non-zero exit code if Y2038 issues are found.
139+
# The output is the standard Cppcheck report.
140+
```
141+
142+
**Alternative CI approach using the build system helper:**
143+
144+
```sh
145+
# Example CI script for build system integration
146+
#!/bin/bash
147+
python3 addons/y2038_buildsystem.py /path/to/your/project
148+
```
149+
150+
## Testing
151+
152+
The Y2038 addon includes comprehensive test suites to ensure reliability and correctness:
153+
154+
### Running Y2038 Addon Tests
155+
156+
To run the Y2038 addon tests, execute:
157+
158+
```bash
159+
# Run the main Y2038 addon tests
160+
python3 -m pytest addons/test/y2038_test.py -v
161+
162+
# Run the build system integration tests
163+
python3 -m pytest addons/test/test_y2038_buildsystem.py -v
164+
165+
# Run all Y2038-related tests
166+
python3 -m pytest addons/test/ -k y2038 -v
167+
```
168+
169+
### Test Coverage
170+
171+
The test suite covers:
172+
173+
- **Core Y2038 detection logic**: Testing identification of Y2038-unsafe functions and types
174+
- **Compiler flag parsing**: Validation of `_TIME_BITS`, `_FILE_OFFSET_BITS`, and `_USE_TIME_BITS64` detection
175+
- **Build system integration**: Testing automatic build system detection and `compile_commands.json` generation
176+
- **Priority-based flag resolution**: Ensuring build system flags take precedence over source directives
177+
- **Warning suppression**: Verifying proper Y2038-safe configuration detection and warning suppression
178+
- **Error reporting**: Testing accurate error messages and source attribution
179+
180+
### Test Structure
181+
182+
- `addons/test/y2038_test.py` - Core addon functionality tests
183+
- `addons/test/test_y2038_buildsystem.py` - Build system integration tests
184+
185+
The tests use mock objects and temporary directories to simulate various project configurations and build systems without requiring actual build tools to be installed.

addons/doc/y2038.txt

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)