Skip to content

Commit bb5cb6b

Browse files
Hiesxhmlukeb
authored andcommitted
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 bb5cb6b

14 files changed

Lines changed: 903 additions & 181 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.bak
22
*.gcno
3+
*.gch
34
*.o
45
*.pyc
56
/cppcheck

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Addons are scripts that analyses Cppcheck dump files to check compatibility with
77
+ [misra.py](https://github.com/danmar/cppcheck/blob/main/addons/misra.py)
88
Used to verify compliance with MISRA C 2012 - a proprietary set of guidelines to avoid such questionable code, developed for embedded systems. Since this standard is proprietary, cppcheck does not display error text by specifying only the number of violated rules (for example, [c2012-21.3]). If you want to display full texts for violated rules, you will need to create a text file containing MISRA rules, which you will have to pass when calling the script with `--rule-texts` key. Some examples of rule texts files available in [tests directory](https://github.com/danmar/cppcheck/blob/main/addons/test/misra/).
99
+ [y2038.py](https://github.com/danmar/cppcheck/blob/main/addons/y2038.py)
10-
Checks Linux system for [year 2038 problem](https://en.wikipedia.org/wiki/Year_2038_problem) safety. This required [modified environment](https://github.com/3adev/y2038). See complete description [here](https://github.com/danmar/cppcheck/blob/main/addons/doc/y2038.txt).
10+
Checks code for [year 2038 problem](https://en.wikipedia.org/wiki/Year_2038_problem) safety. See complete description [here](https://github.com/danmar/cppcheck/blob/main/addons/doc/y2038.md).
1111
+ [threadsafety.py](https://github.com/danmar/cppcheck/blob/main/addons/threadsafety.py)
1212
Analyse Cppcheck dump files to locate threadsafety issues like static local objects used by multiple threads.
1313
+ [naming.py](https://github.com/danmar/cppcheck/blob/main/addons/naming.py)
@@ -50,6 +50,11 @@ Addons are scripts that analyses Cppcheck dump files to check compatibility with
5050
cppcheck --addon=misc src/test.c
5151
```
5252

53+
For project-wide analysis with compile_commands.json:
54+
```bash
55+
cppcheck --project=build/compile_commands.json --addon=y2038
56+
```
57+
5358
It is also possible to call scripts as follows:
5459
```bash
5560
cppcheck --dump --quiet src/test.c

addons/doc/y2038.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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 64-bit signed quantity.
31+
Some systems, mainly embedded systems and older systems, still use a 32-bit signed
32+
time_t representation.
33+
34+
On January 19th, 2038 at 03:14:07 GMT, such 32-bit representations will reach
35+
their maximum positive value.
36+
37+
What happens then is unpredictable: system time might roll back to December
38+
13th, 1901 at 19:55:13, or it might keep running on until February 7th, 2106
39+
at 06:28:15 GMT, or the computer may freeze, or just about anything you can
40+
think of, plus a few ones you can't.
41+
42+
The workaround for this is to switch to a 64-bit signed representation of time
43+
as seconds from the Unix epoch. This representation will work for more than 250
44+
billion years.
45+
46+
Working around Y2038 requires fixing the Linux kernel, the C libraries, and
47+
any user code around which uses 32-bit epoch representations.
48+
49+
There is Y2038-proofing work in progress on the Linux and GNU glibc front.
50+
51+
## What is the Y2038 cppcheck addon?
52+
53+
The Y2038 cppcheck addon is a tool to help detect code which might need fixing
54+
because it is Y2038-unsafe. This may be because it uses types or functions from
55+
GNU libc or from the Linux kernel which are known not to be Y2038-proof.
56+
57+
## How does the Y2038 cppcheck addon work?
58+
59+
The Y2038 addon is a comprehensive tool designed to audit your project for Y2038 compliance. It provides a streamlined, intelligent approach to Y2038 analysis.
60+
61+
### Primary Usage: Cppcheck Integration with Project Files
62+
63+
The Y2038 addon integrates seamlessly with cppcheck's core project parsing infrastructure. For optimal analysis, use the addon with project files:
64+
65+
```bash
66+
cppcheck --project=build/compile_commands.json --addon=y2038
67+
```
68+
69+
For single files, you can also use:
70+
```bash
71+
cppcheck --addon=y2038 source_file.c
72+
```
73+
74+
#### Implementation Details
75+
76+
The addon leverages cppcheck's built-in project parsing capabilities:
77+
78+
- **Core Integration**: Y2038-related compiler flags are extracted by cppcheck core during project parsing and passed through dump file configuration
79+
- **Automatic Flag Detection**: Cppcheck automatically detects Y2038-relevant flags (`-D_TIME_BITS=64`, `-D_FILE_OFFSET_BITS=64`, `-D_USE_TIME_BITS64`) from compilation commands
80+
- **Clean Architecture**: No redundant file parsing - cppcheck handles project files once, addon focuses on analysis
81+
- **Priority Logic**: Dump file configuration (from cppcheck's project parsing) takes precedence over source code `#define` statements
82+
- **Source Fallback**: When no project configuration is available, the addon analyzes source code `#define` statements
83+
84+
This architecture ensures optimal performance and maintains clean separation of concerns between cppcheck core (project parsing) and addon (analysis logic).
85+
86+
The output is the standard Cppcheck analysis report, focused on Y2038-related issues.
87+
88+
## Requirements
89+
90+
The Y2038 addon works with any cppcheck installation and requires no additional dependencies beyond cppcheck itself.
91+
92+
For optimal Y2038 analysis, ensure your project uses a supported build system that generates `compile_commands.json`:
93+
94+
- **CMake**: Use `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`
95+
- **Bear**: For Make/Autotools projects, use `bear` to generate compile commands
96+
- **Ninja**: Use `ninja -t compdb` to generate compile commands
97+
- **Bazel**: Use `bazel aquery` with appropriate flags
98+
99+
If using `bear` for Make-based projects, install it via your package manager:
100+
101+
```bash
102+
# On Debian/Ubuntu
103+
sudo apt-get install bear
104+
105+
# On Fedora
106+
sudo dnf install bear
107+
108+
# On macOS (using Homebrew)
109+
brew install bear
110+
```
111+
112+
## How to use the Y2038 cppcheck addon
113+
114+
### **Auditing Your Project for Y2038 Compliance**
115+
116+
The Y2038 addon seamlessly integrates with your existing cppcheck workflow.
117+
118+
**For projects with compile_commands.json (recommended):**
119+
120+
```bash
121+
cppcheck --project=build/compile_commands.json --addon=y2038
122+
```
123+
124+
**For single file analysis:**
125+
126+
```bash
127+
cppcheck --addon=y2038 source_file.c
128+
```
129+
130+
**For project-wide analysis without compile_commands.json:**
131+
132+
```bash
133+
cppcheck --addon=y2038 src/
134+
```
135+
136+
The integration automatically:
137+
138+
1. **Extracts Y2038 flags** from your project's compilation commands via cppcheck's project parsing
139+
2. **Passes flag information** through dump file configuration to the addon
140+
3. **Analyzes source code** with proper Y2038 context from both build system and source directives
141+
4. **Reports Y2038 issues** using cppcheck's standard error reporting format
142+
143+
### **CI/CD Integration**
144+
145+
For CI/CD integration, use the Y2038 addon with your project's build configuration:
146+
147+
```sh
148+
# Example CI script with compile_commands.json
149+
#!/bin/bash
150+
# Generate compile_commands.json (if not already available)
151+
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -B build
152+
# or: bear -- make
153+
154+
# Run Y2038 analysis
155+
cppcheck --project=build/compile_commands.json --addon=y2038 --error-exitcode=1
156+
157+
# The addon will return a non-zero exit code if Y2038 issues are found.
158+
# The output is the standard Cppcheck report.
159+
```
160+
161+
**For projects without compile_commands.json:**
162+
163+
```sh
164+
# Example CI script for source-only analysis
165+
#!/bin/bash
166+
cppcheck --addon=y2038 --error-exitcode=1 src/
167+
```

addons/doc/y2038.txt

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

0 commit comments

Comments
 (0)