Skip to content

Commit 34713cb

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 34713cb

11 files changed

Lines changed: 1077 additions & 178 deletions

File tree

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/doc/y2038.md

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