Re: SIGSEGV on fclose

From: Henning P. Schmiedehausen (hps@tanstaafl.de)
Date: Fri Jul 14 2000 - 10:06:07 EST


ak@suse.de (Andi Kleen) writes:

>The alternative is the error prone and ugly:

>if (a = alloca()) {
> if (b = allocb()) {
> if (c = allocc()) {
> if (d =allocd()) {
> do_real_work();
> freed(d);
> }
> freec(c);
> }
> freeb(b);
> }
> freea(a);
>}

>which is IMHO just asking for obscure bugs in error paths.

>/* a b c d are NULL */
>a = alloca();
>b = allocb();
>c = allocc();
>d = allocd();
>if (a && b && c && d)
> do_real_work();
>freea(a);
>freeb(b);
>freec(c);
>freed(d);

>Which one do you prefer ?

I would prefer the correct way:

atype *a = NULL;
btype *b = NULL;
ctype *c = NULL;
dtype *d = NULL;

if(!(a= alloca()))
 report_error_and_exit(a);
if(!(b= allocb()))
 report_error_and_exit(b);
if(!(c= allocc()))
 report_error_and_exit(c);
if(!(d= allocd()))
 report_error_and_exit(d);

do_real_work();

freea(a);
freeb(b);
freec(c);
freed(d);

void report_error_and_exit(error)
{
 whine("We had an error on", error);

 if(a)
  freea(a);
 if(b)
  freeb(b);
 if(c)
  freec(c);
 if(d)
  freed(d);

 exit();
}

Both of your examples are asking for trouble, hacker-style. If've seen too
much badly written, "clever" code crash in various ways to like any of your
examples. One of the main disadvantages of "free software". Sorry.

>About the only argument I see is to make one undefined behaviour in ISO
>C crash instead of behave sanely to point out a potential portability
>problem to some hosts (e.g. most Unixes seem to get this correct) early.
>I prefer a sane behaviour instead though.

Congrats, you just found out that there is more to life than Linux. :-)

        Regards
                Henning

-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen       -- Geschaeftsfuehrer
INTERMETA - Gesellschaft fuer Mehrwertdienste mbH     hps@intermeta.de

Am Schwabachgrund 22 Fon.: 09131 / 50654-0 info@intermeta.de D-91054 Buckenhof Fax.: 09131 / 50654-20

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sat Jul 15 2000 - 21:00:20 EST