Re: Compiler didn't error on function with no return statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 20/06/13 20:06, Andy Falanga (afalanga) wrote:
Hi everyone,

I need some help in understanding why my GCC didn't consider this an issue.  I have a function that was constructing a path to a daemon program based on the location of the shared object file where this code is.  Something similar to this:

namespace {
     std::string ConstructPath()
     {
         int lastSlash(0);
         std::string pathVar;
         Dl_info dl_info;
         memset(&dl_info, 0, sizeof(dl_info));

         if((dladdr((void*)ConstructPath, &dl_info)) == 0)
         {
             throw std::runtime_error("Failed to get address ");
         }

         pathVar = dl_info.dli_fname;
         lastSlash = pathVar.find_last_of('/');
         if(std::string::npos == lastSlash)
         {
             // no slashes given ... must be that *.so
             // is in the current directory
             pathVar = "mydaemond";
         }
         else
         {
             pathVar.erase(pathVar.begin() + (lastSlash + 1), pathVar.end());
             pathVar.append("mydaemond");
         }

         // first check if we can find the daemon
         {
             // introducing sub-scope to ensure the file object is closed
             std::ifstream test(pathVar.c_str());
             if(!test.good())
             {
                 throw std::runtime_error("cannot find mydaemond");
             }
         }

         // *** the below statement wasn't there originally, the
         // *** function simply exited after the forced-scope block above,
         // *** however, the function *did* have the return type
         return pathVar;
     }
}

My comments above the final return statement illustrate what my question is about.  Why wasn't this a problem?  There was no return statement and yet, the code compiled fine.  I'm using GCC 4.4.4 on CentOS 6.2.  Is this just a problem with the 4.4.4 compiler that was fixed?  I'm betting there's some subtlety in C++ here that I'm not yet aware of and I'd like to be schooled.

Thanks,
Andy


<http://stackoverflow.com/questions/5655181/how-to-turn-on-gcc-warnings-for-a-forgotten-return-statement>

Failing to provide a return value is legal C, but undefined behaviour. So gcc has to accept it.

You only get warnings if you enable them. Normally, you will want your code to be warning-free when compiled with "-Wall". Many people use "-Wextra" as well - I personally enable many of the other warning flags as well.






[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux