|
| 1 | +/* |
| 2 | + File: vix_error_examples.cpp |
| 3 | + Purpose: |
| 4 | + This file contains commented C++ code snippets that intentionally |
| 5 | + trigger common C++ errors and runtime issues. |
| 6 | +
|
| 7 | + Each section demonstrates ONE specific error that Vix CLI is able |
| 8 | + to detect and explain. |
| 9 | +
|
| 10 | + ⚠️ Do NOT compile this file as-is. |
| 11 | + Uncomment ONE section at a time to test error handling. |
| 12 | +*/ |
| 13 | + |
| 14 | +#include <iostream> |
| 15 | +#include <memory> |
| 16 | +#include <vector> |
| 17 | +#include <string> |
| 18 | +#include <string_view> |
| 19 | +#include <thread> |
| 20 | + |
| 21 | +// ============================================================================ |
| 22 | +// [CPP001] cout not declared (missing <iostream> / std::) |
| 23 | +// ============================================================================ |
| 24 | + |
| 25 | +/* |
| 26 | +int main() |
| 27 | +{ |
| 28 | + cout << "Hello"; // error: 'cout' is not declared |
| 29 | +} |
| 30 | +*/ |
| 31 | + |
| 32 | +// ============================================================================ |
| 33 | +// [CPP002] header not found |
| 34 | +// ============================================================================ |
| 35 | + |
| 36 | +/* |
| 37 | +#include "missing.hpp" // fatal error: file not found |
| 38 | +int main() {} |
| 39 | +*/ |
| 40 | + |
| 41 | +// ============================================================================ |
| 42 | +// [CPP003] missing semicolon |
| 43 | +// ============================================================================ |
| 44 | + |
| 45 | +/* |
| 46 | +int main() |
| 47 | +{ |
| 48 | + int x = 42 // error: expected ';' |
| 49 | + std::cout << x << "\n"; |
| 50 | +} |
| 51 | +*/ |
| 52 | + |
| 53 | +// ============================================================================ |
| 54 | +// [CPP004] streaming std::vector to ostream |
| 55 | +// ============================================================================ |
| 56 | + |
| 57 | +/* |
| 58 | +int main() |
| 59 | +{ |
| 60 | + std::vector<int> v{1, 2, 3}; |
| 61 | + std::cout << v << "\n"; // error: invalid operands to operator<< |
| 62 | +} |
| 63 | +*/ |
| 64 | + |
| 65 | +// ============================================================================ |
| 66 | +// [CPP005] overload ambiguity with nullptr |
| 67 | +// ============================================================================ |
| 68 | + |
| 69 | +/* |
| 70 | +void process(int) {} |
| 71 | +template <typename T> |
| 72 | +void process(T*) {} |
| 73 | +
|
| 74 | +int main() |
| 75 | +{ |
| 76 | + process(nullptr); // error: no matching function |
| 77 | +} |
| 78 | +*/ |
| 79 | + |
| 80 | +// ============================================================================ |
| 81 | +// [CPP006] use-after-move |
| 82 | +// ============================================================================ |
| 83 | + |
| 84 | +/* |
| 85 | +struct Obj { void run() {} }; |
| 86 | +
|
| 87 | +int main() |
| 88 | +{ |
| 89 | + auto ptr = std::make_unique<Obj>(); |
| 90 | + auto other = std::move(ptr); |
| 91 | + ptr->run(); // error: use-after-move |
| 92 | +} |
| 93 | +*/ |
| 94 | + |
| 95 | +// ============================================================================ |
| 96 | +// [MEM001] unique_ptr copy (ownership violation) |
| 97 | +// ============================================================================ |
| 98 | + |
| 99 | +/* |
| 100 | +int main() |
| 101 | +{ |
| 102 | + std::unique_ptr<int> a = std::make_unique<int>(42); |
| 103 | + auto b = a; // error: use of deleted copy constructor |
| 104 | +} |
| 105 | +*/ |
| 106 | + |
| 107 | +// ============================================================================ |
| 108 | +// [MEM002] shared_ptr raw pointer double delete |
| 109 | +// ============================================================================ |
| 110 | + |
| 111 | +/* |
| 112 | +int main() |
| 113 | +{ |
| 114 | + int* p = new int(5); |
| 115 | + std::shared_ptr<int> a(p); |
| 116 | + std::shared_ptr<int> b(p); // runtime double delete |
| 117 | +} |
| 118 | +*/ |
| 119 | + |
| 120 | +// ============================================================================ |
| 121 | +// [MEM003] delete vs delete[] |
| 122 | +// ============================================================================ |
| 123 | + |
| 124 | +/* |
| 125 | +int main() |
| 126 | +{ |
| 127 | + int* arr = new int[10]; |
| 128 | + delete arr; // mismatched delete |
| 129 | +} |
| 130 | +*/ |
| 131 | + |
| 132 | +// ============================================================================ |
| 133 | +// [MEM004] dangling std::string_view |
| 134 | +// ============================================================================ |
| 135 | + |
| 136 | +/* |
| 137 | +int main() |
| 138 | +{ |
| 139 | + std::string_view sv = std::string("hello"); |
| 140 | + std::cout << sv << "\n"; // dangling view |
| 141 | +} |
| 142 | +*/ |
| 143 | + |
| 144 | +// ============================================================================ |
| 145 | +// [MEM005] returning reference to local variable |
| 146 | +// ============================================================================ |
| 147 | + |
| 148 | +/* |
| 149 | +const std::string& bad() |
| 150 | +{ |
| 151 | + std::string s = "hello"; |
| 152 | + return s; // dangling reference |
| 153 | +} |
| 154 | +*/ |
| 155 | + |
| 156 | +// ============================================================================ |
| 157 | +// [MEM006] use of uninitialized variable |
| 158 | +// ============================================================================ |
| 159 | + |
| 160 | +/* |
| 161 | +int main() |
| 162 | +{ |
| 163 | + int x; |
| 164 | + std::cout << x << "\n"; // uninitialized read |
| 165 | +} |
| 166 | +*/ |
| 167 | + |
| 168 | +// ============================================================================ |
| 169 | +// [RT001] double free / invalid free |
| 170 | +// ============================================================================ |
| 171 | + |
| 172 | +/* |
| 173 | +int main() |
| 174 | +{ |
| 175 | + int* p = (int*)malloc(sizeof(int)); |
| 176 | + free(p); |
| 177 | + free(p); // double free |
| 178 | +} |
| 179 | +*/ |
| 180 | + |
| 181 | +// ============================================================================ |
| 182 | +// [RT002] heap-buffer-overflow |
| 183 | +// ============================================================================ |
| 184 | + |
| 185 | +/* |
| 186 | +int main() |
| 187 | +{ |
| 188 | + int* a = new int[3]; |
| 189 | + a[3] = 42; // heap-buffer-overflow |
| 190 | +} |
| 191 | +*/ |
| 192 | + |
| 193 | +// ============================================================================ |
| 194 | +// [RT003] stack-buffer-overflow |
| 195 | +// ============================================================================ |
| 196 | + |
| 197 | +/* |
| 198 | +int main() |
| 199 | +{ |
| 200 | + char buf[4]; |
| 201 | + buf[10] = 'x'; // stack-buffer-overflow |
| 202 | +} |
| 203 | +*/ |
| 204 | + |
| 205 | +// ============================================================================ |
| 206 | +// [RT004] use-after-free |
| 207 | +// ============================================================================ |
| 208 | + |
| 209 | +/* |
| 210 | +int main() |
| 211 | +{ |
| 212 | + int* p = new int(5); |
| 213 | + delete p; |
| 214 | + std::cout << *p << "\n"; // use-after-free |
| 215 | +} |
| 216 | +*/ |
| 217 | + |
| 218 | +// ============================================================================ |
| 219 | +// [UB001] signed integer overflow (UBSan) |
| 220 | +// ============================================================================ |
| 221 | + |
| 222 | +/* |
| 223 | +int main() |
| 224 | +{ |
| 225 | + int x = 2147483647; |
| 226 | + x += 1; // signed overflow |
| 227 | +} |
| 228 | +*/ |
| 229 | + |
| 230 | +// ============================================================================ |
| 231 | +// [UB002] null pointer dereference |
| 232 | +// ============================================================================ |
| 233 | + |
| 234 | +/* |
| 235 | +struct Node { int v; }; |
| 236 | +
|
| 237 | +int main() |
| 238 | +{ |
| 239 | + Node* n = nullptr; |
| 240 | + std::cout << n->v << "\n"; // UB |
| 241 | +} |
| 242 | +*/ |
| 243 | + |
| 244 | +// ============================================================================ |
| 245 | +// [TS001] data race (ThreadSanitizer) |
| 246 | +// ============================================================================ |
| 247 | + |
| 248 | +/* |
| 249 | +int shared = 0; |
| 250 | +
|
| 251 | +void worker() |
| 252 | +{ |
| 253 | + for (int i = 0; i < 1000; ++i) |
| 254 | + shared++; |
| 255 | +} |
| 256 | +
|
| 257 | +int main() |
| 258 | +{ |
| 259 | + std::thread t1(worker); |
| 260 | + std::thread t2(worker); |
| 261 | + t1.join(); |
| 262 | + t2.join(); |
| 263 | +} |
| 264 | +*/ |
| 265 | + |
| 266 | +// ============================================================================ |
| 267 | +// End of file |
| 268 | +// ============================================================================ |
0 commit comments