Catching sanitizer errors programmatically

The C and C++ languages offer little protection against programmer errors. Errors do not always show up where you expect. You can silently corrupt the content of your memory. It can make bugs difficult to track. To solve this problem, I am a big fan of programming in C and C++ using sanitizers. They slow your program, but the check that memory accesses are safe, for example.

Thus if you iterate over an array and access elements that are out of bounds, a memory sanitizer will immediately catch the error:

  int array[8];
  for(int k = 0;; k++) {
    array[k] = 0;

The sanitizer reports the error, but what if you would like to catch the error and store it in some log? Thankfully, GCC and LLVM sanitizers call a function (__asan_on_error()) when when an error is encounter, allowing us to log it. Of course, you need to record the state of your program. The following is an example where the state is recorded in a string.

#include <iostream>
#include <string>
#include <stdlib.h>

std::string message;

extern "C" {
void __asan_on_error() {
  std::cout << "You caused an error: " << message << std::endl;

int main() {
  int array[8];
  for(int k = 0;; k++) {
    message = std::string("access at ") + std::to_string(k);
    array[k] = 0;
  return EXIT_SUCCESS;

The extern expression makes sure that C++ does not mangle the function name.

Running this program with memory sanitizers will print the following:

You caused an error: access at 8

You could also write to a file, if you would like.

Published by

Daniel Lemire

A computer science professor at the University of Quebec (TELUQ).

3 thoughts on “Catching sanitizer errors programmatically”

    1. If you are interested in the location of the error in your program, you could use backtrace functions, indeed.

      My example illustrates a simple case where we want a description of the state of the program when an error happens. In my example, we know where the error happens a priori.

  1. Please be patient. I’m trying to express an idea from the hip and want to have fun and be innovative. I spent a lot of time on this topic so please don’t avoid me. Be more Austin than Boston. Maybe try coding this:

    Each piece of information can be represented for computational purposes with a single glyph or character. Take numbers for example, if I said, “Let P = 2 and NP = 11. The only difference would be the number of steps (time) and memory required to display one NP at a time in two cases.” But there’s no reason infinite numbers can’t each have a single processing unique. However, computation would require a fairly machine-readable library only. But having each number have a 1 or 0 representation and then a single representation only on the input makes more sense. The only step missing was the ingenuity of the programmer in separating P and NP. Suppose for example P is prime and NP is never prime including non-prime integers: P = NP false. So P = NP is true. But what is a prime number? If I had two teachers and four apples I could give each teacher two apples. If I had two apples I could give each teacher a piece. It seems more logical to us to specify as long as there is an equal division into two or more parts to call the number COMPOSITE. This makes the input composite and the output primitive. So how can we provide proof that this is possible. Well, consider the output of prime 11 as NP as a proof that NP is prime. The proof is very tedious but we are still talking about a computer that can quickly confirm that the proof is correct. We can even say in imagination, use the number of primes that we can invert and invert on the right side to explain the figure equal to the difference between 2 and 11.
    The proof that the machine emits divides the number into left and right correct answers and identifies prime 10s and a solution to the subsum problem that returns only a subset that includes zero when the number is prime.

    The formula is prime time 2 + next prime, then prime + next prime as a product, minus the prime equals the prime that starts with 11… for example
    3×2=6, 6+5=11, 3+5=8*2=16, 16-5=11 left prime 2 right prime 11
    Finite operator -5 so
    5×2=10, 10+3=13, 5+3=8, 8*2=16-3=13 left prime 5 right prime 13
    Final operator -3 continues
    7×2=14, 14+3=17, 7+3=10, 10*2=20, 20-3=17 left 7 right 7 or 17
    operator -3
    reaching a peak…. +3, +3, +5 for -11 and +11… equal to zero is 6 inverted occurrences and the right side above the character then becomes a nine.

Leave a Reply

Your email address will not be published. The comment form expects plain text. If you need to format your text, you can use HTML elements such strong, blockquote, cite, code and em. For formatting code as HTML automatically, I recommend

You may subscribe to this blog by email.