- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Finally! Thank you for posting code again!
Admin
As long as this is Java, then _inputStream.read() will return -1 when it reaches the end of the stream, so it will be possible for _char == -1 to be true.
Also, catching an OutOfMemoryError is extremely bad practice, as is catching anything derived from Error...
JDK5.0 API - java.lang.Error
Because OOME is thrown when the JVM is out of memory (suprise suprise...) the JVM cannot handle the catch case and just exits.
Admin
I think the problem is that (char)-1 is *not* -1 as char is an unsigned type.
The catch(OutOfMemoryError) actually made me go 'WTF?' for real
Admin
Ah, too true - Friday afternoon and my mind is no longer here ;-)
Admin
>I think the problem is that (char)-1 is *not* -1 as char is an unsigned type.
Well,Java does not have unsigned data types, o that part is OK.
The problen is that stream.read() actually returns an int, in which case last 8 bits will technically have 'char' value; (int)-1 will signify failure or EOF. However, in this case by assigning the value directly to char type, there's no way to distinguish between (unsigned char)255 -> valid value and (int)-1-> EOF
Admin
What is this trying to do? Read as much data from _inputStream as it will offer, stripping off up to two CR+LF's from the front? Or any combination of CR+LF's, and the inner pair of if() blocks are completely redundant? (I figure I could work it out by poking around the net, but why do that when someone who does Java for a living can prolly figure it out in five seconds)
Admin
Parsing HTTP headers, I suspect.
Admin
I suppose they didnt realise the reason they were running out of memory in the first place....
while (true) {
char _char;
Now unless java has some wierdness - im pretty sure thats allocating a new block of memory each loop?
Admin
Java does have one unsigned datatype, namely char: its range is 0 to 0xFFFF.
So char == -1 will in fact return true if char is 0xFFFF.
Admin
No, that will create a char on the stack, but it will be reused every loop (or recreated in the same place on teh stack).
This part will allocate a new byte on the heap though, which if it wasnt for Javas magic garbage collection, would leak a lot of memory (being a C++ coder, this line made me go "WTF!")
_inputStream.read(
new byte[_inputStream.available()]);
however, the Java spec states that the garbage collecter *is not guarenteed to run at any time during your programs execution*! So depending on the platform this is running on, those bytes may not get cleaned up till the program quits.
Admin
LOL
I write much better code when I'm smoking hashish, people underestimate its ability to help solve complex problem...
Admin
Cannot believe the HTTP client company is so lazy that they did not even strip out their comments in their code. That's a serious business (not technical) mistaken.
Admin
Ow. My head.
Admin
No, it won't. I just checked.
Java only does four types of math: int, long, float, and double. byte, short, and char all get promoted to an int whenever any math is done with them (including comparisons). I suppose you could argue that boolean operations count as math so that's a fifth type.
In any case, all math operations involving bytes, shorts, and chars are done with ints. So the following code:
public class test {
public static void main(String[] args) {
char c = (char) -1;
System.out.println(c == -1);
System.out.println(-1 == '\uFFFF');
}
}
Prints:
false
false
I just tested it. And, yes, it's really annoying that Java has a policy of "no unsigned datatypes" and then treats chars as essentially unsigned shorts.
Admin
Actually, that's not quite true: OOME is thrown when there is not enough memory to honour a request to allocate more. Depending on what you are doing, this may be recoverable (e.g. if you're allocating a 128MB block).
Admin
In the case of the Sun JVM, objects won't become candidates for GC inside a method - so allocating objects in a long-running loop in a single method is likely to cause you problems.
We've resolved GC issues in long-running loops (e.g. numerical analysis) by extracting methods out.
Admin
however, the Java spec states that the garbage collecter *is not guarenteed to run at any time during your programs execution*! So depending on the platform this is running on, those bytes may not get cleaned up till the program quits.
On mobile phones it typically runs GC when memory is about to run out. I did a test once, and was like WTF, when showing available memory and it was continually shrinking. I was curious what will happen, when it runs out, but it just ran GC and freed all unused memory.
Admin
It does not. It is a variable local to the loop, same stack slot will be used at each loop.
Admin
The real WTF is that if this is indeed Java Code, that they payed a "huge" amount for a closed source HTTPClient - i dont know if that was like 10 years ago because the jakarta http commons util exists for quiet some time (years)....
Admin
Being serious about this stripping comments? WTF.
Admin
The real WTF is that there were two WTFs on the same day!
Admin
And one disappeared for a while the other day.... Some WTFery is going on at The Daily WTF...
Admin
Maybe I get your remark wrong, but a while loop generating a stack?
That's nonsense, the assembly equivalent of a while loop would be a conditional jump, that doesn't create a call on the stack.
Admin
I think the problem is when a read() return -1, it is not detected by the if() (because of the conversion to char), then it sits in the while(true) loop, appending one char (with value 0xFFFF) to the StringBuffer at each iteration until there is no more memory and the processor emits smoke.
They could just read the whole buffer (since they allocate it at the end any way), then search for "\r\n\r\n" in-memory (much more simple to do) and then build a StringBuffer from the first part of byte[] buffer.
Admin
As a novice at programming it's no surprise I don't understand everything that is said on here, but what I find very confusing is all the different answers and explainations. A little help please.
Admin
You're thinking the same thing, just saying it differently. The variable is local to the block, you can't access it outside the scope of the while block. As far as the java language cares, it only exists inside the while block, and the specifications require it to be initialized to zero at the point of declaration.
Now the compiler probably does NOT actually allocate new stack space of it every time it enters the loop, but instead optimizes it by allocating stack space for all variables on function entry. Now all it has to do is zero the value at the beginning of the loop.
Strictly speaking in compiler theory though, you only need to put the variable on the stack inside of the loop. It's the optimizer that will move it back outside :)
Admin
Anyway, what was this http client doing on server ? This CodeSOD smells like a J2ME...
Admin
The OOME in this case really tells the whole story, I'm pretty sure of what's happening here without even trying to understand the conditional logic in there. Something similar has actually happened to me -- except I fixed the loop rather than catching the OOME :)
The string buffer has an internal buffer that is being added to with each pass of the loop. When the exit condition for a loop is wrong and the loop runs forever adding one char every time this will very quickly allocate all available memory to the string buffer.
Just think: A processor with a memory bandwidth of gigabytes per second and a computer with maybe a single gigabyte of ram.
StringBuffer sb = new StringBuffer();
while(true) { sb.appendChar('x'); }
will run out of memory within seconds, especially if the VM size is restricted.
Admin
Fewww... when I start reading, and got to the comments, I was sure that someone had posted MY code. I have put both
Admin
'bout time somebody posts a non-WTF version of this code.
People just gotta love this Paula HTTP Client. I can imagine management decision.. "WHAT? 100 % CPU usage? We need better hardware!"
Admin
*Ouch*... My brain... :)
Seriously, the little side note:
Sounds a bit like me, if I do not sound too high on myself... :) My biggest problem is not that I need to know how something works, but I expect other developers to as well. My bad... :)
Peace!
Admin
Well maybe you're a computer (did you pass the turing test already) but the phrase :
Sounds like a load of bollocks to me...
Unless you're a computer compiling shit won't help you understand it.
I precompile it in my head to figure it out. WTF!
Admin
Actually, in Java, local variables must be explicitly assigned to (Java Language Specification, chapter 16) before they are read and the compiler must be able to prove it or it refuses to compile.
Admin
OK - maybe not "compile" in the sense of doing translation to opcodes... My fault for copying without editing. But there were smileys present, and I did say "a bit like me"...
But along those lines, I am sure that you can get an idea of what happens when you see code like
- You can see a possible bug simply by knowing how the code will (or wil not) execute depending on the value of bValue.
Hey - I once debugged something by having a hex dump of a memory area read to me over the phone, which was kinda like reverse-compiling... :)
Peace!
--------------------------------------------------
Just because you do not know what I am talking about, that does not mean that I do not.
Admin
Actually, the specification only requires fields to be initialized to zero; a local variable will not be automatically initialized. If it is used before it is initialized, you will get a 'the local variable var may not have been initialized' compiler error.
Admin
And the semantics of the or operator (remember VB6).
duh!
Admin
<font color="#009933">> And the semantics of the or operator (remember VB6).</font>
Ahhh... But I do not need to worry about that, nor how it works in the many other languages that I do not currently have a need for. It is but one of the many benefits of being a highly-paid specialist. I am sure that one day, you will understand.
<font color="#660000">>> Hey - I once debugged something by having a hex dump of a
>> memory area read to me over the phone, which was kinda like
>> reverse-compiling... :)
</font><font color="#009933">> duh!</font>
Easy, kid... I said "kinda like" and there is no information in my post that clarifies what kind of memory I was looking at. So jumping to a conclusion would be inappropriate. ...Just like taking an inappropriate tone with those whom you do not know.
Admin
Maybe you're one of those selfnamed (highly-paid) specialists the ends up (with his atrocities ) on theDailywtf?
Till the cold freemarket wind sweeps across the plain, and its back to shoveling dung.
Admin
Well it can help. I do it a bit with Java. Having written a JVM from scratch in the past, I know vaguely what it does behind the scenes and what the various opcodes are and when they are generated. However, that still doesn't really satisfy me properly because the implementations of various JVMs are all different and you just have to trust that it's doing something sensible and not be too much of a control freak about it.
When I first encountered interpreted and compiled-but-virtual languages, I didn't like them because I was a further step removed from the processor and felt uncomfortable not being able to control the processor properly. But those sorts of things crop up in other places too. I was used to programming in C and it is relatively easy to know how that is going to compile. Use C++ though, and the preprocessor is doing loads behind your back and bloating your code for you.
I thought about it and realised that if I wanted to be a complete control freak then I would really have to program in assembler because even in plain C, the optimiser can be doing strange things behind your back. Useful, but still strange. And also, even in assembler, you need to know your CPU inside-out to know what it's doing because CPUs contain various levels of microcode and nanocode for various instructions.
So knowing how your language is compiled can give you hints as to how efficient your algorithms will be and how much memory they will consume. A lot of it is still up to various lower-level factors over which we have less control so keep that in mind. Caring about it, however, is a significant step towards being a good programmer.
Admin
Gasp! A highly paid specialist that doesn't know jack... why am I not surprised??
captcha: null (maybe if I was a highly paid specialist then I would know what this means)
Admin
> Maybe you're one of those selfnamed (highly-paid) specialists
> the ends up (with his atrocities ) on theDailywtf?
I can honestly say that no, I have never seen my code on TDWTF...! I have not even seen code close to it. While I have written some (really) bad code in my younger days (some of it easily WTF-worthy!) and even being a college dropout, I do not think that "atrocity" applied since I had a VIC-20! :)
As far as the freemarket wind, remember there are companies out there that are competent enough to recognize experience and wisdom, and they will pay for it, even when the market sucks. Trust in that when things start to look bad - there is always a position for a skilled (or better skilled) developer. The last think a company needs when the software market sucks is to put out a poor(er)-quality product, which will only cause them to lose further sales. That is the best time for them to spend money wisely.
Peace!
Admin
> Gasp! A highly paid specialist that doesn't know jack... why am I not surprised??
Heh... One might wonder just exactly how you would be aware of my body of knowledge (or not). Not to point too fine a point on it, but I did not say that I did not know VB6 (or any other languages for that matter), I said that I do not have to worry about the ones that I do not currently have a need for.
> captcha: null (maybe if I was a highly paid specialist then I would know what this means)
Perhaps... One can hope... :) J/K
Peace!
Admin
i'm sorry, but i don't like the whole new "code snippet of the day" idea. to me, the daily wtf is code like this, and while i like a few stories mixed in, we've had nothing but stories. code is what i think should be the main diet of the daily wtf, like it used to be.
Admin
Um, all he said was that he didn't need to know the semantics of the VB6 or operator. He didn't actually say he didn't know it... just that it was unnecessary since he doesn't work with VB6.
Er, wait, I mean... He suggested people who make lots of money don't write VB6! He is TEH STUPID ELITIST!1 BURN HIMMMMM
Admin
Agreed, love the code, this is why I visit this place!
Admin
Well, I think the community here is quite heterogeneous with respect to knowledge on coding, and on IT in general. Which is natural. No one has a full grasp of the unbounded ocean of possible IT knowledge anymore... and, of course, different life histories... As for your request for help: I think you should try to be a little bit more specific. Whence could we know what you know or not?
When it comes down to discussions on code snippets in programming languages I don't know (or don't know well) I also often don't understand the comments. But WTF? Sometimes I learn something from them, nevertheless. And otherwise I just skip them.
Admin
So... Rather than getting a story per day which may or may not have code in it, we will be getting two stories per day, one of which is guaranteed to have code. How is this not an improvement?
Admin
It never ceases to amaze me when people, presented with free entertainment, feel the obligation to criticize and complain because it's not exactly what they were looking for.
Personally, I like Alex' writing - I usually get a laugh-of-the-day out of the story. The code, if present, is just an added bonus.
Cable has hundreds of channels - most with nothing of value to watch, but there is only one TDWTF!
Admin
I have been lurking around here for several months only and don't know how TDWTF formerly was. For my taste, a good mix of stories w/o code, stories with code, WTF code snippet collections and WTF collections of codeless stuff is preferable. The problem with pure code WTFs is that often only a (rather small?) subset of people here would benefit from them. Or does anyone here know well all the programming languages that frequently occured on TDWTF so far? Whereas I daresay that everyone can profit from reading about management WTFs. And the subtitle of TDWTF is "Curious Perversions in Information Technology", after all. For me, that does include not only code, but also people, hardware, management, even politics.
Nevertheless, I admit that the assortment during the last weeks may have been a bit onesided.
Admin
OOME is much worse than a failed call to malloc. The VM spec states that after an error is thrown everything may be broken and all guarantees the lang and VM gave you may be long gone. In Azureus they do catch and report errors, probably with a catch(Throwable) line: I got an OOME and after that every action I tried to do involving a torrent threw an AbstractMethodError because the VM invalidate part of the class. Never catch an Error in Java.