Print Story some C code
Diary
By i (Thu Dec 21, 2006 at 12:09:23 PM EST) (all tags)
found this little snippet somewhere. audit it. is it safe? is there some input that will result in crash, er, undefined behaviour? i couldn't find any. can you?



#include <stdlib.h>

int main(int argc, char **argv)
{
    int a, b;
    if (argc != 3)
        return 1;
    a = strtol(argv[1], (char**)NULL, 10);
    b = strtol(argv[2], (char**)NULL, 10);
    return b ? a / b : 0;
}

oh, and i've got a job. starting Jan 02.

Update [2006-12-21 19:38:15 by i]:atoi is bad, using strtol instead. the question remains: is this code safe?

< "Tower Records in Tokyo" | BBC White season: 'Rivers of Blood' >
some C code | 15 comments (15 topical, 0 hidden) | Trackback
Congratulations on the new job! by TPD (2.00 / 0) #1 Thu Dec 21, 2006 at 01:57:13 PM EST


why sit, when you can sit and swivel with The Ab-SwivellerTM
thx by i (2.00 / 0) #2 Thu Dec 21, 2006 at 02:05:11 PM EST



[ Parent ]
Mazel Tov... by motty (2.00 / 0) #3 Thu Dec 21, 2006 at 02:20:58 PM EST
ITYM Jan 2. Either that or you're about to get slightly less than five years back pay. Which would be nice but strikes me as unlikely.

I amd itn ecaptiaghle of drinking sthis d dar - Dr T
thx by i (4.00 / 1) #5 Thu Dec 21, 2006 at 02:26:38 PM EST
I mean the second day of the upcoming January, yes ;)


[ Parent ]
Overflowing atoi by miker2 (2.00 / 0) #4 Thu Dec 21, 2006 at 02:25:05 PM EST
atoi, when overflowed, results in undefined behaviour, so feeding it ${HOST_ARCHITECTURE_MAX_INT_VALUE} + 1 for either a or b should result in undefined behaviour. 

What's the deal with argv[0]?? Useless arg?

Good work on the new jobby job.


Ah, sociopathy. How warm, how comforting, thy sweet embrace. - MNS
thx by i (4.00 / 1) #6 Thu Dec 21, 2006 at 02:34:04 PM EST
oh and i didn't realise that atoi can result in UB, so make that strtol() instead.

argv[0] is the program name IIRC.


[ Parent ]
You are correct. by miker2 (2.00 / 0) #7 Thu Dec 21, 2006 at 02:48:43 PM EST
my C programming skillz aren't so m4d anymore.

Ah, sociopathy. How warm, how comforting, thy sweet embrace. - MNS
[ Parent ]
Yes by ShadowNode (2.00 / 0) #8 Thu Dec 21, 2006 at 03:07:43 PM EST
Specifically the called name, which allows you to differentiate based on symlinks.

[ Parent ]
oh, and by ShadowNode (2.00 / 0) #10 Thu Dec 21, 2006 at 03:08:28 PM EST
You can overwrite it, for process list shenanigans.

[ Parent ]
Overflow by ni (2.00 / 0) #9 Thu Dec 21, 2006 at 03:07:54 PM EST
if a > INT_MAX but < LONG_MAX (which depends on the platform) a and b can overflow.


256: What are you searching for? mx: Kaola penis. 256: Why aren't you using image search?
In English by ObviousTroll (2.00 / 0) #11 Thu Dec 21, 2006 at 04:05:16 PM EST
ni is saying that you declared a and b as type int, but strtol returns a long. Depending on the processor, this is a Bad Thing.


--
It was an American tradition, like fathers chasing kids around with power tools.
[ Parent ]
No it is not a Bad Thing by komet (2.00 / 0) #13 Fri Dec 22, 2006 at 12:17:14 AM EST
If int and long are different sizes, the return value will be truncated anyway (regardless of input) which will result in overflow but not undefined behaviour.

I suggest the code is safe because no buffers are written to, and no strange pointer arithmetic is going on.

--
<ni> komet: You are functionally illiterate as regards trashy erotica.

[ Parent ]
Pfft. by ObviousTroll (2.00 / 0) #14 Fri Dec 22, 2006 at 02:07:06 AM EST
Deliberately accepting code that generates warnings is a Bad Thing.


--
It was an American tradition, like fathers chasing kids around with power tools.
[ Parent ]
NO YUO! by komet (2.00 / 0) #15 Fri Dec 22, 2006 at 02:27:23 AM EST
Only girls and clowns care about C compiler warnings. Proper men couldn't care less as long as the generated assembler code is correct.

--
<ni> komet: You are functionally illiterate as regards trashy erotica.
[ Parent ]
obC by TurboThy (4.00 / 1) #12 Thu Dec 21, 2006 at 08:28:42 PM EST
All C code is inherently unsafe.

Congrats on getting a job. Unemployment is boring in the long run.
__
Sommerhus til salg, første række til Kattegat.

some C code | 15 comments (15 topical, 0 hidden) | Trackback