1

I have been debugging a crash in a program which aborts with the error stack smashing detected. I have narrowed down the crash to just one function which is:

static ssize_t a (const char *x, const char *y, size_t z) {
#ifdef SOME_FLAG
    return b(x, y, z);
#endif
}

Since I am not defining SOME_FLAG anywhere, the function essentially does nothing and exits without a return statement.

I observed that if I just add a return 0 outside the #ifdef #endif block, the program doesn't abort (due to stack smash). The abort is also not seen if I define the function as static void instead of static ssize_t

Can the lack of a return statement cause stack-protector to trigger abort?

sg1993
  • 335
  • 2
  • 19
  • 7
    Sure. It is undefined behavior to not return when the function is supposed to. – NathanOliver Feb 26 '19 at 17:32
  • Well I did get the warning that I am supposed to return but what I am unsure about is how is this resulting in a stack overflow/corruption.. – sg1993 Feb 26 '19 at 17:33
  • 1
    Undefined Behaviour (UB) allows the compiler to do _anything_. – Roger Lipscombe Feb 26 '19 at 17:35
  • is like to ask "what the undefined behavior does", forget that, just return 0 – bruno Feb 26 '19 at 17:35
  • 1
    @NathanOliver: In C, falling through a function that is declared as returning a value is defined as equivalent to returning an arbitrary value if the caller never observes the value that was (not) returned. This could sometimes be useful for functions that accept a "mode" parameter to select among various actions, only some of which would have anything meaningful to return. If a caller that invokes `someFunc(2)` is never supposed to look at the return value, any effort `someFunc` might spend loading a return value would be wasted. – supercat Feb 26 '19 at 17:37
  • @supercat C++ is *not* C. – Jesper Juhl Feb 26 '19 at 17:40
  • @NathanOliver It's not ub if the returned (well, not returned) value is never used. – Swordfish Feb 26 '19 at 17:44
  • @JesperJuhl: The question is tagged both C and C++. – supercat Feb 26 '19 at 17:45
  • @Swordfish For C++ it is: https://timsong-cpp.github.io/cppwp/stmt.return#2.sentence-8 – NathanOliver Feb 26 '19 at 17:47
  • 1
    @sg1993 Please pick a single language. C and C++ are different languages. – NathanOliver Feb 26 '19 at 17:49
  • 1
    @NathanOliver Didn't see the C++-tag at first. For C it isn't ub when the value isn't used. – Swordfish Feb 26 '19 at 17:49
  • @NathanOliver: Given that many people who work with C++ also have to work occasionally with C, and vice versa, it's useful for people working in each language to know ways in which the other one differs. – supercat Feb 26 '19 at 17:50
  • Removed the C tag since my code is in C++. Also, to clarify, I was not using the return value anywhere.. – sg1993 Feb 26 '19 at 17:51
  • 2
    *Also, to clarify, I was not using the return value anywhere.* – For C++ that doesn't matter. See NathanOliver's link. – Swordfish Feb 26 '19 at 17:53

1 Answers1

4

Regarding C++:

If a function (other than main) declared to return a non-void value exists without return or throw, then the behaviour of the program is undefined.

Can the lack of a return statement cause stack-protector to trigger abort?

Yes. The behaviour is undefined. Anything can happen.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • In a worst-case scenario it may even appear to work (sometimes). – Jesper Juhl Feb 26 '19 at 17:38
  • @JesperJuhl That's a bad scenario. Worst-case scenario (that has actually happened to my knowledge) for UB: https://en.wikipedia.org/wiki/Therac-25 (Pedantic note: I'm not sure if it would be technically correct to call a race condition UB in context of assembly) – eerorika Feb 26 '19 at 17:41