Author Topic: Apple's most famous source code  (Read 8778 times)

jj2007

  • Member
  • *****
  • Posts: 10260
  • Assembler is fun ;-)
    • MasmBasic
Apple's most famous source code
« on: February 24, 2014, 05:15:52 AM »
sslKeyExchange.c has all chances to become software of the year (more) :lol:

Line 631:
    if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
        goto fail;
        goto fail;

GOTO??? Apple should have abandoned BASIC a long time ago, or at least they should have switched to a modern dialect ;-)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7212
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Apple's most famous source code
« Reply #1 on: February 24, 2014, 06:30:04 AM »
 :P
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

anta40

  • Member
  • ***
  • Posts: 312
Re: Apple's most famous source code
« Reply #2 on: February 24, 2014, 12:12:36 PM »
I don't really get this anti-GOTO behaviour.
Linux kernel itself, one of the most active & complex open source projects in the world,
still has some GOTOs here and there.

I think this mailing list post give you a hint why GOTOs are still being used
https://lkml.org/lkml/2003/1/12/203

:P

K_F

  • Member
  • *****
  • Posts: 1568
  • Anybody out there?
Re: Apple's most famous source code
« Reply #3 on: February 24, 2014, 06:10:58 PM »
The developer must have a 'stutter'... 2x goto fail.

The billboard advert

Apple Software.. making sure you fail twice !!
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

sinsi

  • Guest
Re: Apple's most famous source code
« Reply #4 on: February 24, 2014, 08:29:39 PM »
For those of us who only know c as the third letter of the alphabet, an explanation

jj2007

  • Member
  • *****
  • Posts: 10260
  • Assembler is fun ;-)
    • MasmBasic
Re: Apple's most famous source code
« Reply #5 on: February 24, 2014, 09:31:21 PM »
For those of us who only know c as the third letter of the alphabet, an explanation

Thanks, indeed very nicely explained :t
Quote
Note the two goto fail lines in a row. The first one is correctly bound to the if statement but the second, despite the indentation, isn't conditional at all. The code will always jump

Assembly is safer ;)
.if eax
    jmp fail
    jmp fail
    jmp fail
.endif

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: Apple's most famous source code
« Reply #6 on: February 25, 2014, 03:45:39 AM »
Jochen,

GOTO??? Apple should have abandoned BASIC a long time ago, or at least they should have switched to a modern dialect ;-)

Donald Knuth wrote that text.

Gunther
Get your facts first, and then you can distort them.

ragdog

  • Member
  • ****
  • Posts: 610
Re: Apple's most famous source code
« Reply #7 on: February 25, 2014, 04:14:58 AM »
Hi

Quote
   if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
        goto fail;
        goto fail;

Why is this a bug?

Ok i know not what make this command "goto"
but i think is same


Quote
Assembly is safer ;)
.if eax
    jmp fail
    jmp fail
    jmp fail
.endif

Or not?


TWell

  • Member
  • ****
  • Posts: 748
Re: Apple's most famous source code
« Reply #8 on: February 25, 2014, 04:22:43 AM »
Hi

Quote
   if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
        goto fail;
        goto fail;

Why is this a bug?

Ok i know not what make this command "goto"
but i think is same
because it's like this:
.if eax
    jmp fail
.endif
    jmp fail

jj2007

  • Member
  • *****
  • Posts: 10260
  • Assembler is fun ;-)
    • MasmBasic
Re: Apple's most famous source code
« Reply #9 on: February 25, 2014, 10:29:38 PM »
Good & bad news: Apple’s massive ‘goto fail’ fixed in iOS, but not in OS X

Apple continues trying to compile the OS X code. In the meantime, iphone 5s users report that the iOS 7.0.6 security update leads to blue screens and endless rebooting... sounds like fun :P

Besides, more and more people lament that any modern C compiler would have issued an 'unreachable code' warning; and that therefore either Apple coders are dumb, or they aren't ;-)

sinsi

  • Guest
