Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and how does GCC compile a function with a missing return statement?

Tags:

c

linux

gcc

#include <stdio.h>

char toUpper(char);

int main(void)
{
    char ch, ch2;
    printf("lowercase input : ");
    ch = getchar();
    ch2 = toUpper(ch);
    printf("%c ==> %c\n", ch, ch2);

    return 0;
}

char toUpper(char c)
{
    if(c>='a'&&c<='z')
        c = c - 32;
}

In toUpper function, return type is char, but there is no "return" in toUpper(). And compile the source code with gcc (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4), fedora-14.

Of course, warning is issued: "warning: control reaches end of non-void function", but, working well.

What has happened in that code during compile with gcc? I want to get a solid answer in this case. Thanks :)

like image 325
harrison Avatar asked Sep 02 '11 08:09

harrison


People also ask

Why does it say missing return statement?

The “missing return statement” message occurs when a method does not have a return statement. Each method that returns a value (a non-void type) must have a statement that literally returns that value so it can be called outside the method.

How do I fix missing a return statement?

In order to solve the missing return statement error, we simply need to add the return statement to the method just like to the one we did in case one. So, we will return the some value of the same type which we used before the name like as: public static String checkNumber( int number) { //initialize variable i.

Is Missing return statement syntax error?

The missing return statement error is a compile-time error. Therefore this type of error tends to be easy to detect. The main causes are: a return statement was simply omitted by mistake. the method doesn't return any value, but type void is not declared in the method signature.

What if return is missing in Python?

Without the final return, the flow of control reaches the end of the function without hitting a return statement, so the default value of None is returned.


2 Answers

You should keep in mind that such code may crash depending on compiler. For example, clang generates ud2 instruction at the end of such function and your app will crash at run-time.

like image 143
ARA1307 Avatar answered Sep 18 '22 17:09

ARA1307


What happened for you is that when the C program was compiled into assembly language, your toUpper function ended up like this, perhaps:

_toUpper:
LFB4:
        pushq   %rbp
LCFI3:
        movq    %rsp, %rbp
LCFI4:
        movb    %dil, -4(%rbp)
        cmpb    $96, -4(%rbp)
        jle     L8
        cmpb    $122, -4(%rbp)
        jg      L8
        movzbl  -4(%rbp), %eax
        subl    $32, %eax
        movb    %al, -4(%rbp)
L8:
        leave
        ret

The subtraction of 32 was carried out in the %eax register. And in the x86 calling convention, that is the register in which the return value is expected to be! So... you got lucky.

But please pay attention to the warnings. They are there for a reason!

like image 28
Ray Toal Avatar answered Sep 17 '22 17:09

Ray Toal