0Pricing
C++ Academy · Lesson

UBSan and TSan

Find undefined behavior and races.

UBSan and TSan is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two More Sanitizers

Beyond ASan, two sanitizers target different bug classes.

  • UBSan: undefined behavior (overflow, bad shifts, null deref)
  • TSan: data races in multithreaded code

Enabling UBSan

UndefinedBehaviorSanitizer is enabled with -fsanitize=undefined. It can combine with ASan.

g++ -fsanitize=undefined -g main.cpp -o app
./app

Signed Integer Overflow

Signed overflow is undefined behavior in C++. UBSan reports it at the exact line.

#include <climits>
#include <iostream>

int main() {
    int x = INT_MAX;
    int y = x + 1; // signed overflow: UB
    std::cout << y << "\n";
    return 0;
}

Other UB UBSan Catches

UBSan flags many subtle errors.

  • Out-of-range shifts (x << 40 for 32-bit int)
  • Null pointer dereference
  • Misaligned access
  • Invalid enum or bool values

Making UBSan Halt

By default UBSan prints and continues. Add -fno-sanitize-recover=undefined to abort on the first error, which is better for CI.

g++ -fsanitize=undefined -fno-sanitize-recover=undefined -g main.cpp -o app

Enabling TSan

ThreadSanitizer is enabled with -fsanitize=thread. It detects data races: two threads accessing the same memory without synchronization, where at least one writes.

g++ -fsanitize=thread -g main.cpp -o app -pthread
./app

A Data Race Example

Two threads increment a shared counter without a lock. TSan reports the racing accesses and their stacks.

#include <thread>
#include <iostream>

int counter = 0;

void work() { for (int i = 0; i < 1000; ++i) ++counter; }

int main() {
    std::thread t1(work), t2(work);
    t1.join();
    t2.join();
    std::cout << counter << "\n";
    return 0;
}

Fixing the Race

Protect shared data with a mutex or use std::atomic. After fixing, TSan reports no races.

#include <thread>
#include <atomic>
#include <iostream>

std::atomic<int> counter{0};

void work() { for (int i = 0; i < 1000; ++i) ++counter; }

int main() {
    std::thread t1(work), t2(work);
    t1.join();
    t2.join();
    std::cout << counter << "\n";
    return 0;
}

TSan vs ASan Compatibility

TSan cannot be combined with ASan in the same build (they conflict). Use separate build configurations: one for ASan+UBSan, another for TSan.

Sanitizer Build Matrix

A practical CI setup runs the test suite in two sanitizer builds.

  • Build A: -fsanitize=address,undefined
  • Build B: -fsanitize=thread

This covers memory, UB, and concurrency bugs.

Cost and Limits

TSan can slow programs 5-15x and use much more memory. Like all sanitizers, it only catches bugs on executed code paths, so good multithreaded tests matter.

Quick Check

Recall what TSan detects.

Recap

You learned UBSan and TSan.

  • UBSan (-fsanitize=undefined) finds overflow, bad shifts, null deref
  • Use -fno-sanitize-recover to abort on first UB
  • TSan (-fsanitize=thread) finds data races; fix with mutex/atomic
  • TSan and ASan need separate builds

Frequently asked questions

Is the “UBSan and TSan” lesson free?

Yes — the full text of “UBSan and TSan” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “UBSan and TSan”?

Find undefined behavior and races. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “UBSan and TSan” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Using gdb and lldb
  2. AddressSanitizer
  3. UBSan and TSan
  4. Valgrind Basics
← Back to C++ Academy