Re: Apple's most famous source code
« Reply #10 on: February 25, 2014, 11:08:44 PM »
Is there any reason for one statement like that to be split into two lines?
Code: [Select]
    if ((err = ReadyHash(&SSLHashSHA1, &hashCtx)) != 0)
        goto fail;
    if ((err = SSLHashSHA1.update(&hashCtx, &clientRandom)) != 0)
        goto fail;
    if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
        goto fail;
    if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
        goto fail;
        goto fail;
    if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
        goto fail;

    if ((err = ReadyHash(&SSLHashSHA1, &hashCtx)) != 0) goto fail;
    if ((err = SSLHashSHA1.update(&hashCtx, &clientRandom)) != 0) goto fail;
    if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0) goto fail;
    if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) goto fail;
        goto fail;
    if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0) goto fail;

jj2007

  • Member
  • *****
  • Posts: 10260
  • Assembler is fun ;-)
    • MasmBasic
Re: Apple's most famous source code
« Reply #11 on: February 26, 2014, 12:50:45 AM »
Is there any reason for one statement like that to be split into two lines?

Indeed. No need for !=0, too many brackets etc, no wonder they don't understand their own code...

#include <stdio.h>

int CheckFail(int err) {
  int i, whatever[]={0, 0, 1, 3, 0};
  for (i=0;i<5;i++)
   if (err == whatever[ i ]) goto fail;
  return 0;
fail:
  return i+1;
}

int main(int argc, char* argv[]) {
  printf("fail=%i", CheckFail(3));
  _getch();
}

TWell

  • Member
  • ****
  • Posts: 748
Re: Apple's most famous source code
« Reply #12 on: February 26, 2014, 01:10:23 AM »
almost same without goto
Code: [Select]
while (1) {
if ((err=func1()) != 0) break;
if ((err=func2()) != 0) break;
if ((err=func3()) != 0) break;
if ((err=func4()) != 0) break;
break;
}
final();
or this full example
Code: [Select]
int func1(void) {return 0;}
int func2(void) {return 0;}
int func3(void) {return 0;}
int func4(void) {return 0;}
void final(void) {}

int main(int argc, char **argv)
{
int err = 0;
do {
if ((err=func1()) != 0) break;
if ((err=func2()) != 0) break;
if ((err=func3()) != 0) break;
if ((err=func4()) != 0) break;
} while (0);
final();
return err;
}
As Apple developers can use source validators and etc..., so that was bit strange error ?

xanatose

  • Member
  • ***
  • Posts: 396
Re: Apple's most famous source code
« Reply #13 on: February 26, 2014, 11:07:44 PM »
How about:
Code: [Select]
static OSStatus SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams, uint8_t *signature, UInt16 signatureLen) {
  OSStatus err;
  ...
  for(;;) {
if (0 != (err = SSLHashSHA1.update(&hashCtx, &serverRandom))) {
          break;
        }
if (0 != (err = SSLHashSHA1.update(&hashCtx, &signedParams))) {
          break;
        }
err = SSLHashSHA1.final(&hashCtx, &hashOut);
        break;
  }
  SSLFreeBuffer(&signedHashes);
  SSLFreeBuffer(&hashCtx);
  return err;
}

This avoid the condition is always constant warning that while(1) has. To make the meaning clearer you can also create a macro called
Code: [Select]
#define forever() for(;;)

Assuming good faith (and not someone trying to bypass security) The goto fail error seem like someone deleted an if and forgot to delete the goto.

The same mistake could happen using while(1) or for. as break is nothing but a glorified and highly limited goto :)

However the use of braces would have prevented the un-noticing of the error.  (as the compile would have failed).  As the lonely break would have being a break; }

Mark44

  • Regular Member
  • *
  • Posts: 40
Re: Apple's most famous source code
« Reply #14 on: March 06, 2014, 05:56:28 PM »
I don't really get this anti-GOTO behaviour.
Linux kernel itself, one of the most active & complex open source projects in the world,
still has some GOTOs here and there.

I think this mailing list post give you a hint why GOTOs are still being used
https://lkml.org/lkml/2003/1/12/203

:P
Edsger Dykstra notwithstanding, I believe there is a place for gotos. I worked in the Windows division at Microsoft for five years, and I seem to recall seeing a few gotos in the Windows source code.