https://blog.plover.com/2017/11/12/ The Universe of Discourse Mark Dominus (Tao Sun, 12 Nov 2017 Min Xiu ) mjd@pobox.com No, it is not a compiler error. It is never a compiler error. [TOP] When I used to hang out in the comp.lang.c Usenet group, back when there was a comp.lang.c Usenet group, people would show up fairly About me often with some program they had written that didn't work, and ask if their compiler had a bug. The compiler did not have a bug. The RSS Atom compiler never had a bug. The bug was always in the programmer's code and usually in their understanding of the language. 12 recent entries When I worked at the University of Pennsylvania, a grad student Leopards, faces, posted to one of the internal bulletin boards looking for help with a etc. program that didn't work. Another graduate student, a super-annoying Think speak, now know-it-all, said confidently that it was certainly a compiler bug. speak! It was not a compiler bug. It was caused by a misunderstanding of the David McShane's way arguments to unprototyped functions were automatically promoted. mural with 18 Franks, revealed This is actually a subtle point, obscure and easily misunderstood. David McShane's Most examples I have seen of people blaming the compiler are much mural with 18 sillier. I used to be on the mailing list for discussing the Franks development of Perl 5, and people would show up from time to time to Genealogy of the ask if Perl's if statement was broken. This is a little House of Reuss mind-boggling, that someone could think this. Perl was first released Surnames from in 1987. (How time flies!) The if statement is not exactly an obscure nicknames nobody or little-used feature. If there had been a bug in if it would have has any more been discovered and fixed by 1988. Again, the bug was always in the Just give the man programmer's code and usually in their understanding of the language. the fish! Claude helps me Here's something I wrote in October 2000, which I think makes the find more case very clearly, this time concerning a claimed bug in the stat() presidential function, another feature that first appeared in Perl 1.000: emoji Polish Spam On the one hand, there's a chance that the compiler has a broken Just ANSWER THE stat and is subtracting 6 or something. Maybe that sounds likely QUESTION to you but it sounds really weird to me. I cannot imagine how A complex bug such a thing could possibly occur. Why 6? It all seems very with a [?]simple[?] unlikely. fix Another corner of Well, in the absence of an alternative hypothesis, we have to Pennsylvania take what we can get. But in this case, there is an alternative hypothesis! The alternative hypothesis is that [this person's] Archive: program has a bug. 2025: JF Now, which seems more likely to you? 2024: JFMAMJ JASOND + Weird, inexplicable compiler bug that nobody has ever seen 2023: JFMAMJ before JASOND 2022: JFMAMJ or JASOND 2021: JFMAMJ + Programmer fucked up JASOND 2020: JFMAMJ Hmmm. Let me think. JASOND 2019: JFMAMJ I'll take Door #2, Monty. JASOND 2018: JFMAMJ Presumably I had to learn this myself at some point. A programmer can JASOND waste a lot of time looking for the bug in the compiler instead of 2017: JFMAMJ looking for the bug in their program. I have a file of (obnoxious) JASOND Good Advice for Programmers that I wrote about twenty years ago, and 2016: JFMAMJ one of these items is: JASOND 2015: JFMAMJ Looking for a compiler bug is the strategy of LAST resort. LAST JASOND resort. 2014: JFMAMJ JASOND Anyway, I will get to the point. As I mentioned a few months ago, I 2013: JFMAMJ built a simple phone app that Toph and I can use to find solutions to JASOND "twenty-four puzzles". In these puzzles, you are given four 2012: JFMAMJ single-digit numbers and you have to combine them arithmetically to JASOND total 24. Pennsylvania license plates have four digits, so as we 2011: JFMAMJ drive around we play the game with the license plate numbers we see. JASOND Sometimes we can't solve a puzzle, and then we wonder: is it because 2010: JFMAMJ there is no solution, or because we just couldn't find one? Then we JASOND ask the phone app. 2009: JFMAMJ JASOND The other day we saw the puzzle <<5 4 5 1>>, which is very easy, but I 2008: JFMAMJ asked the phone app, to find out if there were any other solutions JASOND that we missed. And it announced "No solutions." Which is wrong. So 2007: JFMAMJ my program had a bug, as my programs often do. JASOND 2006: JFMAMJ The app has a pre-populated dictionary containing all possible JASOND solutions to all the puzzles that have solutions, which I generated 2005: OND ahead of time and embedded into the app. My first guess was that bug had been in the process that generated this dictionary, and that it had somehow missed the solutions of <<5 4 5 1>>. These would be indexed ----------------- under the key 1455, which is the same puzzle, because each list of Subtopics: solutions is associated with the four input numbers in ascending order. Happily I still had the original file containing the Mathematics 242 dictionary data, but when I looked in it under 1455 I saw exactly the Programming 99 two solutions that I expected to see. Language 95 Miscellaneous 72 So then I looked into the app itself to see where the bug was. Code Book 50 Studio's underlying language is Javascript, and Code Studio has a Tech 49 nice debugger. I ran the app under the debugger, and stopped in the Etymology 35 relevant code, which was: Haskell 33 Oops 30 var x = [getNumber("a"), getNumber("b"), getNumber("c"), getNumber("d")].sort().join(""); Unix 27 Cosmic Call 25 This constructs a hash key (x) that is used to index into the canned Math SE 25 dictionary of solutions. The getNumber() calls were retrieving the Physics 21 four numbers from the app's menus, and I verified that the four Law 21 numbers were <<5 4 5 1>> as they ought to be. But what I saw next Perl 17 astounded me: x was not being set to 1455 as it should have been. It Biology 15 was set to 4155, which was not in the dictionary. And it was set to Brain 15 4155 because Calendar 15 Food 15 the built-in sort() function [mjd-univer] was sorting the numbers Higher-Order Perl Blosxom into Comments disabled the wrong order. [WTF] For a while I could not believe my eyes. But after another fifteen or thirty minutes of tinkering, I sent off a bug report... no, I did not. I still didn't believe it. I asked the front-end programmers at my company what my mistake had been. Nobody had any suggestions. Then I sent off a bug report that began: I think that Array.prototype.sort() returned a wrongly-sorted result when passed a list of four numbers. This seems impossible, but ... I was about 70% expecting to get a reply back explaining what I had misunderstood about the behavior of Javascript's sort(). But to my astonishment, the reply came back only an hour later: Wow! You're absolutely right. We'll investigate this right away. In case you're curious, the bug was as follows: The sort() function was using a bubble sort. (This is of course a bad choice, and I think the maintainers plan to replace it.) The bubble sort makes several passes through the input, swapping items that are out of order. It keeps a count of the number of swaps in each pass, and if the number of swaps is zero, the array is already ordered and the sort can stop early and skip the remaining passes. The test for this was: if (changes <= 1) break; but it should have been: if (changes == 0) break; Ouch. The Code Studio folks handled this very creditably, and did indeed fix it the same day. (The support system ticket is available for your perusal, as is the Github pull request with the fix, in case you are interested.) I still can't quite believe it. I feel as though I have accidentally spotted the Loch Ness Monster, or Bigfoot, or something like that, a strange and legendary monster that until now I thought most likely didn't exist. A bug in the sort() function. O day and night, but this is wondrous strange! [ Addendum 20171113: Thanks to Reddit user spotter for pointing me to a related 2008 blog post of Jeff Atwood's, "The First Rule of Programming: It's Always Your Fault". ] [ Addendum 20171113: Yes, yes, I know sort() is in the library, not in the compiler. I am using "compiler error" as a synecdoche for "system software error". ] [ Addendum 20171116: I remembered examples of two other fundamental system software errors I have discovered, including one honest-to-goodness compiler bug. ] [ Addendum 20200929: Russell O'Connor on a horrifying GCC bug ] [Other articles in category /prog] permanent link