1

I wrote the below SegmentationFault.cpp:

#include <csignal>

int main() {
    raise(SIGSEGV);
    return 0;
}

This causes a Segmentation Fault as expected. If I run it normally (without sudo):

$./SegmentationFault
Segmentation fault (core dumped)
$

But if I run it with sudo, it just exit sliently without any error:

$sudo ./SegmentationFault
$

Why is this?

Edit: Thanks to @Someprogrammerdude, I've update SegmentationFault.cpp to generate the error more appropriately.

hhy
  • 164
  • 11
  • 8
    Dereferencing a null pointer leads [*undefined behavior*](http://en.cppreference.com/w/cpp/language/ub). Since the behavior is it's undefined it can't be predicted. Sometimes it might actually *seem* to work. For all you know, running that program as the root user might format your hard drive. – Some programmer dude Aug 07 '17 at 10:25
  • 2
    Writing to `*p` is undefined behavior according to the language specification, so from a language lawyer perspective anything could happen, including the behavior you are observing :) From a practical perspective, it is possible to figure out why it happens like it happens, but it is not guaranteed to be reproducible with, say, another compiler. – Magnus Hoff Aug 07 '17 at 10:25
  • 1
    I cannot reproduce the behavior you're experiencing, but I'd recommend running `sudo strace ./SegmentationFault` to see what is actually happening. – Oleg Andriyanov Aug 07 '17 at 10:32
  • What platform? What compiler? (My bet is that `sudo` eats the error.) – David Schwartz Aug 07 '17 at 10:35
  • @Someprogrammerdude, that sounds pretty scary... How can I explicitly produce a seg fault safely with sudo? – hhy Aug 07 '17 at 11:19
  • @DavidSchwartz, It's Ubuntu 14, compiled with GCC 4.8. – hhy Aug 07 '17 at 11:20
  • 2
    What is your goal? To be able to consistently generate a segmentation fault? To "crash" the program any way possible? To generate a core-dump? What is the *actual* problem you're trying to solve? – Some programmer dude Aug 07 '17 at 11:22
  • And don't worry to much, dereferencing a null pointer as the root user will most likely not do anything bad. But doing bad things as the root user is not something you really should do. – Some programmer dude Aug 07 '17 at 11:23
  • @Someprogrammerdude, just to create a segmentation fault so that I can see why the error message is eaten with `sudo`. And to see if somehow I can print that error message even with `sudo`. – hhy Aug 07 '17 at 11:29
  • 1
    [See this old answer](https://stackoverflow.com/a/18986514/440558) on how to generate a segfault. Perhaps this question should even be marked as a duplicate of that question? – Some programmer dude Aug 07 '17 at 11:41
  • 1
    @Someprogrammerdude no. The answer you linked does not explain the behavior of `sudo` in question. – Oleg Andriyanov Aug 07 '17 at 11:49
  • Could it be that the error message is generated by the shell? Or is this some error handler that can be configured? Is this even a programming question, now that people pointed out how to raise the segmentation fault? – PaulR Aug 07 '17 at 12:51

0 Answers0