• John Hensley (unregistered)

    There's no way this boss has programmed in anything other than QBasic.

    I also suspect that the previous developer was just "uncooperative" rather than unskilled.

  • (cs) in reply to toxik

    toxik:
    makomk:
    eddieboston:
    1) The whole premise of writing to a file to do an SQL query.

    2) Using one function to do three different tasks, and passing in an arbitrary, hard-coded ActionFlag to determine what to do.

    3) Can anyone say "memory leak"?

    4) The "C Cheat-Sheet" at the end.

    5) The response variable.  First of all, it's assigned to and never used.

    6) ...and second of all, is he really adding together two return values from the "system" function???  WTF?!

    7) The two busy loops that "wait for database to return data."  I mean, seriously, where do they teach this stuff?

    and finally...

    8) It's not in Visual Basic.

    Seriously, I could go on, but I need new goggles.

    9) Executing DEL using system() to delete a file

    10) The buffer overflow (which Grimoire mentioned)

    11) The failure to escape spaces in the filename... if this is UNIX, he'd probably be able to get away with it, though.

    12) The fact that it makes the Python/Perl programs that used popen() to run the command-line mysql client and execute commands look like a huge improvement.

    13) The use of strcat over sprintf (I know, I'm scraping the bottom of the barrel here...)

    I'm sure there are more...


    14) The name of the function. execSqlCommand - yet it can delete files which has nothing to do with the acctual SQL interfacing.

    15) Same loop to wait for the database to return data is executed for deleting files.

    16) "-i" <FONT color=#000000>is added with a separate strcat instead of being included in the original strcpy.</FONT>

    <FONT color=#000000>17) Same three lines of code in Case 1 and Case 2. Failure to exploit leaving a break; out in this rare case when it actually would've helped.</FONT>

    It's a classic Tufte WTF: the more you look at it, the more WTF it becomes.

    --RA

  • (cs) in reply to marvin_rabbit
    marvin_rabbit:
    makomk:
    Anonymous:
    makomk:
    Anonymous:
    This is just perverse. And I like how he adds another cycle through incrementing x in the execute field of the for AND in the loop code increments z (whereas both could be done in the execute field of the for or in the looped code)
    One increments z, the other x - they're different vars. The value of z is never actually used for anything (and it's initialised to 0 redundantly, too). Why are there *two* for loops, anyway?

    (wonder if this is going to work... no, probably not)

    Yeah, but he could as well just
    for (;;x++,z++) {}
    or just not increment z at all >_>
    I know; I misread the post. Sorry. I vote for not having z at all as being the sane solution. (Of course, this is The Daily WTF - if everyone went for the sane solution, we'd have nothing to mock).

    OH MY GOD!!

    Are we REALLY debating the most efficient code to lock the CPU at 100% while LOOPING TO 2 BILLION.

    Let's remember what we're doing here... If we're looping to 2 Billion, code efficiency gots nuttin to do with it!!

    That's like arguing about,  um about... arguing about something really stupid when it doesn't really matter anyway.  (I never was good at analogies.)

    Yeah, specially since he could sleep(n) to lock the process for n seconds at zero percent of CPU. Talk about efficency. And it's not compiler dependant!

    What? Oh, yeah, he could have, of course, written the function so that it would need no delay whatsover...

    What? He didn't need ANY delay AT ALL in the first place? Hummmm, not very experienced in maintaining low-end computers with low reliability SOs, right? (any cheap PC with a random Linux distribution will do) The operative system may be taking a few milliseconds to close the file, so this function would return, and another part of the program would attempt to open the file and get an error because the file is open.

    As for my experience, restarting certain JSP server software in my web server needs a 5 seconds delay between stopping and starting the program, because Linux will take a pair of seconds at least to close the TCP port, and the JSP server will fail inmediatly instead of waiting until the port is cleared.

    This is, of course, a different WTF, and won't be told here............

  • (cs)

    Well...

    At least he didn't cast the return value of malloc(); that has to count for <i>something</i>...

  • (cs)
    Alex Papadimoulis:
    ...One his first day, his boss gave him a thoughtful heads-up about the code in the system: the guy before Marc was a really bad coder and had unfortunately contributed to a large portion of the application....
    <font size="5">I</font>'m going to go out on a limb here: the really bad coder is the boss.
  • (cs) in reply to voodooc
    voodooc:
    What cracks me up is -- did anyone consider, maybe the prior programmer WASN'T so bad... but he refused to use the 'best practices' of the Clearly Elite Boss?

    Insincerely,

    Voodoo C.

    Yeah, I was thinking that myself.  The Ex-Programmer might have been the ONLY qualified guy around that place.

    Six months from now, the boss will be talking to 'The Next New Guy', saying "You know, the last guy we had in here was a real ass-hat.  But we're moving forward now..."

  • (cs) in reply to marvin_rabbit
    marvin_rabbit:
    voodooc:
    What cracks me up is -- did anyone consider, maybe the prior programmer WASN'T so bad... but he refused to use the 'best practices' of the Clearly Elite Boss?


    Yeah, I was thinking that myself.  The Ex-Programmer might have been the ONLY qualified guy around that place.

    Six months from now, the boss will be talking to 'The Next New Guy', saying "You know, the last guy we had in here was a real ass-hat.  But we're moving forward now..."


    Quite.  I can see how it could happen.  I have sometimes pointed out bad consequences of an action, and my statement has not been popular.  That does not stop the consequences from happening though.

    "Look out for that train!"  "Quit being so negative."  SPLAT!

    Sincerely,

    Gene Wirchenko

  • .* (unregistered) in reply to Enric Naval
    Enric Naval:
    What? He didn't need ANY delay AT ALL in the first place? Hummmm, not very experienced in maintaining low-end computers with low reliability SOs, right? (any cheap PC with a random Linux distribution will do) The operative system may be taking a few milliseconds to close the file, so this function would return, and another part of the program would attempt to open the file and get an error because the file is open.



    WTF?? Linux does not keep open file handles around once you close them, and even if you forgot to close them opening the same files gives no error (unless you explicitly locked the file).

    Enric Naval:

    As for my experience, restarting certain JSP server software in my web server needs a 5 seconds delay between stopping and starting the program, because Linux will take a pair of seconds at least to close the TCP port, and the JSP server will fail inmediatly instead of waiting until the port is cleared.



    WTF again! No OS takes "a pair of seconds" just to close a TCP port.
  • G. Nazi (unregistered)

     

    I'm not sure what's worse, that code or that spelling of "rediculous".

  • (cs) in reply to toxik
    Anonymous:

    (wonder if this is going to work... no, probably not)

    Yeah, but he could as well just
    for (;;x++,z++) {}
    or just not increment z at all >_>

    Indeed, infinitely incrementing loops will delay execution quite well. Some might say a little too well.
  • (cs) in reply to foxyshadis

    twichs

    God, its like some fratcal WTF. The more you look at it, the more WTFs become visable. Geez, I've got to look away before my brain goes splat! (although this WTF reminds me of the SQL Driver one from a few months back).

  • (cs)

    Losing 1K of memory every time you execute an SQL statement is nothing to call a "minor wtf"...

    Also, i'm going to bet on hardcoded file names, so buffer overflows and quoting file names I don't think should be on the WTF list. In addition, since this is Windows code (or DOS?) and old, since its in C, I'd point out that the 'n' functions like strncat might not have been available, and that there may not have been any other way to execute queries other than the cmdline client. It may be possible that writing to files was the ONLY way to get this done.

    Also, if it were DOS, then there is no possible way that a delay would be needed, since DOS was singlethreaded. So it was probably Win95 or Win3.1

  • (cs) in reply to .*
    Anonymous:
    Enric Naval:
    What? He didn't need ANY delay AT ALL in the first place? Hummmm, not very experienced in maintaining low-end computers with low reliability SOs, right? (any cheap PC with a random Linux distribution will do) The operative system may be taking a few milliseconds to close the file, so this function would return, and another part of the program would attempt to open the file and get an error because the file is open.



    WTF?? Linux does not keep open file handles around once you close them, and even if you forgot to close them opening the same files gives no error (unless you explicitly locked the file).

    You don't know if the rest of the code will open the file in read-write mode (as opposed to readonly mode), either for making changes to it or because of sheer dumbness. And then you'll get an error.

    And, of course, the original WTF uses windows, since it's using the DEL command :)

    Then, there are also those nice race-conditions (see below).

    Enric Naval:

    As for my experience, restarting certain JSP server software in my web server needs a 5 seconds delay between stopping and starting the program, because Linux will take a pair of seconds at least to close the TCP port, and the JSP server will fail inmediatly instead of waiting until the port is cleared.



    WTF again! No OS takes "a pair of seconds" just to close a TCP port.

    Well, my Redhat 9 box does. It "normally" takes much less than a second, so I initially set the delay to 2 seconds, just to be on the safe side. But once in a while it would take more than 2 seconds and I would find in the morning that the JSP server couldn't start because the port was binded to a different process (the older process).

    I also thought about simply starting again and again until the port was opened but that had its own problems, namely race conditions between the two processes. They are mostly solved on version 3, but version 2 did this all the time.

    Preliminary explanation: the JSP server uses a .pid file to see if other instances of it have been already started.

    1. I launch the first instance by firing a perl script, "httpd start", that checks that the file doesn't exist, and prepares itself to create it. Since the file stores the process PID, the script asks the OS for its own PID.
    2. Then the perl script finishes its alloted CPU time, so the SECOND perl script, "httpd start" again, that I have fired checks the pid file, sees that it doesn't exist, asks for its own PID, writes it and launches the JSP server. (Remember I want to launch scripts until one succeeds)
    3. The first script comes back, OVERWRITES THE FILE with its own pid and proceeds to launch *another* JSP server.
    4. Since java doesn't like having two JSP servers using the same classfiles, none of them will work. (no, I don't know why, and yes, it's a WTF. Running Tomcat and another JSP server on the same /usr/java/jdkx.x directory also gave me weird errors or no working at all)
    5. So I try the "httpd stop" command on the perl script, but it can only stop the first process, because it uses the PID stored in the .pid file, and it is currently storing only the PID for the first instance.
    6. Also, I run "httpd start" again and it works like a charm, since the "httpd stop" has successfully deleted the .pid file, which was not locked by the software (oooops) and I get stuck with two running processes.
    7. Unfortunately, the second instance is still there and nothing works. In older versions of the software, it will even apparently bind to the port in spite of it being already binded. Is it failing to bind but not reporting any error? is it a linux error or an error on the program? WTF??
    8. There are other non linux-specific errors. When I kill -9 the JSP server instead of using the perl script and it will refuse to start because the old .pid file is still there. Or when it failed to start but didn't delete the .pìd file. Fortunately, they seem to have solved most of these nuisances in newer versions.....

  • (cs) in reply to .*
    Anonymous:


    WTF again! No OS takes "a pair of seconds" just to close a TCP port.

    One note: this is probably worsened by the perl script probably throwing a signal to the process, then returning inmediately. This means that if I launch a new process as soon as the close script returns, the process may be still attempting to clear all reasources, closing files, and, of course, unbinding itself from TCP ports.

    Even if the perl script waited until the process had finished, it is conceivable that returning from a perl script and automatically launching a new perl script may take less time than the OS clearing all resources from the process (or the perl script having been lucky in the CPU execution queue), and that the OS has not had enough physical time to unbind the TCP port in time for the second process because it was clearing other things first.

    Final note: an OS may say that it has closed a process and cleared the resources, but behind the scenes it is still clearing resources. Think of this as a hard disk reporting that a file has been written, when it is actually still sitting in the disk cache. All this in the name of performance, of course.

    Sooooo, to this, you add different combos of OS (windows NT,2000,2003,XP,SP1,SP2,SP3,Debian,Redhat 8,9, Fedora 1,2,3,4,5-test3, Linux Enterprise server), all the combinations of hard disk/motherboard/network card, and the use of different kernel versions (Redhat 9):

    [root@griho root]# rpm -qa | grep kernel
    kernel-2.4.20-28.9
    kernel-2.4.20-31.9
    kernel-2.4.20-20.9
    kernel-2.4.20-30.9
    kernel-2.4.20-27.9
    

    And a Fedora Core 2 server, where I also had some trouble with JSP server, is not better. I have used ALL of these along one year, one after the other:

    [root@webquest Activitat-26]# rpm -qa | grep kernel
    kernel-smp-2.6.5-1.358
    kernel-smp-2.6.9-1.6_FC2
    kernel-smp-2.6.8-1.521
    kernel-smp-2.6.10-1.770_FC2
    kernel-smp-2.6.9-1.3_FC2
    kernel-smp-2.6.9-1.11_FC2
    kernel-smp-2.6.10-1.9_FC2
    kernel-smp-2.6.10-1.771_FC2
    

    Guess what, some of those versions will refuse to boot my server for no apparent reason. How many of those have TCP port closing bugs wihout me knowing?

    Now, finally, compare this with a SUN server with Solaris, or some other enterprise level hardware-software combo, and check uptimes, reliability, etc. Of course, they also have race conditions and such, but they are free of some of the odities of "normal" OS.

  • yes it is (unregistered)

    I bet the boss claimed to have written that code because he was sick of some noob programmer whinging about the last guy.

  • BackInserter-&gt;pop() (unregistered) in reply to baldheadedguy

    ....unless you make 'x' and 'z' volatile [:D]

  • Captain Planet (unregistered) in reply to BackInserter-&gt;pop()

    ActionFlag! He's our hero!

    ActionFlag!

  • (cs) in reply to BackInserter-&gt;pop()

    Apparently, this guy is a 'boss' in the video game sense; i.e., some evil badass you have to overcome to reach your goal.

  • John Hensley (unregistered) in reply to Schol-R-LEA
    Schol-R-LEA:
    Apparently, this guy is a 'boss' in the video game sense; i.e., some evil badass you have to overcome to reach your goal.

    Good one. I guess the secret to beating him is to attack during the delay loop.
  • (cs) in reply to Grimoire
    Grimoire:

    Why do so many C coders think that 1024 is "big enough" for a temporary string?


    Most likely, they just hope it is, as they don't know any better. realloc(), anyone?

  • (cs)

    God. If I happen to read one more post containing the word goggles, or brillant, or brillance even, or first post,

    im going to KILL MYSELF.

    Die! you irritating F**KS[6]

    I need coffee.

  • (cs) in reply to Gene Wirchenko
    Gene Wirchenko:


    "Look out for that train!"  "Quit being so negative."  SPLAT!



    Bwahaha! A lot of guys say just that when crossing a railroad in their car. Guess their brain stops working once they start the engine...
  • robymus (unregistered) in reply to .*

    Anonymous:
    WTF again! No OS takes "a pair of seconds" just to close a TCP port.

    Sorry for being not to specific on the terms, but.. after closing a socket, it will remain in (i don't remember what) state, and can not be reused again for a few seconds (os dependent), except with the SO_REUSEADDR socket option. Caused me a lot of trouble, when testing java server application, restarting it and it just could not bind to the same port. Fortunatelly we have solution, and we have the reason (i can not remember what is it, it was long time ago, getting old :))

  • nick (unregistered) in reply to Manni

    Good compiler would probably optimize the empty loops away anyway.

  • andrey (unregistered) in reply to toxik
    Anonymous:
    This is just perverse. And I like how he adds another cycle through incrementing x in the execute field of the for AND in the loop code increments z (whereas both could be done in the execute field of the for or in the looped code)

    It was probably because the compiler was smarter than the programmer and skipped over the 'dead' loops in a futile attempt to get the programmer to lookup the sleep() function. 
  • (cs) in reply to robymus
    Anonymous:

    Anonymous:
    WTF again! No OS takes "a pair of seconds" just to close a TCP port.

    Sorry for being not to specific on the terms, but.. after closing a socket, it will remain in (i don't remember what) state, and can not be reused again for a few seconds (os dependent), except with the SO_REUSEADDR socket option. Caused me a lot of trouble, when testing java server application, restarting it and it just could not bind to the same port. Fortunatelly we have solution, and we have the reason (i can not remember what is it, it was long time ago, getting old :))

    I have looked at "man netstat", and the state is probably TIME_WAIT: "The socket is waiting after close to handle packets still in the network."

    Other alternatives: CLOSE_WAIT "The remote end has shut down, waiting for the socket to close." CLOSING "Both sockets are shut down but we still don’t have all our data sent."

    I guess that I was closing the server while it was handling some or other connection (translation: someone was downloading a page from the JSP server), and that's why it wouldn't close inmediately. In an inactive server (no visits at that time of the day) it closes inmediately. Of course I'm not going to test this in the production server that happens to have visits constantly. You know, stopping the JSP server mid-visit on purpose just to see in what interesing ways it breaks :)

  • (cs) in reply to eddieboston
    eddieboston:
    7) The two busy loops that "wait for database to return data."  I mean, seriously, where do they teach this stuff?


    In the first programming book I ever had.

    Because many 8-bit computers did not have a BASIC command equivalent of sleep(1);, they recommended FOR I = 1 TO 1000: NEXT as a reasonable equivalent.

    Obviously, it was a rather damaging programming habit, as many who had done that on 8-bit machines were in for a surprise when they started programming PC stuff on a 386SX-16 and upgraded to a 486DX-33 :)

  • erlando (unregistered) in reply to AndrewVos
    AndrewVos:

    God. If I happen to read one more post containing the word goggles, or brillant, or brillance even, or first post,

    im going to KILL MYSELF.

    Die! you irritating F**KS[6]

    I need coffee.



    Brillant! The goggles did something! And I got first post about it!

    Seriously dude, lighten up. I hope the coffee helped..
  • (cs) in reply to Grimoire
    Grimoire:
    Don't forget the buffer over run, should the lengths of the input and output filenames exceed 980 (or so).  Why do so many C coders think that 1024 is "big enough" for a temporary string?


    I thought filename lengths were limited to 255 characters?

    Unless the filenames also include the path, shouldn't 1024 be enough?

  • (cs) in reply to dummy
    dummy:
    Grimoire:
    Don't forget the buffer over run, should the lengths of the input and output filenames exceed 980 (or so).  Why do so many C coders think that 1024 is "big enough" for a temporary string?


    I thought filename lengths were limited to 255 characters?

    Unless the filenames also include the path, shouldn't 1024 be enough?

    The filenames probably also included the path. And if this is a recent version of Windows, although there is a maximum filename+path limit of sorts, you can still create and access files with paths to the file longer than it (though a lot of software is upset by it, apparently).

  • (cs) in reply to AndrewVos
    AndrewVos:

    God. If I happen to read one more post containing the word goggles, or brillant, or brillance even, or first post,

    im going to KILL MYSELF.

    Die! you irritating F**KS[6]

    I need coffee.



    An asshat with brillant pair of goggles 8-| made the first post. Now please, kill yourself

    Sincerely,

    Irritating Fuck (or Magic Duck, your choise)

  • An apprentice (unregistered) in reply to Enric Naval
    Enric Naval:
    Anonymous:

    Sorry for being not to specific on the terms, but.. after closing a socket, it will remain in (i don't remember what) state, and can not be reused again for a few seconds (os dependent), except with the SO_REUSEADDR socket option. Caused me a lot of trouble, when testing java server application, restarting it and it just could not bind to the same port. Fortunatelly we have solution, and we have the reason (i can not remember what is it, it was long time ago, getting old :))

    I have looked at "man netstat", and the state is probably TIME_WAIT: "The socket is waiting after close to handle packets still in the network."

    Other alternatives: CLOSE_WAIT "The remote end has shut down, waiting for the socket to close." CLOSING "Both sockets are shut down but we still don’t have all our data sent."

    I guess that I was closing the server while it was handling some or other connection (translation: someone was downloading a page from the JSP server), and that's why it wouldn't close inmediately. In an inactive server (no visits at that time of the day) it closes inmediately. Of course I'm not going to test this in the production server that happens to have visits constantly. You know, stopping the JSP server mid-visit on purpose just to see in what interesing ways it breaks :)

    Well, what prevents you from writing a shell/Perl/Python/whatever script that would check the state of the socket first and then perhaps sleep() for a while before launching the JSP server?

  • (cs) in reply to An apprentice
    Anonymous:
    Enric Naval:
    Anonymous:

    Sorry for being not to specific on the terms, but.. after closing a socket, it will remain in (i don't remember what) state, and can not be reused again for a few seconds (os dependent), except with the SO_REUSEADDR socket option. Caused me a lot of trouble, when testing java server application, restarting it and it just could not bind to the same port. Fortunatelly we have solution, and we have the reason (i can not remember what is it, it was long time ago, getting old :))

    I have looked at "man netstat", and the state is probably TIME_WAIT: "The socket is waiting after close to handle packets still in the network."

    Other alternatives: CLOSE_WAIT "The remote end has shut down, waiting for the socket to close." CLOSING "Both sockets are shut down but we still don’t have all our data sent."

    I guess that I was closing the server while it was handling some or other connection (translation: someone was downloading a page from the JSP server), and that's why it wouldn't close inmediately. In an inactive server (no visits at that time of the day) it closes inmediately. Of course I'm not going to test this in the production server that happens to have visits constantly. You know, stopping the JSP server mid-visit on purpose just to see in what interesing ways it breaks :)

    Well, what prevents you from writing a shell/Perl/Python/whatever script that would check the state of the socket first and then perhaps sleep() for a while before launching the JSP server?

    Well, you're absolutely right, it would the best option. But I had a few imponderables ot the time:

    1. Not knowing Python/Perl
    2. Not wishing to invest time on that scheme, when I can simply write sleep(5) in the /etc/rc.d/init.d script, which is already, of course, written in bash, and get 99,99% success percentage.
    3. Being, at that time, relatively newbie to linux and to server administration in general (I knew about the sleep command just because it existed in C).

    If I already knew Perl or Python, I could probably write a script that did such a thing. Currently, with my lowish and fragmented level of bash, I could do at most a call to netstat and do a grep on its output. Basically, when the server is active, I get this on netstat:

    
    [root@webquest imatges]# netstat -atnp
    (lots of things)
    tcp        0      0 :::80                   :::*                    LISTEN      18128/java
    (more lots of things)
    

    So I can do something like this in the restarting script (not checked code, this won't probably work, just to get the idea):

    httpd stop;
    
    netstat -atnp | grep -e ":::80";
    
    while [[ $? == "0" ]]; do
      sleep(1);
      netstat -atnp | grep -e ":::80";
    done;
    httpd start;
    

    (notice how I have to execute netstat at two different places because bash does not have the do/while construct, so I have to use while/do. Either that, or GOTO. This is probably one of the few cases in programming where using GOTO is justified, which exemplifies how much what of a WTF bash becomes as soon as you attempt to do anything complicated. Using GOTO because of lacking do/while)

    Here I execute netstat with a few options to get to see only the TCP ports and get the details. Then I pipe the output to a "grep" command to grep for the port 80 line (yes, I could get false positives, I know, it's just a hack). Then I look at the exit code of the grep command ("0" for having found matching line(s), "1" for not finding any line, "130" for control+c). Basically, this loops until the netstat output doesn't contain teh string ":::80".

    Notice that sleep only lets me sleep in increments of one second. If I want to sleep less time, I could also run a loop like in the WTF, since bash will not optimize it :) Running the command continuosly is probably not an option, since it would take almost 100% CPU until killed if something went wrong.

    I guess that maybe Perl or Python have some mechanism to interrogate the system about the state of a certain port, like a network library, or maybe, calling a C function :)

    I could also hack the perl script that launches the server, altought this has the minor annoyance of having to update the script every time I upgrade the JSP server software, since the script is part of the software and it is distributed together.

  • the_JinX (unregistered) in reply to WWWWolf
    WWWWolf:

    In the first programming book I ever had.

    Because many 8-bit computers did not have a BASIC command equivalent of sleep(1);, they recommended FOR I = 1 TO 1000: NEXT as a reasonable equivalent.

    Obviously, it was a rather damaging programming habit, as many who had done that on 8-bit machines were in for a surprise when they started programming PC stuff on a 386SX-16 and upgraded to a 486DX-33 :)
    I remember doing an initialiser while time not oldtime + 1sec, add one..

    To calculate the length the for loop needed to be in a (quikbasic) dos game ..
  • (cs) in reply to the_JinX
    Anonymous:
    WWWWolf:

    In the first programming book I ever had.

    Because many 8-bit computers did not have a BASIC command equivalent of sleep(1);, they recommended FOR I = 1 TO 1000: NEXT as a reasonable equivalent.

    Obviously, it was a rather damaging programming habit, as many who had done that on 8-bit machines were in for a surprise when they started programming PC stuff on a 386SX-16 and upgraded to a 486DX-33 :)
    I remember doing an initialiser while time not oldtime + 1sec, add one..

    To calculate the length the for loop needed to be in a (quikbasic) dos game ..


    In the 90s, lots of old DOS programms crashed on new PCs because they were too fast... the programs try to calculate the length of the loop in that way, but eventually run into a division-by-zero error.
  • (cs) in reply to Enric Naval
    Enric Naval:
    1. Being, at that time, relatively newbie to linux and to server administration in general (I knew about the sleep command just because it existed in C).


    So, in this thread you've accused Linux of being a slow and unreliable OS, and sung the praises of Solaris - and only NOW do you admit that the problems you had with Linux were, in fact, not caused by anything to do with Linux, but with the fact that you yourself were too inexperienced to manage it properly.

    There's a saying about which workmen are most inclined to blame their tools.  How does it go again...
  • An apprentice (unregistered) in reply to ammoQ
    ammoQ:
    In the 90s, lots of old DOS programms crashed on new PCs because they were too fast... the programs try to calculate the length of the loop in that way, but eventually run into a division-by-zero error.

    Ahh yes, the infamous Borland Pascal Runtime Error 200... That was an implementation bug, as Pascal's 'delay' function (equivalent of C's 'sleep') was using exactly this kind of busy spin loop. But I can't think of any other way to do it (DOS timer interrupt had insufficient time resolution). And after all, in a single-threaded environment it wasn't that important to conserve processor's time.

  • (cs) in reply to the_JinX
    Anonymous:
    I remember doing an initialiser while time not oldtime + 1sec, add one..

    To calculate the length the for loop needed to be in a (quikbasic) dos game ..


    ...and did you, too, discover the funny things that happen that way - specifically, how you actually may end up with fractional seconds? :) I also remember fondly some of my first experiences with sleep() and usleep() (as in "never rely on those to sleep exactly as long as you told them to")...

    And guess why the DOS version of Ultima II doesn't work too well on a newish computer, let alone anything faster than a 286. You try to start the thing, it switches to 40-column mode, and you get "Division by Zero". (Not to even mention the fact that the most widespread PC versions these days have eff'd-up data files, but what else can you expect from Electronic Arts...)

  • nstlgc (unregistered)

    Quite off-topic, but I did LOL at "DNS issues being 'resolved'". Nice touch, Alex :-)

  • (cs) in reply to Iago
    Iago:
    Enric Naval:
    1. Being, at that time, relatively newbie to linux and to server administration in general (I knew about the sleep command just because it existed in C).


    So, in this thread you've accused Linux of being a slow and unreliable OS, and sung the praises of Solaris - and only NOW do you admit that the problems you had with Linux were, in fact, not caused by anything to do with Linux, but with the fact that you yourself were too inexperienced to manage it properly.

    There's a saying about which workmen are most inclined to blame their tools.  How does it go again...

    You are being a bit of a troll on your message.... Anyways... I find that having to know about TCP port states and perl/python only to RESTART A FRIGGIN' JSP SERVER withouth the server failing to restart at seemingly random times is a bit of a wtf.

    I'm now more experienced, and I still run into strange behaviours when going out of the already beaten paths. And experience just teached how to *avoid* those pits, not how to *solve* them. There is no current solution right now to my problem except sleeping until the OS frees the port. Either that, or edit the JSP server source code to change the way it opens the TCP port, and recompile...... which I find a teensy bit exaggerated for a minor problem, mind you.

    You're right, however, in that two years ago I wouldn't have dared doing that change on source code, and now I would. So you could say it is my fault. But really, this still feels to me like having to modify my car engine by hand. I don't mean repairing/replacing, I mean changing slightly the way a certain motor piece interacts with the rest of the motor. Computers should "just work".

    Notice that my university has a SPARC with SunOS, and that it is vastly more reliable than MY linux PC-based server, in spite of the SPARC being like a decade older. Distances are getting reduced with time and new advancements in linux and PCs, but still....

    About Linux being slow and unreliable.... Well, Solaris is starting to feel older and older when compared to linux, but it still excels in Sun hardware when compared to Linux PCs: reliability, number of processes, etc.

  • (cs) in reply to Iago
    Iago:
    Enric Naval:
    1. Being, at that time, relatively newbie to linux and to server administration in general (I knew about the sleep command just because it existed in C).


    So, in this thread you've accused Linux of being a slow and unreliable OS, and sung the praises of Solaris - and only NOW do you admit that the problems you had with Linux were, in fact, not caused by anything to do with Linux, but with the fact that you yourself were too inexperienced to manage it properly.

    There's a saying about which workmen are most inclined to blame their tools.  How does it go again...

    Oh, and notice that I had to restart the JSP server at night, because backuping the whole server using either "cp" or "tar" happened to lock both the HTTPD and the JSP server while copying "/var" and maybe "/usr", I never got to learn exactly which ones...... Linux is so nice and reliable for enterprise level........

  • Varizore (unregistered) in reply to makomk

    I know; I misread the post. Sorry. I vote for not having z at all as being the sane solution. (Of course, this is The Daily WTF - if everyone went for the sane solution, we'd have nothing to mock).

    It's probably there to ensure the compiler doesn't optimise the loop out.

  • (cs) in reply to .*
    Anonymous:


    WTF again! No OS takes "a pair of seconds" just to close a TCP port.


    If you're talking about a listening port, then this is a normal part of TCP. TCP specifies that a recently closed port must not be reused for some amount of time (it's basically more than the timeout for senders that might still be sending data) to avoid the obvious problem.  This state is called CLOSE_WAIT by netstat if you've seen that.
  • (cs) in reply to WWWWolf
    WWWWolf:
    eddieboston:
    7) The two busy loops that "wait for database to return data."  I mean, seriously, where do they teach this stuff?


    In the first programming book I ever had.

    Because many 8-bit computers did not have a BASIC command equivalent of sleep(1);, they recommended FOR I = 1 TO 1000: NEXT as a reasonable equivalent.

    Obviously, it was a rather damaging programming habit, as many who had done that on 8-bit machines were in for a surprise when they started programming PC stuff on a 386SX-16 and upgraded to a 486DX-33 :)




    This was also a reason why for a while computers came with a "Turbo" button.  (Really, it was a "slow down" button for such programs, of course).
  • (cs) in reply to Maurits
    Maurits:
    Otto:
    If a successful return code is zero, then success on both del's will also be zero. Anything else means a failure occurred, it just won't say where.


    Note if one returns +1 and one returns -1 you get a false success.


    Two problems with that notion:
    1. This being a DOS or Windows machine (see use of "del" command), the return code from a program (aka ERRORLEVEL) is a number from 0-255. It cannot be negative.
    2. Given that the same command is being used for both cases ("del" again), it seems highly unlikely that the command would have error return codes that would span from negative to positive to begin with. Generally speaking, programs tend to stick to one or the other, even in the unix world. This is not an absolute, I grant you, but it's usually the case.

  • robthebloke (unregistered) in reply to makomk
    makomk:
    As I've said before, I'm not sure why they're needed; system() waits for the command to exit before it returns, so unless the database client forks and returns (which would be a whole new level of WTF in itself) or something weird is going on...


    however if you appended & to the end of either filename, you can run the delete calls as a background process, and then system will return instantly..... 
  • (cs) in reply to Rank Amateur
    Rank Amateur:

    toxik:
    makomk:
    eddieboston:
    1) The whole premise of writing to a file to do an SQL query.

    2) Using one function to do three different tasks, and passing in an arbitrary, hard-coded ActionFlag to determine what to do.

    3) Can anyone say "memory leak"?

    4) The "C Cheat-Sheet" at the end.

    5) The response variable.  First of all, it's assigned to and never used.

    6) ...and second of all, is he really adding together two return values from the "system" function???  WTF?!

    7) The two busy loops that "wait for database to return data."  I mean, seriously, where do they teach this stuff?

    and finally...

    8) It's not in Visual Basic.

    Seriously, I could go on, but I need new goggles.

    9) Executing DEL using system() to delete a file

    10) The buffer overflow (which Grimoire mentioned)

    11) The failure to escape spaces in the filename... if this is UNIX, he'd probably be able to get away with it, though.

    12) The fact that it makes the Python/Perl programs that used popen() to run the command-line mysql client and execute commands look like a huge improvement.

    13) The use of strcat over sprintf (I know, I'm scraping the bottom of the barrel here...)

    I'm sure there are more...


    14) The name of the function. execSqlCommand - yet it can delete files which has nothing to do with the acctual SQL interfacing.

    15) Same loop to wait for the database to return data is executed for deleting files.

    16) "-i" <font color="#000000">is added with a separate strcat instead of being included in the original strcpy.</font>

    <font color="#000000">17) Same three lines of code in Case 1 and Case 2. Failure to exploit leaving a break; out in this rare case when it actually would've helped.</font>

    It's a classic Tufte WTF: the more you look at it, the more WTF it becomes.

    --RA



    This one is approaching the holy grail in terms of the WTF/line-of-code ratio: 1!  Almost every single line of code is a WTF!

    The only WTF I don't see is an SQL injection attack.  Of course, it could still contain one if the caller of the function isn't careful with those filenames.

  • David Morton (unregistered) in reply to Enric Naval
    Enric Naval:
    The first script comes back, OVERWRITES THE FILE with its own pid and proceeds to launch *another* JSP server.

    The real wtf is that you aren't using flock to create the pid file.

  • (cs) in reply to David Morton
    Anonymous:
    Enric Naval:
    The first script comes back, OVERWRITES THE FILE with its own pid and proceeds to launch *another* JSP server.
    The real wtf is that you aren't using flock to create the pid file.

    I didn't write that perl script, and flock is C function anyways. However, thanks for tip. I'll take that function into account if I ever need to lock a file.

  • (cs) in reply to WWWWolf
    WWWWolf:
    eddieboston:
    7) The two busy loops that "wait for database to return data."  I mean, seriously, where do they teach this stuff?


    In the first programming book I ever had.

    Because many 8-bit computers did not have a BASIC command equivalent of sleep(1);, they recommended FOR I = 1 TO 1000: NEXT as a reasonable equivalent.


    I'm surprised more WTF readers aren't aware of this.  An empty for loop was a very common method of creating a delay in BASIC.  BASIC was still in pretty common usage as of 20 years ago.

    The real WTF, which I guess is the very definition of a WTF, is that this aberration is in professional code.  Code someone was paid to write.

    I wish I could have joined the company for the boss's salary back when I was 14, because that's when I was writing code of this caliber.

Leave a comment on “C SQL. C SQL Run. Run SQL Run.”

Log In or post as a guest

Replying to comment #:

« Return to Article