Mysterious return line and memory card.

When working with memory allocation, valgrind and gdb, I had to write a simple c program with an invalid free:

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

int main (void)
{
    int* arr = (void*) malloc(100 * sizeof(int));
    arr[50] = 10;
    free(arr + (20 * sizeof(int)));
    printf("arr[50] = %d\n", arr[50]);
    return 0;
}

What causes the error:

*** Error in `./allocWithFunnyFree': free(): invalid pointer: ... ***
======= Backtrace: =========
...
======= Memory map: ========
...

Then I tried to get rid of the output by redirecting to sdtoutand stderrto /dev/null, but noticed that the output would still be printed, which left me puzzled.

I stopped the program before calling free()with the debugger, looked at the directory /proc/PID/fdand tried again to redirect all the listed fd to /dev/null, but still the same result.

I searched the Internet for answers and asked some of my colleagues, but no one could explain to me how this conclusion is printed and why it cannot be redirected.

, , /Linux, , , .
!

+4
1

( glibc) /dev/tty, ( 3 ), 1 2, .

Glibc LIBC_FATAL_STDERR_, . , backtraces stderr :

$ export LIBC_FATAL_STDERR_=2
$ ./allocWithFunnyFre 2>free_backtrace

strace, ( ).

+4

Source: https://habr.com/ru/post/1690519/


All Articles