🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #3525

Re: Should C/C++ compilers be smart enough to catch this???

#3525
From: Joe Durusau
Date: Thu, 03 Apr 2003 20:44
80 lines
3522 bytes
Fred Ma wrote:
> 
> Hello,
> 
> I just spent a great number of hours chasing down a
> bug that showed itself on g++ on cygwin, but not on
> g++ on solaris2.5.8.  What did it turn out to be?
> 
> I had a class member function that returned an
> object, according to the prototype.  Also according
> to the function definition.  But, the function body
> didn't actually contain a return statement.  Like
> SomeFunc() below:
> 
>     SomeClass{
>         //  ...
>         ReturnType SomeFunc( SomeArgs);
>         Void AnotherFunc(void);
>         //  ...
>     }
> 
>     ReturnType SomeClass::SomeFunc(SomeArgs){
>         //    Do stuff,
>         //    No return statement
>     }
> 
>     void SomeClass::AnotherFunc(void){
>         //    Do stuff...
> 
>         //    Call SomeFunc():
>         SomeFunc(SomeArgs);
>     }
> 
> This caused corruption that resulted in memory
> access violations, even though SomeFunc's
> returned object isn't used at the point where it
> is invoked.  The memory violations didn't happen
> at the point where SomeFunc() was invoked,
> either, but in a later step in the code.
> 
> The frustrating thing is that the compiler didn't
> complain at all about the discrepancy between
> the lack of a return statement, even though the
> function needed one.  I personally find this kind
> of thing quite common when I reorganize my
> code e.g. moving functionality in and out of class
> member functions; that is, the finger details like
> return types and argument lists change so that
> it's easy to miss.
> 
> Shouldn't compilers be smart enough to
> not create code that corrupts when it is pretty
> straighforward to realize that (1) there is an
> obvious discrepancy, for which a warning can
> easily be issued, or (2) realize that the return
> type really isnt' being used and compile it in a
> way that doesn't create memory corruption?
> 
> Fred
> 
> P.S.  Since this happens only on cygwin, it
> might specific to any customizations to g++
> for cygwin.  All in all, cygwin's gcc is a much
> better indication of whether my code is buggy,
> because the solaris executables generally
> seem to run fine even when there are cringe-
> inducing bugs, which I only find because they
> crash on cygwin.
> 
> --
> Fred Ma, fma@doe.carleton.ca
> Carleton University, Dept. of Electronics
> 1125 Colonel By Drive, Ottawa, Ontario
> Canada, K1S 5B6

Did you run the compiler with -Wall?

Speaking only for myself,

Joe Durusau
Technical Details
Message-ID: <3E8C9D24.C6F85289@lmco.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!swen.emba.uvm.edu!att541!att542!ip.att.net!newsfeed3.global.lmco.com!svlnews.lmms.lmco.com!not-for-mail
References: <3E8C8C4F.ABC60B0F@doe.carleton.ca>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,2ca0bb2ef0ae6ada
X-Google-Attributes: gid11318c,public
X-Google-Thread: fc89c,2ca0bb2ef0ae6ada
X-Google-Attributes: gidfc89c,public
X-Google-ArrivalTime: 2003-04-03 13:32:33 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!swen.emba.uvm.edu!att541!att542!ip.att.net!newsfeed3.global.lmco.com!svlnews.lmms.lmco.com!not-for-mail
From: Joe Durusau <joseph.a.durusau@lmco.com>
Newsgroups: comp.unix.programmer,gnu.gcc.help
Subject: Re: Should C/C++ compilers be smart enough to catch this???
Date: Thu, 03 Apr 2003 15:44:20 -0500
Organization: Lockheed Martin Corporation
Lines: 80
Message-ID: <3E8C9D24.C6F85289@lmco.com>
References: <3E8C8C4F.ABC60B0F@doe.carleton.ca>
NNTP-Posting-Host: 166.27.88.87
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 4.61 [en] (WinNT; U)
X-Accept-Language: en