Re: Goto statements in the Kernel

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

 



Hi,

Bob Smith a écrit :

> Why is that ? I googled up reasons like minimizing return points,
> but is that it ?

Most of the time, 'goto' is used to minimize code duplication in error
handling.

Without 'goto':

int myfunc (void)
{
   ret = dosomething1();
   if (ret < 0)
     return ret;

   ret = dosomething2();
   if (ret < 0)
   {
     undosomething1();
     return ret;
   }

   ret = dosomething3();
   if (ret < 0)
   {
     undosomething2();
     undosomething1();
     return ret;
   }

   ret = dosomething4();
   if (ret < 0)
   {
     undosomething3();
     undosomething2();
     undosomething1();
     return ret;
   }
  return 0;
}

With 'goto':

int myfunc(void)
{
   ret = dosomething1()
   if (ret < 0)
     goto ret_dosomething1;

   ret = dosomething2()
   if (ret < 0)
     goto ret_dosomething2;

   ret = dosomething3()
   if (ret < 0)
     goto ret_dosomething3;

   ret = dosomething4()
   if (ret < 0)
     goto ret_dosomething4;

ret_dosomething4:
  undosomething3();
ret_dosomething3:
  undosomething2();
ret_dosomething2:
  undosomething1();
ret_dosomething1:
  return ret;
}

Sincerly,

Thomas
-- 
PETAZZONI Thomas - thomas.petazzoni@xxxxxxxx
http://thomas.enix.org - Jabber: thomas.petazzoni@xxxxxxxxx
KOS: http://kos.enix.org/ - SOS: http://sos.enix.org
Fingerprint : 0BE1 4CF3 CEA4 AC9D CC6E  1624 F653 CB30 98D3 F7A7

Attachment: signature.asc
Description: OpenPGP digital signature


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux