Segmentation error only when I redirect stdout to / dev / null?

I have a C ++ unit test that produces useful output for stderr and basically noise (unless I am debugging) on ​​stdout, so I would like to redirect stdout to / dev / null.

Curiously, this leads to a segmentation error.

Is there a reason why the code may happen with the error "> / dev / null" and doesn't it work otherwise?

The output is completely printf s if it has any bearing.

It’s hard for me to publish the broken code because this is a study submitted for publication. I hope that there is an “obvious” possible reason based on this description.

post mortem

Segaft was called with the following code:

 ArrayElt* array = AllocateArrayOfSize(array_size); int index = GetIndex(..) % array_size; ArrayElt elt = array[index]; 

The third time I forgot that x % y remains negative when x negative in C / C ++.

Ok, so why did this happen when I was redirected to /dev/null ? I assume that the invalid memory address I was accessing was in the output buffer for stdout - and this buffer is not allocated when it is not needed.

Thanks for the good answers!

+4
source share
3 answers

There is no “normal” reason for I / O on stdout to trigger a kernel dump when standard output is redirected to / dev / null.

You most likely have a wandering pointer or a buffer overflow that triggers a kernel dump when sent to / dev / null, rather than sent to standard output, but it will be difficult to identify the problem without code.

It is generally recommended that you post useful information about the standard output and noise with standard error.

+3
source

This does not exactly answer your question, but it can. Have you tried using gdb ? This is a command line debugging tool that can find where segfaults occur. It is quite easy to use. Here is a pretty detailed tutorial on how to use it.

+6
source

Maybe something checks for "isatty", which may cause different behavior for /dev/null .

Perhaps there is something reading from stdout that would not work for /dev/null .

+1
source

All Articles