0

I am trying to build a simple code with obvious leaks using AddressSanitizer in CLion but it does not detect any problems with my code.

The main.cpp code file has the following code:

#include <cstdio>
#include <iostream>
#include <cstring>

#define STR_LENGTH 18

int main()
{

    printf("Test Project\n\n");

    char source[] = "This is a char array.";
    char dest[STR_LENGTH];

    strcpy(dest, source);

    printf("%s is the string after strcpy.\n",dest);


    char stack_mem[3] = {42,42,42};
    char stack_mem2[3] = {63,63,63};

    char x = stack_mem[4];
    std::cout << "X: " << static_cast<int>(x) << "\n";
    char y = stack_mem2[-1];
    std::cout << "X: " << static_cast<int>(y) << "\n";

    int* ptr = new int;

    return 0;
}

My CMakeLists.txt file contains the following code:

cmake_minimum_required(VERSION 3.19)
project(asan)

set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_VERBOSE_MAKEFILE true)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")

add_executable(asan main.cpp)

However, I do not see any problems when I run this. The output that I get is as follows:

/home/dev/source.code/test-branch/asan/cmake-build-debug/asan
Test Project


Process finished with exit code 1

I tried to build the same code from the terminal with g++ using the -fsanitize=address flag and it showed the correct problems but I cannot seem to make it work with CLion.

saadurr
  • 57
  • 10

1 Answers1

0

I have been playing around with the AddressSanitizer under CLion 2022.3.1 in combination with gcc 12.2.1 and clang 15.0.6 under Linux. I was fooled by the IDE GUI at first.

enter image description here

The non-zero return code is the first hint that everything is working as intended. The return code is shown in the "Console" tab. Where the usual console program text output to stdout appears. There is a second tab "Sanitizers" next to the console tab. In there

enter image description here

you get the output of the AddressSanitizer you are probably looking for.

uli
  • 121
  • 2