Which Numbers Would Crash this Function?

10

2

Which values of x and y will cause a crash with some C compilers?

int f(int x, int y) {
    return (y==0) ? 0 : (x/y);
}

ugoren

Posted 2012-01-12T13:01:14.827

Reputation: 16 527

Since C's ternary operator shortcuts, I would say none would. This question doesn't seem to fit the format for this site, which is focused on program puzzles and code golf. See the faq for details http://codegolf.stackexchange.com/faq.

– Steven Rumbalski – 2012-01-12T14:48:27.023

This isn't code golf, but is a puzzle. There is an answer, and it's just a couple of numbers. – ugoren – 2012-01-12T14:51:45.847

I stand corrected. – Steven Rumbalski – 2012-01-12T14:59:55.397

2Actually, judging by the K&R book, this function really must never crash. But by the ANSI C standard, the behavior in the particular crashing case is undefined, and with x86 compilers it crashes. – ugoren – 2012-01-12T15:05:37.900

Does undefined mean that it doesn't crash? Or does it mean that it's the implementations choice how to handle such things? Does the C standard preclude signed integers from being represented by ones' complement? – Steven Rumbalski – 2012-01-12T15:18:58.317

Undefined means it's implementation dependent, and may return any value, crash, or format your hard disk. The C standard says that if the result of division can't be stored in an integer (which is the case here), behavior is undefined. – ugoren – 2012-01-12T15:28:05.633

The FAQ is quite clear that puzzle on this site are expected to have an objective criteria for determining a winner. A "give me a list of examples" type problem does not qualify because all examples are equally valid. – dmckee --- ex-moderator kitten – 2012-01-12T17:26:20.783

1@dmckee, If you give the right answer, you're the winner. What cretirion could be more clear and objective? There's only one answer (or do you have another example?) – ugoren – 2012-01-12T18:10:09.217

Answers

7

-2147483648 (INT_MIN) and -1

#include <stdio.h>
#include <limits.h>
int f(int x, int y) {
    return (y==0) ? 0 : (x/y);
}
int main() {
    int r = f(INT_MIN, -1);
    printf("%d\n", r);
    return 0;
}

$ gcc -Wall division.c && ./a.out # => zsh: floating point exception ./a.out

eregon

Posted 2012-01-12T13:01:14.827

Reputation: 356

Indeed. Though this should give a warning, because 2147483648 isn't a valid integer. – ugoren – 2012-01-12T14:57:46.073

1Yes, that's why I used INT_MIN after, to use a valid int. I guess the reason is 2147483648 is not a valid int, since INT_MAX is 2^31-1 with 32-bit int. – eregon – 2012-01-12T14:59:40.027

Ah. Two's complement. I missed that. – Steven Rumbalski – 2012-01-12T15:00:59.763

Yes, it should compile cleanly with INT_MIN (which is -2147483648). – ugoren – 2012-01-12T15:02:33.193

3

The right answer is already given, but I immediately thought about Microsoft Pex.

Pex automatically generates test suites with high code coverage. Right from the Visual Studio code editor, Pex finds interesting input-output values of your methods, which you can save as a small test suite with high code coverage. Microsoft Pex is a Visual Studio add-in for testing .NET Framework applications

After adding your puzzle in the sandbox site, it finds the answer in a few seconds, the same as eregons answer. (click ask pex)

Note: it does it in C#, but the language is not really relevant.

  • x: int.MinValue
  • y: -1
  • Exception: OverflowException
  • Message: Arithmetic operation resulted in an overflow.

Ron Sijm

Posted 2012-01-12T13:01:14.827

Reputation: 131

1Nice. It surely doesn't brute-force it, because it wouldn't end in a few seconds. I guess someone in MS realized that numbers around 0 and MAX_INT are always interesting. – ugoren – 2012-01-17T15:19:15.963

Hopefully it's a little more clever than that. It might look at (x/y) and know that INT_MIN, -1, 0 etc. are all problem cases for that expression, and try to reverse engineer a way to produce those values at the time of evaluation. – Clueless – 2012-01-18T21:01:50.240