|
| 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 Integration with Project Files |
| 60 | + |
| 61 | +The Y2038 addon integrates seamlessly with cppcheck's core project parsing infrastructure. For optimal analysis, use the addon with project files: |
| 62 | + |
| 63 | +```bash |
| 64 | +cppcheck --project=build/compile_commands.json --addon=y2038 |
| 65 | +``` |
| 66 | + |
| 67 | +For single files, you can also use: |
| 68 | +```bash |
| 69 | +cppcheck --addon=y2038 source_file.c |
| 70 | +``` |
| 71 | + |
| 72 | +#### Implementation Details |
| 73 | + |
| 74 | +The addon leverages cppcheck's built-in project parsing capabilities: |
| 75 | + |
| 76 | +- **Core Integration**: Y2038-related compiler flags are extracted by cppcheck core during project parsing and passed through dump file configuration |
| 77 | +- **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 |
| 78 | +- **Clean Architecture**: No redundant file parsing - cppcheck handles project files once, addon focuses on analysis |
| 79 | +- **Priority Logic**: Dump file configuration (from cppcheck's project parsing) takes precedence over source code `#define` statements |
| 80 | +- **Source Fallback**: When no project configuration is available, the addon analyzes source code `#define` statements |
| 81 | + |
| 82 | +This architecture ensures optimal performance and maintains clean separation of concerns between cppcheck core (project parsing) and addon (analysis logic). |
| 83 | + |
| 84 | +The output is the standard Cppcheck analysis report, focused on Y2038-related issues. |
| 85 | + |
| 86 | +## Requirements |
| 87 | + |
| 88 | +The Y2038 addon works with any cppcheck installation and requires no additional dependencies beyond cppcheck itself. |
| 89 | + |
| 90 | +For optimal Y2038 analysis, ensure your project uses a supported build system that generates `compile_commands.json`: |
| 91 | + |
| 92 | +- **CMake**: Use `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON` |
| 93 | +- **Bear**: For Make/Autotools projects, use `bear` to generate compile commands |
| 94 | +- **Ninja**: Use `ninja -t compdb` to generate compile commands |
| 95 | +- **Bazel**: Use `bazel aquery` with appropriate flags |
| 96 | + |
| 97 | +If using `bear` for Make-based projects, install it via your package manager: |
| 98 | + |
| 99 | +```bash |
| 100 | +# On Debian/Ubuntu |
| 101 | +sudo apt-get install bear |
| 102 | + |
| 103 | +# On Fedora |
| 104 | +sudo dnf install bear |
| 105 | + |
| 106 | +# On macOS (using Homebrew) |
| 107 | +brew install bear |
| 108 | +``` |
| 109 | + |
| 110 | +## How to use the Y2038 cppcheck addon |
| 111 | + |
| 112 | +### **Auditing Your Project for Y2038 Compliance** |
| 113 | + |
| 114 | +The Y2038 addon seamlessly integrates with your existing cppcheck workflow. |
| 115 | + |
| 116 | +**For projects with compile_commands.json (recommended):** |
| 117 | + |
| 118 | +```bash |
| 119 | +cppcheck --project=build/compile_commands.json --addon=y2038 |
| 120 | +``` |
| 121 | + |
| 122 | +**For single file analysis:** |
| 123 | + |
| 124 | +```bash |
| 125 | +cppcheck --addon=y2038 source_file.c |
| 126 | +``` |
| 127 | + |
| 128 | +**For project-wide analysis without compile_commands.json:** |
| 129 | + |
| 130 | +```bash |
| 131 | +cppcheck --addon=y2038 src/ |
| 132 | +``` |
| 133 | + |
| 134 | +The integration automatically: |
| 135 | + |
| 136 | +1. **Extracts Y2038 flags** from your project's compilation commands via cppcheck's project parsing |
| 137 | +2. **Passes flag information** through dump file configuration to the addon |
| 138 | +3. **Analyzes source code** with proper Y2038 context from both build system and source directives |
| 139 | +4. **Reports Y2038 issues** using cppcheck's standard error reporting format |
| 140 | + |
| 141 | +### **CI/CD Integration** |
| 142 | + |
| 143 | +For CI/CD integration, use the Y2038 addon with your project's build configuration: |
| 144 | + |
| 145 | +```sh |
| 146 | +# Example CI script with compile_commands.json |
| 147 | +#!/bin/bash |
| 148 | +# Generate compile_commands.json (if not already available) |
| 149 | +cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -B build |
| 150 | +# or: bear -- make |
| 151 | + |
| 152 | +# Run Y2038 analysis |
| 153 | +cppcheck --project=build/compile_commands.json --addon=y2038 --error-exitcode=1 |
| 154 | + |
| 155 | +# The addon will return a non-zero exit code if Y2038 issues are found. |
| 156 | +# The output is the standard Cppcheck report. |
| 157 | +``` |
| 158 | + |
| 159 | +**For projects without compile_commands.json:** |
| 160 | + |
| 161 | +```sh |
| 162 | +# Example CI script for source-only analysis |
| 163 | +#!/bin/bash |
| 164 | +cppcheck --addon=y2038 --error-exitcode=1 src/ |
| 165 | +``` |
| 166 | + |
| 167 | +## Testing |
| 168 | + |
| 169 | +The Y2038 addon includes comprehensive test suites to ensure reliability and correctness: |
| 170 | + |
| 171 | +### Running Y2038 Addon Tests |
| 172 | + |
| 173 | +To run the Y2038 addon tests, execute: |
| 174 | + |
| 175 | +```bash |
| 176 | +# Run the main Y2038 addon tests |
| 177 | +python3 -m pytest addons/test/y2038_test.py -v |
| 178 | + |
| 179 | +# Run the build system integration tests |
| 180 | +python3 -m pytest addons/test/test_y2038_buildsystem.py -v |
| 181 | + |
| 182 | +# Run all Y2038-related tests |
| 183 | +python3 -m pytest addons/test/ -k y2038 -v |
| 184 | +``` |
| 185 | + |
| 186 | +### Test Coverage |
| 187 | + |
| 188 | +The test suite covers: |
| 189 | + |
| 190 | +- **Core Y2038 detection logic**: Testing identification of Y2038-unsafe functions and types |
| 191 | +- **Compiler flag parsing**: Validation of `_TIME_BITS`, `_FILE_OFFSET_BITS`, and `_USE_TIME_BITS64` detection |
| 192 | +- **Build system integration**: Testing automatic build system detection and `compile_commands.json` generation |
| 193 | +- **Priority-based flag resolution**: Ensuring build system flags take precedence over source directives |
| 194 | +- **Warning suppression**: Verifying proper Y2038-safe configuration detection and warning suppression |
| 195 | +- **Error reporting**: Testing accurate error messages and source attribution |
| 196 | + |
| 197 | +### Test Structure |
| 198 | + |
| 199 | +- `addons/test/y2038_test.py` - Core addon functionality tests |
| 200 | +- `addons/test/test_y2038_buildsystem.py` - Build system integration tests |
| 201 | + |
| 202 | +The tests use mock objects and temporary directories to simulate various project configurations and build systems without requiring actual build tools to be installed. |
0 commit comments