 Yeah, you won't understand anything I say Okay, so I got started I Think we'll get started What up? Yeah, all right, so disclaimer at first. So like I said, I have a cold So this is for me. This is an experiment. I like to program. Obviously everybody likes program I like to debug a lot. I thought it would be nice to talk about bugs. I'm not sure how to do that actually So like I said, it's an experiment I'm not an expert. That's the most important thing. I'm not an expert in the room. I don't believe them. Don't believe anything I say without said believe everything I say The keyword in this slide at least It's a lame joke, but the key keyword here is story, you know, so I'm not sure this is working very well. So let's just do this. Oh Yeah, sorry. Yeah, so the keyword here is a story now So to me at least every bug has a story now I have a story behind it and the context and the story of the bugs very important You know, I just noticed because I have a cord what cold and I'm doing this next speaker is gonna have hell. Sorry About how would that there? Yeah, excellent. Yeah Yeah, that's working well. Yeah, so Like I said in the description which probably never nobody read These are all bugs that I came across while doing some stuff And I tried to rewrite them for presentation write a story around them to be kind of funny. Maybe hopefully and Maybe somebody will learn anything. I don't know. I will certainly learn something like not to do this ever again Okay, so We because of the Duke in the Duke title, I thought let's do a whole scenario scenario about Duke So, yeah, this is Duke King Duke, you know, he's bored He doesn't know what to do with his life. You know, so he tries some stuff still bored, you know There's a lot of stuff fucking bored, you know, so he just says, you know, fuck it. I'm gonna go to sleep So all is sleeping, you know Duke gets an epiphany a dream, you know a vision. I'm gonna go to space not sure why but whatever it works Yeah, I'll find these was easy to find these pictures, you know on the internet. So just use them. Yeah So Duke, you know, he decides, okay, let's I'm gonna go to space So, you know, he goes online and learns how to go to space and or there's some stuff and the stuff I think it come. Yeah, it comes along. He starts building. Yeah, and you know, that's when I shit starts to go sideways Sorry. Yeah, this way. Yeah, this one. This sounds nicer It's wrong. Yeah Yeah, okay. This is hard On the on the future. Yeah, it's buzzing because of this thing Excellent Yes, I have a goal, you know, I can't think straight. So this better. Yeah Excellent. Thank you. So, you know, he starts building and this is the first book, you know So I have a couple of bugs and this is the first one So the first bug or the first system he tries to build is the fuel gauge, you know Very simple system. I'm not sure why Duke is doing it in, you know with a program But you know just because he can and because it works for my presentation So he builds a fuel gauge Small piece of program Small program small piece of code program small program And it monitors the fuel, you know as you're filling it up, etc Suddenly your fuel tank overflows and I know fuel tanks don't look like this, but this works for my code It's much easier. You know, it's just bear with me. Yeah so It overflows Duke doesn't understand why why does it overflow? You know, it's a very simple program, you know It's just it calculates the cylindrical Volume every time you fill some fuel in, you know, and it calculates the Delta's and then this doesn't make any sense I know, but okay, so let's just look at some code. It's pretty easy actually So the volume of a cylinder is basically the area of a circle times the height Very simple. I think it's Yeah, there calculate fuel volume. That's it, you know As you can see I use the pie the unicode pie for pie, which I'm very proud of. Yes. Thank you. Thank you. Yeah, so It looks a bit ugly, but okay So it's very simple, you know, and you fill some fuel in, you know, and With the function fill you say, okay. I filled. I don't know one centimeter. Okay, let's go click one city Me to add it to the total it's not right. It's not what it starts to go wrong, you know Overflows like I said So, so Duke he writes some test code which which I never do actually but test code and He prints out Okay, in retrospect, I should have maybe printed out the values, but doesn't matter So he chooses the fill rate of 100, you know 100 centimeter one meter for example per second or something and he calculates the values and works He doesn't understand why it works or he's glad that it works But why doesn't it work when in a real-world scenario? He does another experiment With 10, you know, even less and it doesn't work and it not only doesn't work, you know It's it prints out a value which is less than the total value should be, you know I think by two or something and again, maybe I should have put that on the slides But you know, like I said, this is new experimental shit. So it doesn't work Okay, he starts looking at the code again, not sure which slide is next. Okay, let's go back He starts looking at the code again, you know, can understand why it doesn't work. He starts debugging I'm not sure how to explain how I debug this, but he starts debugging. Okay, just debugging and Turns out something very simple something one of the things everybody knows everybody understands. Nobody applies, you know And if you look at the code, you know So if you look at it Yeah, yeah, I'm not duplicating the screen. So I have to do this So if you look at the code at the top, we're using a lot of floats and instant such But you look at the methods everything or almost everything is a double, you know instead of one line where we Cast it to a float and add it to yet another double so what we have here is The fader to communicate what we have here is Is what's it called? Oh Sorry, we do have a bug. Yes. Thank you very much. Yes But what is the bug is? You give a float to a double and then you do some calculations on it as if it is a double and then you return it and In I don't know 99 of 100 cases. This will work excellent, you know But I chose these values explicitly to make a break, you know And that's what happens actually now. I we widened the flow to a double we do some calculations on it and Then we return it as a double and then we cast it back to a float Which is not the same as doing the same exact calculations on the float. Everybody knows this, but you know It's a bitch to find these kind of bugs. That's the first bug fuel Duke fixes it on to the next bug Yeah, not sure Yeah, so What's the second most points I think for a space flight Um, yeah, so to me or you know to work with this Narrative story, it's an in-flight entertainment system. This is the most important thing the second most important thing So Duke thought, you know, I'm gonna build myself if I'm in the same system I don't want to be word on on my way to whatever. Yeah, so we start building something and You know Duke likes to read a lot. So he made he started out with a book list A huge book list By the way, all this code is going to be you can download it somewhere And you know He tested it it works, you know kind of you know and Just before you know, he's starting to do the final preparations to go to space Effective Java third edition comes out, you know, which deserves an applause. I think so he added adds it to the book list and He doesn't see it in the system for some reason. He doesn't know why and This this is basically how the how the how the list should look, you know, and At the top that's that's the format of every line So you got a title on the from and it's basically just a huge text file with a lot of books, you know And you have the title of the book then, you know, okay, it starts at bite I don't know so here everything starts at the same bite because I was too lazy But it starts by 33. For example, it's at ends at 45 But he doesn't see effective Java. He doesn't know why he doesn't see effective Java. Why the fuck so he starts looking at his code so Some easy stuff I'm gonna skip some of the stuff because it's not it's necessary for debugging But it's gonna be boring if I say everything So he just reads in the file. He parses it. We're gonna look how I think Yeah, is this readable actually? Excellent. I'm very sorry. I I tried making it as big as I could so this is not gonna work actually if it's not readable Nobody can read this not even people in the front row Okay, sorry people I'm very sorry so So yeah, he does some basic this is basically You know like script parsing interpretive parsing. This is very simple stuff, you know, you just you you get a Character buffer with all the characters and you then you loop every every over every character is the letter. Okay Yeah, that kind of stuff is an allowed character that I added is not allowed character. Then I break the program. Yeah So it looks very simple Yeah, I Think for the for the people in the front row. It looks very simple at least, you know, but Yeah So Duke, you know, he starts looking at the list more more closely at the output The list of books we do have, you know, but the ones we don't have and you notice something is this is the last book of the list We actually have the beloved by Tony Morrison, okay Yeah, okay, sorry So Something goes wrong or it's an educate. I know what's wrong with the bug of course But an educated guess would say okay Something goes wrong either with the end of this line or something in the beginning of the next line or from this line on forward and If you look at the really Okay, if you look at this the one below the blue lines Jane Iyer or air and Charlotte's Bronte you can see the E with the I Don't know what's called so but if you see that one It looks peculiar, you know, I mean even note that plus plus says this is not cool So we go back and we look at the code Yeah, I'm gonna speed this up a little bit because I'm running outside and the other bugs are more interesting So basically this the idea behind this Is For the people who can't see the first line above We're using a character set of utf-8 Yeah And that's that's the incorrect characters as the actual characters that should be something like iso The problem with this is here So there You can see we we do a lot of the character is scared is digit is is white space that kind of stuff and that that stuff Only that's what is actually being parsed. It doesn't test what the bytes Code underneath it, you know you parse it with the incorrect characters that so When you when you you know, you see it a lot when you do it in notepad or something you open the file with incorrect Character set you see those, you know rombuses with with with question marks in them and that's an invalid character So that's why the why the application brings so yeah Duke continues to character set and everybody is happy So, oh, yeah, so do my malicious to get space lift off and then Another program that's actually important and this time it really is important, you know, it's it's kind of like a radar You know, you don't want one to be a yeah Yeah, thank you. So he doesn't want to collide with any asteroids or whatever space junk So we built in a radar, you know as a collision detection system But you know Duke lives in 2d space who is only doing it for 2d, which is much easier Which and this I'm not real at all. I know But anyway, so the problem is, you know The system it checks for collisions and it says okay incoming asteroid. You're not going to collide with it And he starts colliding with it, you know, it keeps happening keeps happening keeps happening and As you can imagine, it's annoying so He don't say the code Can anybody read this any rows at all? Yeah You can read this I'm really sorry guys I'm not sure what to do. I could I could do that So I'm gonna try to read the important bits, but like I said, I'm not mirroring the monitor So I'm gonna do this kind of stuff because I'm right from the state here. Sorry Yeah, I'm not gonna jump anymore. Yeah, okay, so that's certain so How how it works Okay, so how it works is We basically just plot two lines and one from From the spaceship to your direction and one from the asteroid to its direction and we just see if they intersect We see the point of intersection and then we calculate based on our speed and asteroid speed when the asteroid will hit that point And when we will hit that point very simple It's like, you know what they do with the ships, you know sailing ships and that kind of stuff on a big map and Composers and the cool stuff, you know, which is actually what I did to do this So because the actual bug was a 3d and Very difficult to explain because I don't understand very well myself. So So that's basically what we do here. Yeah So the important part of this equation we just assume the first part works because it does work I know that and we are running out of time. So the first part we assume that it works. Yeah The two lines before the if statement those are the important lines So that's basically we know that there is going to be a collision. We're going to calculate the collision of both the speeds Now the time to arrive to the collision point for the ship and for the asteroid. Yeah, I Think this is more readable Okay, so I'm going to skip this one. So Sorry, I'm not doing that on purpose. It wasn't important. Yeah, I think this one is more real than this is important So these this is basically, you know, you just calculate the distance between the ship and And asteroid and the collision point and you know You divide it by the time and you do the same for the asteroid You can see that the functions are slightly different. No, they're basically the same mathematically speaking. They're Yeah, I Don't remember. Okay. Yeah, so And here lies the problem Because running out of time I'm going to do this quickly. So here here lies the problem This is a mistake in case of operator precedence And the operator precedence in this context shouldn't really matter, you know, because you're basically doing the same mathematical operation You're actually doing exactly the same mathematical operation. Does anybody know why this doesn't work? Okay, sorry So Unfortunately, you can't see this slide very well, but the values I chose I chose them very particularly so if you can look at the code later just test it out and One of these I think the one on top will actually return zero and The one at the bottom will return the actual value and Both should actually return the actual value and The reason for that is, you know, because we're doing something different with the precedence of the operators And the one on the top we're calculating what's between the brackets first Dividing it by two and then we're subtracting it from the distance ship And that results in this case it overflows because it's a flows and it results in a zero if you use double for the same thing You'll get the same values but You know, that's not the bug the buggy. This is the actual bug, you know Problem with operator precedence, you know, you think you're doing something fancy mathematically speaking and such and just you know Shoot yourself in the foot Which I did actually So Yeah, the problem is we're using different equations, you know, we should either use the same equation for both The same correct the equation for both of the same wrong equation for both perfectly the correct one So that's that the last one. I think it's more interesting. I hope so I hope so. So yeah, what's the most important thing on space flight? I Found this cool picture. I hope anybody can read it One guy in front work. He's married. I think so that helps. So yeah, so so yeah, the Duke is You know, he's lonely in space. He wants to Look at a picture of his wife, which people tend to do I think on the lonely or not occurs or I don't know But he wants to look at the picture of his wife And he built a fancy program to do that for him because He does that and this is what he gets, you know, I don't know about you, but doesn't look correct Which I think isn't okay, this is readable, but this doesn't look very much. It's your main function. So display image Think this is more readable That's readable. Sorry How much time do I have? Five minutes So yeah, so He reads an image, you know, he converts it to a buffered image Not sure what that does. Let's check it here Yeah, I didn't include that one, but okay So basically the convert image calls this function Yeah, so the image is a YUV image if anybody knows what it is kind of weird format Where they put the bytes in a very peculiar order and you have to convert them to RGB to be able to view them on a computer now This is basically How you do it? This is just, you know, lifted from Wikipedia. So it's not very complicated It's not very important actually to the bug But what he does here On the third line or the second line of the function itself, you can see you get a byte buffer as an input, you know And we create an int buffer from the byte buffer, you know Just one of the features of byte buffers. Yeah But what you see there is he doesn't duplicate on the byte buffer That's very important because if anybody could see the previous slide Yeah So I can't jump anymore, but if you look at the top right there when we create the By buffer we created a byte buffer as little ending Yeah But one of this is a Java bug, you know, so it's not my personal bug You know, this was actually in Java itself. It's still there. I think When you do duplicates on a byte buffer, you do duplicate you duplicate everything Except the endianness of the byte buffer So this is a big ending byte buffer and he writes to it as if it is a big ending byte buffer So that's why RGBA gets twisted around with something like B-A-R-G or something like that. That's why you've got that weird lady Yeah, you just duplicate it with the right endianness and everything is cool And that's what you get So yeah, that's that's it links for the for the code and such and If you have any questions, which I doubt We're gonna learn all about methanandles. Well, you're gonna learn 25 minutes worth of methanandles, I guess Yeah, yeah exactly Yeah This guy away from it here