3

In Java there will be a stacktrace that says StackOverflowError and the whole system won't crash, only the program.

In C I'm aware that an array index out of bounds will produce a segmentation fault. Is it the same for a stack overflow in C and there will also be a segmentation fault i.e. same error type for a similar problem?

I'm not testing a conscious infinite resursion in C to see what happens because I don't know the consequences.

Or is it sometimes something much worse and a stack overflow in C could cause an operating system failure and force you to power cycle to get back? Or even worse, cause irreversible hardware damage? How bad effects can a stack overflow mistake have?

It seems clear that the protection is better in Java than in C. Is it any better in C than in assembly / machine code or is it practically the same (lack of) protection in C as a assembly?

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 1
    What makes you think there's no protection? Have you ever experienced a segmentation fault? What have you seen happening? – Kayaman Jan 13 '17 at 12:30
  • 3
    No, nothing bad should happen to the OS. Modern processors and OSes make it impossible for a program to affect the OS or other programs accidentally. – alain Jan 13 '17 at 12:31
  • 1
    On a typical modern operating system (Linux, Windows, other Unix likes etc.) a C program (or any other program in any language) can normally not result in an operating system failure. The fautling process will just crash and that's all. – Jabberwocky Jan 13 '17 at 12:33
  • 4
    _In C I'm aware that an array index out of bounds will produce a segmentation fault_: That's not true, an index out of bounds in C results in __undefined behaviour__ (google that term). Though it __may__ result in a segfault. – Jabberwocky Jan 13 '17 at 12:37
  • C does not know about a stack, consequently it does not know about what would happen when overflowing the stack. – alk Jan 13 '17 at 13:19
  • A stack overflow is detected by the operating system, not the C runtime. However `gcc` can offer some detection and protection - but remember that you pay in performance (e.g. Java). See http://stackoverflow.com/questions/1629685/when-and-how-to-use-gccs-stack-protection-feature – cdarke Jan 13 '17 at 13:58

4 Answers4

6

In C I'm aware that an array index out of bounds will produce a segmentation fault. Is it the same for a stack overflow in C and there will also be a segmentation fault i.e. same error type for a similar problem?

There's no guarantee in C that there will be a segmentation fault. The C standard says it's undefined behaviour and leave it at that. How that might manifest, it at all, is up to the implementation/platform.

Or is it sometimes something much worse and a stack overflow in C could cause an operating system failure and force you to power cycle to get back? Or even worse, cause irreversible hardware damage? How bad effects can a stack overflow mistake have?

It's pretty rare on modern Operating systems that anything untoward would happen to the system; typically, only the program would crash. Modern operating systems use various memory protection techniques.

It seems clear that the protection is better in Java than in C. Is it any better in C than in assembly / machine code or is it practically the same (lack of) protection in C as a assembly?

That's because in Java, memory is "managed". In C, it's left to the programmer; it's by design. A C compiler does generate machine code in the end; so it can't be any better or worse. Obviously, a good compiler could detect some of these problems and warn you, which is an advantage in C compared to assembly.

P.P
  • 117,907
  • 20
  • 175
  • 238
4

Well the handling of memory failure, as any system resource failure, is basically handled by the OS, not the language itself.

Excluding some specific actions of prevention, as the stack checking, this kind of problems normally triggers an OS exception that can be handled by the language runtime.

The stack checking if enabled, normally specifying some switches on the compiler command line, instructs the compiler to insert check probes code for each stack consuming operation to verify the memory availability.

By default when for any reason, overuse of the stack or corruption, the execution try to access memory outside the bounds of allocated stack space the OS triggers a structured exception. Java as many C runtime normally handle those exception and also supply some way to pass them to the user code for eventual recovery (i.e. through signal or SEH). If no handler has been associated from the user code the control is passed to the runtime that by default will manage a controlled task shutdown (gracious shutdown).

If no handling is available, not even from runtime, the OS will shutdown the task and operate an abruptly resource relief (i.e. truncate files, close ports etc).

In any case the OS wil protect the system, unless the OS if a flaw one...

In C it is normal to register an handler that protect the code fragment that can fail. The way you can handle the exception depends on your OS (i.e. under windows you can wrap the code that can fail in an exception handler __try __except).

Frankie_C
  • 4,764
  • 1
  • 13
  • 30
3

This is not a C problem, at least what happens is not specified by C. C would only says that it is undefined behavior. So effect is matter of the runtime. On any reasonable OS this will produce some kind of error that will be catched and in *nixes will produce a segmentation fault to your process. Even exotic small OSes will protect itself from your faulty process. Whatever, this will never crash the OS. Java is not better than C, they are different languages and have different runtime. Java is, by design, more secure in the sense that it will protect you against many memory problems (among others). C gives you finer control over the machine, and yes it is more or less a kind of assembly language.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
3

Each executing thread have their stack allocated during thread creation at runtime. If a stack overflow is detected during program execution (native program compiled), only your program (process) will be affected, not the OS.

Jeandey Boris
  • 743
  • 4
  • 9
  • there is a big IF on the start of your 2nd sentence – Kami Kaze Jan 13 '17 at 14:02
  • @KamiKaze — the o/s memory management ensures that the overflow is detected. The program typically tries to access an unmapped address and the mapping code refuses to extend the stack any more. – Jonathan Leffler Jan 13 '17 at 14:06
  • 1
    I totally agree. I put a "If" because it's to the o/s memory management responsability to detect and manage such issue. Some handmade o/s, for specific reasons, may not apply such control. – Jeandey Boris Jan 13 '17 at 14:20
  • @JeandeyBoris Even some popular corporate-made o/s (and h/w) *did not* apply such control. Real-mode x86, embedded systems, etc. – user3125367 Jan 13 '17 at 16:07