C++ Academy · Lesson

UBSan and TSan

Find undefined behavior and races.

Lesson 3 of 413 steps

UBSan and TSan is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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
Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “UBSan and TSan” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “UBSan and TSan”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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