sewardj | 16031ba | 2005-07-06 19:01:53 +0000 | [diff] [blame] | 1 | |
| 2 | #include <stdio.h> |
| 3 | #include <malloc.h> |
| 4 | |
| 5 | // Do a test comparison. By default memcheck does not use the |
| 6 | // expensive EQ/NE scheme as it would be too expensive. The |
| 7 | // assignment to *hack is a trick to fool memcheck's bogus-literal |
| 8 | // spotter into thinking this is a bb which needs unusually careful |
| 9 | // attention, and therefore the expensive EQ/NE scheme is used. |
| 10 | |
| 11 | __attribute__((noinline)) // keep your grubby hands off this fn |
| 12 | void foo ( int* p1, int* p2, unsigned int * hack ) |
| 13 | { |
| 14 | *hack = 0x80808080; |
| 15 | if (*p1 == *p2) |
| 16 | printf("foo\n"); |
| 17 | else |
| 18 | printf("bar\n"); |
| 19 | } |
| 20 | |
| 21 | |
| 22 | int main ( void ) |
| 23 | { |
| 24 | |
| 25 | unsigned int hack; |
| 26 | |
| 27 | int* junk1 = malloc(sizeof(int)); |
| 28 | int* junk2 = malloc(sizeof(int)); |
| 29 | |
| 30 | short* ps1 = (short*)junk1; |
| 31 | short* ps2 = (short*)junk2; |
| 32 | |
| 33 | int* pi1 = (int*)junk1; |
| 34 | int* pi2 = (int*)junk2; |
| 35 | |
| 36 | // both words completely undefined. This should give an error. |
| 37 | foo(pi1,pi2, &hack); |
| 38 | |
| 39 | // set half of the words, but to different values; so this should |
| 40 | // not give an error, since inspection of the defined parts |
| 41 | // shows the two values are not equal, and so the definedness of |
| 42 | // the conclusion is unaffected by the undefined halves. |
| 43 | *ps1 = 41; |
| 44 | *ps2 = 42; |
| 45 | foo(pi1,pi2, &hack); |
| 46 | |
| 47 | // set half of the words, but to the same value, so this forces the |
| 48 | // result of the comparison to depend on the undefined halves. |
| 49 | // should give an error |
| 50 | *ps1 = 42; |
| 51 | *ps2 = 42; |
| 52 | foo(pi1,pi2, &hack); |
| 53 | |
| 54 | return 0; |
| 55 | } |