Oops, I did it again - Funny Programming Fails
Transcript of Oops, I did it again - Funny Programming Fails
![Page 1: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/1.jpg)
Oops, I did it again - Funny Programming Fails
Lukas Iffländer Tim HegemannDecember 14, 2016
1
![Page 2: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/2.jpg)
Funny Programming Fails
2
![Page 3: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/3.jpg)
Outline
How to accidentally cheat PABS
Toyota - Nothing is Impossible, even Code that Kills
A Story of Knights and Farmers
Funny Takeouts
Broken by Optimization
Santas Sled
3
![Page 4: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/4.jpg)
How to accidentally cheat PABS
![Page 5: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/5.jpg)
The Assignment
Implement the MergeSort algorithm for arrays of int in Java
4
![Page 6: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/6.jpg)
PABS Tests
@Test
public void testMergeSortSorted() {
int[] testArray = {1, 2, 3, 4, 5, 6};
MergeSort.sort(testArray);
assertArrayEquals("Array not sorted",
new int[] {1, 2, 3, 4, 5, 6}, testArray);
}
@Test
public void testMergeSortSortedDesc() {
int[] testArray = {6, 5, 4, 3, 2, 1};
MergeSort.sort(testArray);
assertArrayEquals("Array not sorted",
new int[] {1, 2, 3, 4, 5, 6}, testArray);
}
5
![Page 7: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/7.jpg)
More PABS Tests
// testMergeSort:
int[] testArray = {1, 3, 7, 5, 2, 9};
// [...]
// testMergeSort2:
int[] testArray = {16, 22, 38, 27, 85, 38, 60};
// [...]
// testMergeSort3:
int[] testArray = {7, 75, 24, 20, 12, 54, 19,
42, 73, 81};
// [...]
// testMergeSort4:
int[] testArray = {8, 12, 69, 31, 49, 49, 40, 3, 53,
13, 84, 36, 86, 72, 89, 94, 70};
6
![Page 8: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/8.jpg)
Seems legit ...
The following code passes ALL six tests:
7
![Page 9: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/9.jpg)
Accepted Solution
public static void sort(int[] arr) {
if (arr.length < 2) return;
sort(arr, 0, arr.length - 1);
merge(arr, 0, arr.length / 2, arr.length - 1);
}
static void sort(int[] arr, int start, int end) {
if (end - start == 1) return;
int mid = (start + end + 1) / 2;
sort(arr, start, mid);
sort(arr, mid, end);
merge(arr, start, mid, end);
}
8
![Page 10: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/10.jpg)
Accepted Solution
static void merge(int[] arr, int start,
int mid, int end) {
while (start < end || mid < end) {
if (arr[start] <= (arr[mid])) {
if (start < mid) start++;
else mid++;
} else {
int tmp = arr[mid];
arr[mid] = arr[start];
arr[start++] = tmp;
}
}
}
9
![Page 11: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/11.jpg)
Accepted Solution
Try this example:sort(new int[] {3, 4, 1, 2})
Invariant for merge(...) :Both Parts are sorted ⇒ The whole becomes sorted
10
![Page 12: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/12.jpg)
Debugging
// {3, 4, 1, 2} 0 2 3
merge(int[] arr, int start, int mid, int end) {
// true
while (start < end || mid < end) {
// 3 1
if (arr[start] <= (arr[mid])) {
if (start < mid) start++;
else mid++;
} else { // => swap(0, 2); start++
int tmp = arr[mid];
arr[mid] = arr[start];
arr[start++] = tmp;
}
}
}
11
![Page 13: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/13.jpg)
Debugging
// {1, 4, 3, 2} 1 2 3
merge(int[] arr, int start, int mid, int end) {
// true
while (start < end || mid < end) {
// 4 3
if (arr[start] <= (arr[mid])) {
if (start < mid) start++;
else mid++;
} else { // => swap(1, 2); start++
int tmp = arr[mid];
arr[mid] = arr[start];
arr[start++] = tmp;
}
}
}
12
![Page 14: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/14.jpg)
Debugging
// {1, 3, 4, 2} 2 2 3
merge(int[] arr, int start, int mid, int end) {
// true
while (start < end || mid < end) {
// 4 4
if (arr[start] <= (arr[mid])) {
if (start < mid) start++; // false
else mid++;
} else {
int tmp = arr[mid];
arr[mid] = arr[start];
arr[start++] = tmp;
}
}
}
13
![Page 15: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/15.jpg)
Debugging
// {1, 3, 4, 2} 2 3 3
merge(int[] arr, int start, int mid, int end) {
// true
while (start < end || mid < end) {
// 4 2
if (arr[start] <= (arr[mid])) {
if (start < mid) start++;
else mid++;
} else { // => swap(2, 3); start++
int tmp = arr[mid];
arr[mid] = arr[start];
arr[start++] = tmp;
}
}
}
14
![Page 16: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/16.jpg)
Debugging
// {1, 3, 2, 4} 3 3 3
merge(int[] arr, int start, int mid, int end) {
// false false
while (start < end || mid < end) {
//
if (arr[start] <= (arr[mid])) {
if (start < mid) start++;
else mid++;
} else {
int tmp = arr[mid];
arr[mid] = arr[start];
arr[start++] = tmp;
}
}
}
15
![Page 17: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/17.jpg)
Conclusion
• This merge(...) method is totally crap!• Result of sort({3, 4, 1, 2}) is {2, 1, 3, 4}
• Six JUnit tests failed to detect this!• One of them testing a 17 elements array
16
![Page 18: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/18.jpg)
Toyota - Nothing is Impossible, evenCode that Kills
![Page 19: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/19.jpg)
Unintended Acceleration
17
![Page 20: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/20.jpg)
Unintended Acceleration
• Toyota cars suddenlyaccelerate at full power
• Breakting does NOT stopthe acceleration
• Only way to stop ishandbrakeBreaking distance up to100 meters!!!
• 81 deaths so far!
18
![Page 21: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/21.jpg)
Unintended Acceleration
• Toyota cars suddenlyaccelerate at full power
• Breakting does NOT stopthe acceleration
• Only way to stop ishandbrakeBreaking distance up to100 meters!!!
• 81 deaths so far!
18
![Page 22: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/22.jpg)
First Investigation
• Acceleration code investigated by NASA• Did not find a “smoking gun”• But
• tight timeline• limited information / access (trade secrets)• no exoneration of the system
• Statement of U.S. Transportation Secretary:
“We enlisted the best and brightest engineers to studyToyota’s electronic systems, and the verdict is in. There isno electronic-based cause for unintended high-speed
acceleration in Toyotas.”
• Lesson: Politicians do not know jack shit about software.
19
![Page 23: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/23.jpg)
First Investigation
• Acceleration code investigated by NASA• Did not find a “smoking gun”• But
• tight timeline• limited information / access (trade secrets)• no exoneration of the system
• Statement of U.S. Transportation Secretary:
“We enlisted the best and brightest engineers to studyToyota’s electronic systems, and the verdict is in. There isno electronic-based cause for unintended high-speed
acceleration in Toyotas.”
• Lesson: Politicians do not know jack shit about software.
19
![Page 24: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/24.jpg)
First Investigation
• Acceleration code investigated by NASA• Did not find a “smoking gun”• But
• tight timeline• limited information / access (trade secrets)• no exoneration of the system
• Statement of U.S. Transportation Secretary:
“We enlisted the best and brightest engineers to studyToyota’s electronic systems, and the verdict is in. There isno electronic-based cause for unintended high-speed
acceleration in Toyotas.”
• Lesson: Politicians do not know jack shit about software.
19
![Page 25: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/25.jpg)
Why did NASA not find Anything
• Software in one chip not analyzed at all.Only main CPU software analyzed.
• Toyota told NASA they had EDAC (Error Detection andCorrection)
• But: There was no EDAC for the RAM
20
![Page 26: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/26.jpg)
Why did NASA not find Anything
• Software in one chip not analyzed at all.Only main CPU software analyzed.
• Toyota told NASA they had EDAC (Error Detection andCorrection)
• But: There was no EDAC for the RAM
20
![Page 27: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/27.jpg)
Why did NASA not find Anything
• Software in one chip not analyzed at all.Only main CPU software analyzed.
• Toyota told NASA they had EDAC (Error Detection andCorrection)
• But: There was no EDAC for the RAM
20
![Page 28: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/28.jpg)
Code “Architecture”
256’600 Non-Commented Lines C Source39’000 Non-Commented Lines C Headers (Main CPU only)
??? Proprietary Monitor Chip Software
Code only for acceleration!
21
![Page 29: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/29.jpg)
Code “Architecture”
256’600 Non-Commented Lines C Source39’000 Non-Commented Lines C Headers (Main CPU only)
??? Proprietary Monitor Chip Software
Code only for acceleration!
21
![Page 30: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/30.jpg)
Software Testing @ Toyota
Testing only at vehicle level.
No
• Unit Testing• Integration testing
22
![Page 31: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/31.jpg)
Vehicle Testing not Enough
• Vehicle level testing useful and important• Unexpected component interactions• Environment influences in real-world application
• Complete testing at vehicle level unpractical• Too many combinations of possible conditions, timings• Too many possible sources for failures
• Two faults can counter each other• Source of defects hard to locate
23
![Page 32: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/32.jpg)
Toyota Coding Rules
• 11 of 35 rules suggested for road vehicles found in codingrules
• Rules last updated 1998• Those weren’t followed:105 of 343 switch keywords without default
• 14 of 35 rules violated, 7’134 violations• Macros• Use of #undef
24
![Page 33: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/33.jpg)
Static Code Analysis
• Coverity97 variables declared but not referenced5 include recurision
• Codesonar2272 global variable declared with different types333 cast alters value99 condition contains side-effect64 multiple declaration of global variable22 uninitialized variables
• Uno89 possibly uninitialized variable2 array of 16 byte initialized with 17 bytes
25
![Page 34: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/34.jpg)
Code Complexity
Spaghetti Code
• McCabe Cyclomatic Complexity Metric• Number of “eyes” in flow control graph• Unit tests harder with complex graph• Over 50 considered “untestable”
• Toyota Code• 67 functions with complexity over 50• Throttle angle function: 1461300 LOC, no test plan
26
![Page 35: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/35.jpg)
Global Variables
• Ideal Number: ZERO• Toyota: 9’273 - 11’528 global variables
6’971 local static sufficient1’086 file static sufficient
27
![Page 36: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/36.jpg)
Other Issues
• Poor isolation of task functions• Many large functions• Reviews informal and only on some modules• No configuration management• No bug tracking system• No formal specification
28
![Page 37: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/37.jpg)
Lesson
• Write code you can be confident of it being safe• You should be able to sleep with the knowledge ofsoftware being used in production.
29
![Page 38: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/38.jpg)
Personal Story
30
![Page 39: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/39.jpg)
Personal Story
31
![Page 40: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/40.jpg)
A Story of Knights and Farmers
![Page 41: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/41.jpg)
Singlethreaded Algorithm
32
![Page 42: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/42.jpg)
Multithreaded Algorithm
33
![Page 43: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/43.jpg)
Multithreaded Algorithm
The graphic rendered by the multithreaded algorithm iscorrupt:
• Some pixels have a different color than they should• Some pixels have no color at all
⇒ We need sychronization :(
34
![Page 44: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/44.jpg)
Synchronized Multithreaded Algorithm
private final Semaphore rendezvous;
private final Queue<Knight> knights;
// Implementation: ConcurrentLinkedQueue
public void runMT(int nThreads) {
ExecutorService pool =
Executors.newFixedThreadPool(nThreads);
do {
// do some preparation ... fill knight queue
for (int i = 0; i < nThreads; i++) {
pool.submit(this::run);
}
rendezvous.acquire(nThreads);
} while (/*work to do*/);
pool.shutdown();
} 35
![Page 45: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/45.jpg)
Synchronized Multithreaded Algorithm
private void run() {
while (!knights.isEmpty()) {
Knight knight = knights.remove();
while (!knight.isSatisfied()) {
// do stuff ...
}
}
rendezvous.release();
}
36
![Page 46: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/46.jpg)
Synchronized Multithreaded Algorithm
• The multithreaded variant of the algorithm works (sameoutput as the singlethreaded one)
• It is way faster (factor 2.3 on an Intel Core i3 [2C + HTT])
• Rendering a 1080p scene randomly fails...• Rendering a 4K scene always fails• DEADLOCK
37
![Page 47: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/47.jpg)
Synchronized Multithreaded Algorithm
• The multithreaded variant of the algorithm works (sameoutput as the singlethreaded one)
• It is way faster (factor 2.3 on an Intel Core i3 [2C + HTT])• Rendering a 1080p scene randomly fails...
• Rendering a 4K scene always fails• DEADLOCK
37
![Page 48: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/48.jpg)
Synchronized Multithreaded Algorithm
• The multithreaded variant of the algorithm works (sameoutput as the singlethreaded one)
• It is way faster (factor 2.3 on an Intel Core i3 [2C + HTT])• Rendering a 1080p scene randomly fails...• Rendering a 4K scene always fails
• DEADLOCK
37
![Page 49: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/49.jpg)
Synchronized Multithreaded Algorithm
• The multithreaded variant of the algorithm works (sameoutput as the singlethreaded one)
• It is way faster (factor 2.3 on an Intel Core i3 [2C + HTT])• Rendering a 1080p scene randomly fails...• Rendering a 4K scene always fails• DEADLOCK
37
![Page 50: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/50.jpg)
Debugging
• Deadlock occurs on heavy load• The only blocking structure is that semaphore we added• Debugging prints tell us the semaphore stucks because oftoo few release() calls
• ⇒ some threads never finish
38
![Page 51: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/51.jpg)
Broken Synchronized Multithreaded Algorithm
private void run() {
// ----->
while (!knights.isEmpty()) {
Knight knight = knights.remove();
// <----- IS NOT ATOMIC (but should!)
while (!knight.isSatisfied()) {
// do stuff ...
}
}
rendezvous.release();
}
39
![Page 52: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/52.jpg)
Fixed Synchronized Multithreaded Algorithm
private void run() {
Knight knight;
while ((knight = knights.poll()) != null) {
while (!knight.isSatisfied()) {
// do stuff ...
}
}
rendezvous.release();
}
40
![Page 53: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/53.jpg)
Working 8K Example
41
![Page 54: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/54.jpg)
Conclusions
• Threads can disappear when they throw an uncaughtexception or error
• Threads from ThreadPools do not even log something tostdout/stderr when they die
• Check for exceptions on your own• Use Rust
42
![Page 55: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/55.jpg)
Funny Takeouts
![Page 56: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/56.jpg)
Count it UP!
x = 0;
while x < 5
x = x + 1;
end
%do something with x ...
43
![Page 57: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/57.jpg)
Abbreviations can be tricky!
try {
//...
} catch (SecurityException sex) {
//...
}
44
![Page 58: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/58.jpg)
Redeclaration
public class A {protected String foo;public void setFoo(String fooVal);public String getFoo();public void doSomething() {. . .foo = x.munge();
. . .}};
public class B extends A {/* redeclared here for clarity */protected String foo;public void doSomething() {. . .foo = x.munge();
. . .}}
45
![Page 59: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/59.jpg)
Work for nothing?
int getRandomize(int randMax)
{
srand ( time(NULL) );
int randNum; = rand() % randMax + 1;
return 2;
}
46
![Page 60: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/60.jpg)
OMG - why?
int multiplyBy10(int number)
{
std::stringstream str;
str << number << '0';
str >> number;
return number;
}
47
![Page 61: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/61.jpg)
Correct result but ...
void get_tomorrow_date( struct timeval *date )
{
sleep( 86400 ); // 60 * 60 * 24
gettimeofday( date, 0 );
}
48
![Page 62: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/62.jpg)
Like code structure?
// Not a joke, I've really seen that
for ($i=0 ; $i<3 ; $i++) {
switch($i) {
case 1:
// do some stuff
break;
case 2:
// do some stuff
break;;
case 3:
// do some stuff
break;
}
}
49
![Page 63: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/63.jpg)
Double Kill
$('body *:visible').hide().show();
$('body *:not(:visible)').show().hide()
50
![Page 64: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/64.jpg)
Broken by Optimization
![Page 65: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/65.jpg)
Broken by Optimization
There is an ancient legend, every programmer knows, thataggressive compiler optimizations break your code
51
![Page 66: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/66.jpg)
Broken by Optimization
This legend is true
52
![Page 67: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/67.jpg)
The example
#include "stdio.h"
int main() {
int i, j = 0;
for (i = 1; i > 0; i += i)
++j;
printf("%d\n", j);
}
53
![Page 68: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/68.jpg)
Try the example
$ gcc example.c
$ ./a.out
31
$ �
54
![Page 69: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/69.jpg)
At release...
$ gcc -O3 -Wall example.c
$ ./a.out
�
55
![Page 70: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/70.jpg)
At release...
$ gcc -O3 -Wall example.c
$ ./a.out
^C
$ �
56
![Page 71: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/71.jpg)
gcc -O0 -S example.c
#include "stdio.h"
int main() {int i, j = 0;for (i = 1; i > 0; i += i)
++j;printf("%d\n", j);
}
main:# [...]
movl $0, -8(%rbp)movl $1, -4(%rbp)jmp .L2
.L3:addl $1, -8(%rbp)movl -4(%rbp), %eaxaddl %eax, %eaxmovl %eax, -4(%rbp)
.L2:cmpl $0, -4(%rbp)jg .L3movl -8(%rbp), %eaxmovl %eax, %esimovl $.LC0, %edimovl $0, %eaxcall printf
# [...]57
![Page 72: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/72.jpg)
gcc -O3 -S example.c
#include "stdio.h"
int main() {int i, j = 0;for (i = 1; i > 0; i += i)
++j;printf("%d\n", j);
}
main:.LFB11:
.cfi_startproc
.p2align 4,,10
.p2align 3.L2:
jmp .L2.cfi_endproc
# [...]
58
![Page 73: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/73.jpg)
Well then?
• OK - indeed -O3 is very aggressive
• Trying -O2 …• Same result (even same assembler code!)• Then -O1 ?!• At least this one works:
59
![Page 74: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/74.jpg)
Well then?
• OK - indeed -O3 is very aggressive• Trying -O2 …
• Same result (even same assembler code!)• Then -O1 ?!• At least this one works:
59
![Page 75: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/75.jpg)
Well then?
• OK - indeed -O3 is very aggressive• Trying -O2 …• Same result (even same assembler code!)
• Then -O1 ?!• At least this one works:
59
![Page 76: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/76.jpg)
Well then?
• OK - indeed -O3 is very aggressive• Trying -O2 …• Same result (even same assembler code!)• Then -O1 ?!
• At least this one works:
59
![Page 77: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/77.jpg)
Well then?
• OK - indeed -O3 is very aggressive• Trying -O2 …• Same result (even same assembler code!)• Then -O1 ?!• At least this one works:
59
![Page 78: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/78.jpg)
gcc -O1 -S example.c
#include "stdio.h"
int main() {int i, j = 0;for (i = 1; i > 0; i += i)
++j;printf("%d\n", j);
}
.main# [...]
movl $0, %esimovl $1, %eax
.L2:addl $1, %esiaddl %eax, %eaxtestl %eax, %eaxjg .L2movl $.LC0, %edimovl $0, %eaxcall printfmovl $0, %eax
# [...]
60
![Page 79: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/79.jpg)
What happened?
GCC signed integer overflowoptimization
$ gcc -O3 -fno-strict-overflow example.c produces nearlythe same assembler code as $ gcc -O1 example.c
61
![Page 80: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/80.jpg)
What happened?
GCC signed integer overflowoptimization
$ gcc -O3 -fno-strict-overflow example.c produces nearlythe same assembler code as $ gcc -O1 example.c
61
![Page 81: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/81.jpg)
Broken by Optimization
This legend is trueBut it’s all your own fault :)
62
![Page 82: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/82.jpg)
Santas Sled
![Page 83: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/83.jpg)
Santas Sled
Now, at the end of this talk, let’s have some look at SantaClaus’ sled management software:
• for every reindeer save their name and guide (thereindeer before them)
• save the christmas present for every reindeer• list all reindeers with the present they get
63
![Page 84: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/84.jpg)
Class Reindeer
public class Reindeer {private final String name;private Reindeer guide;
public Reindeer(String name) {...}public Reindeer getGuide() {...}public void setGuide(Reindeer guide) {...}public String getName() {...}
@Override public boolean equals(Object o) {// [...]return Objects.equals(name, reindeer.name) &&
Objects.equals(guide, reindeer.guide);}
@Override public int hashCode() {return Objects.hash(name, guide);
}}
64
![Page 85: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/85.jpg)
Class SantasPlan
public class SantasPlan {Map<Reindeer, String> presents = new HashMap<>();Reindeer leader;
void prepareForChristmas() {Reindeer donner = new Reindeer("Donner");leader = donner;Reindeer comet = new Reindeer("Comet");comet.setGuide(donner);Reindeer blixen = new Reindeer("Blixen");blixen.setGuide(comet);
presents.put(donner, "noise cancelling headphones");presents.put(comet, "a fitness tracker");presents.put(blixen, "new sunglasses");
}
65
![Page 86: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/86.jpg)
Class SantasPlan
void foggyChristmasEve() {Reindeer rudolph = new Reindeer("Rudolph");leader.setGuide(rudolph);leader = rudolph;presents.put(rudolph, "tissues");
}
public static void main(String[] args) {SantasPlan plan = new SantasPlan();plan.prepareForChristmas();plan.foggyChristmasEve();for (Reindeer reindeer : plan.presents.keySet()) {
System.out.println(reindeer.getName() +" gets " + plan.presents.get(reindeer));
}}
}
66
![Page 87: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/87.jpg)
Guess what happens?
1. Everything runs well
Seriously? We’re talking about FAILS!
2. Rudolph does not show up
Why shouldn’t he?
3. Just Rudolph is shown nobody else
Getting closer…
4. Every reindeer is printet but some loose their presents
You got it!
67
![Page 88: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/88.jpg)
Guess what happens?
1. Everything runs wellSeriously? We’re talking about FAILS!
2. Rudolph does not show up
Why shouldn’t he?
3. Just Rudolph is shown nobody else
Getting closer…
4. Every reindeer is printet but some loose their presents
You got it!
67
![Page 89: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/89.jpg)
Guess what happens?
1. Everything runs wellSeriously? We’re talking about FAILS!
2. Rudolph does not show upWhy shouldn’t he?
3. Just Rudolph is shown nobody else
Getting closer…
4. Every reindeer is printet but some loose their presents
You got it!
67
![Page 90: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/90.jpg)
Guess what happens?
1. Everything runs wellSeriously? We’re talking about FAILS!
2. Rudolph does not show upWhy shouldn’t he?
3. Just Rudolph is shown nobody elseGetting closer…
4. Every reindeer is printet but some loose their presents
You got it!
67
![Page 91: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/91.jpg)
Guess what happens?
1. Everything runs wellSeriously? We’re talking about FAILS!
2. Rudolph does not show upWhy shouldn’t he?
3. Just Rudolph is shown nobody elseGetting closer…
4. Every reindeer is printet but some loose their presentsYou got it!
67
![Page 92: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/92.jpg)
Output of main
Blixen gets null
Rudolph gets tissues
Donner gets null
Comet gets null
68
![Page 93: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/93.jpg)
Where is our bug?
@Override
public int hashCode() {
return Objects.hash(name, guide);
}
69
![Page 94: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/94.jpg)
Where is our bug?
• Adding Rudolph as Donner’s guide alters Donnershashcode
• Altering Donner’s hashcode alters Comet’s hashcode …• The HashMap stores the presents under the oldhashcodes
• But looks them up calculating the new ones
• ⇒ FAIL!
70
![Page 95: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/95.jpg)
Where is our bug?
• Adding Rudolph as Donner’s guide alters Donnershashcode
• Altering Donner’s hashcode alters Comet’s hashcode …• The HashMap stores the presents under the oldhashcodes
• But looks them up calculating the new ones• ⇒ FAIL!
70
![Page 96: Oops, I did it again - Funny Programming Fails](https://reader030.fdocuments.in/reader030/viewer/2022012803/61bd203f61276e740b0f9e88/html5/thumbnails/96.jpg)
Merry Christmas
71