Sourav Kannantha B
Sourav Kannantha B

Reputation: 3299

How to differentiate address sanitizer error from script error?

When using Address Sanitizer when either of ASAN or the actual program fails, the exitcode is always 1. How can I differentiate one from the other? I tried setting exitcode option in ASAN_OPTIONS to something other than 1, but still ASAN is exiting with 1. What can I do?

Here is the sample code:

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

int main(int argc, const char *argv[]) {
    char *s = malloc(5);
    strcpy(s, "Hello world!");
    printf("string is: %s\n", s);
    //free(s);
    return 0;
}

Here is the command used to compile: gcc -g -fsanitize=address -o /tmp/tmp.bin /tmp/tmp.c

Here is the command used to execute: ASAN_OPTIONS=exitcode=0 /tmp/tmp.bin ; echo $?

Here whatever exitcode I used (other than 0), I'm getting 1 as output of echo $?

Upvotes: 1

Views: 72

Answers (1)

m__mdy__m
m__mdy__m

Reputation: 52

To differentiate between an AddressSanitizer (ASan) error and a program exit, set ASAN_OPTIONS=halt_on_error=0 to allow the program to continue running after an ASan error. Then check the logs or output for ASan-specific error messages rather than relying solely on the exit code. Unfortunately, exitcode in ASAN_OPTIONS doesn't work as expected in all cases due to tool limitations. A more robust approach is to wrap your program in a script that captures ASan output for analysis.

Upvotes: 0

Related Questions