I ran this C code in CLion and Cygwin toolchain (gcc and gdb) in windows, I tried writing to str[5] which is not allocated:
#include <stdio.h>
#include "stdlib.h"
int main() {
char * str = malloc(4);
str[5] = 'a';
printf("%c\n", str[5]);
return 0;
}
However, it ran without any errors (I was excpecting to get memory access violation and a warning that I didn't free the allocated pointer).
This is how the CMakeLists.txt is configured (I just added the CMAKE_C_FLAGS line):
cmake_minimum_required(VERSION 3.17)
project(myproject C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror")
add_executable(myproject main.c)
Why didn't it throw warning / error for that memory access? How can I fix that?