- 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
Hmm
#include <string.h>
void addhttp( char *url ) { char http[8] - "http://" (somehow find if it has http or not. I'm not sure how now.) url = strcat( http, url );//not sure if I got this right. :P }
Yeah captcha got it right with ewww...
Admin
You win, thread over.
Admin
I'm surprised he didn't reverse the slashes.
Admin
!gfmo, shurely?
Admin
!ftw
Admin
woW
Hmm, writing that backwards wasn't so effective...
Admin
Admin
(I wonder if this was the Geoff Lane I know...)
Captcha: alarm (yes, it is alarming!)
Admin
this is another one who can't read documentation. What's the problem with this
private static const String PROTOCOL = "http://"; static public string PrependProtocol(string urlString){ return PROTOCOL + urlString; // although to be 100% correct // return (urlString.toLowerCase().startsWith(PROTOCOL) ? urlString: // PROTOCOL + urlString); }
Admin
Admin
Perl hackers have a name for this sort of thing: TMTOWTDI
Admin
!FTW .gnimmargorp denrael eh erehw rednow I .gnirts deirw a saw tahT .mih no serifkcab ti epoh I
.sdrawkcab & detrevni si tsop elohw sihT
Admin
and now I know that "What the F---" (WTF) backwards is "For the Win" (FTW). No wonder!
Admin
I guess some people's brains are hardwired to think in Polish Notation.
Admin
Please don't ever try to write C again.
Admin
Fantatsic - o!
Google doesn't bomb out when searching for that function name, maybe you found a GWTF and they corrected it promptly?
CAPTCHA: tacos because everyone knows I'm SO Cat.
Admin
Whoopsie, was thinking of Java.
Admin
Well, it is quite enterprisey. Just needs more abstraction levels and XML.
Admin
Hmm... I haven't seen that. Which C# compiler are you talking about? (IIRC, the spec doesn't specify how the values will be concatenated, so a compiler could use StringBuilder if it wanted to, but I haven't seen it.)
Try this:
class Test { static void Main() { string a = "a"; string b = "b"; string c = "c"; string d = "d"; string e = "e"; string f = "f"; string g = "g"; string h = "h"; string i = "i"; string j = "j"; string k = "k"; string l = "l";
}
Using the MS C# compiler (1 and 2) that uses String.Concat.
Jon
Admin
How can someone use a word like "prepend" and write such code? Maybe exhausted from searching the dictionary...
Admin
StringBuilder? StringBuffer.
Admin
Not as of Java 1.5. StringBuilder is the unsynchronized version of StringBuffer, basically, and is used (at least by Sun's compiler) for string concatenation. About time, too - has anyone ever used a StringBuffer in multiple threads?
Jon
Admin
This was one of the best replies ever.
Btw I think this is a brilliant WTF...kinda reminds me of the good old IsTrue days
Admin
it's something sexual
Admin
Strings and C are notoriously tricky, so your mistakes are easy to understand.
You don't check that url pointer points to large enough a buffer. Thus blindly adding characters to it may cause buffer overflows. strcat() won't allow you to use the same variables as both source and destination. It doesn't do any bounds check either.
Admin
Admin
WTF? I was able to read all that reversed text without any trouble at all? Sign of a backwards brain? Slhoud I wrory?
-L
Admin
Agreed, though most people make them harder than they should be. All that's necessary is to read the standard and, as Dan Pop was fond of saying, engage your brain. If you're not willing to do those to things, stay the hell away from C.
Since nothing gets appended to the object pointed to by "url", that hardly matters.
strcat always uses the same object as its first source operand and its destination. Using objects that overlap for the two source operands produces undefined behavior, but this code manages to avoid that particular error.
That's obvious, if you understand C, but correct. So, hey, you get one out of three.
Here's what's actually wrong with the posted code:
void addhttp( char *url )
The object pointed to by "url" is not modified; it should be const-qualified. Of course, since the function ought to have side effects, either the object pointed to by url should be modified, or the function's return type should be "char *" and it should allocate a new string.
If the function is intended to modify the object pointed to by url, some might also want the function to take a size_t parameter containing the size of that object, so the function would know whether there's room for the result. Of course the resulting string is always a fixed amount larger than the input, so the function could just require that the caller supply a sufficiently large buffer. I take no position on the matter in this case, since I wouldn't implement this functionality in this manner in the first place.
char http[8] - "http://"
Minus instead of equals. No trailing semicolon. Unnecessary size specification on the "http" array, since it's the same size as the initializer.
url = strcat( http, url );//not sure if I got this right.
If url is null, you've just invoked undefined behavior. If the string pointed to by url has nonzero length, you've just overflowed the http object, and invoked undefined behavior. Assigning to url only changes the value of the pointer in this function; it has no side effects, since the function then returns.
(We'll assume this is C99, or a C++ implementation being used as a halfassed C implementation, and allow the // comment delimiter.)
But #include <string.h> was right, as were the lines with the curly braces, so the defect rate, measured by SLOC, was only around 50%.
Admin
...ti ot em taeb uoy tub ...tsrif sdrawkcab dna detrevni gnitsop tuoba gnikniht no flesym etalutargnoc ot tuoba tsuj saw I
erus ton m'I ...ymmuy :ahctpac
Admin
"This is either madness... or brilliance!"
("Remarkable how often those two traits coincide.")
Admin
Admin
Don't you mean "Brillant!" ?
(sorry about the double post, I had a brillant moment myself!)
Chris
Admin
(: .em rof gnipyt-esrever eht od ot 'cat' gnisu ot gnitroser m'I .ylkciuq sa stsop hcus epyt t'nac I taht si FTW rehto ehT
D: !stsop lamron sa tsaf sa tsomla meht daer I .sdia yna tuohtiw yltcerid stsop eseht daer nac I !FTW
Admin
!!!LFOR
daehkcid a tahw
Admin
?etisoppo eht siht sI. 'tac' fo draeh ev'I ?'cat' s'tahW
Admin
Incredible code. Really negative iq :)
Admin
This article reminded me of this so much...
Admin
my head just caved in
Admin
oh i get it! the browsers add http for you. that's really funny that he didn't need to do that catnation
Admin
That was my second impression, and I strongly believe in it.
Admin
I Laaaauuuugggghed!
Admin
Gross. Totally un-greppable
Admin
"When your only tool is a hammer, everything looks like a nail."
Well, at least he got the job done. Granted, there should have been some checks to see it the string already had "http://", but if it works...
Admin
Was Brian an old asm coder? Then it is right, but wrong in java.
Admin
Being to lazy to learn any kind of code, and too cheap to pay for someone to do it for me, I think I know what happened. When I borrow (steal) someone else's code I often make it do things it was never meant to do. So you find code that maybe appends one letter to the front of something, you really don't know how it works, or why, and try to make it append many letters. It comes out backwards, no problem, I'll just reverce my input. I still don't know what I am doing, but it works.
AJU,I'm not a coder, but I'm related to one.
Admin
//return (urlString.reverse() + "//:ptth").reverse();
even better, (urlString.reverse() + "http://".reverse()).reverse();
of course you can swap arbitrary blocks without copying this way, i.e. reverse a, then reverse b, then reverse ab. Someday some weenie is going to ask about it on a "programming quiz" for a job application, so it's a good one to know.
Admin
Hey take it easy -- we're in 2007 but I guess some of us still get paid by the number of lines of code you write!!
Admin
Come on guys - this is obviously done to waste time or something. A guy who knows about enumerators, padding, etc., will certainally know concat.
Admin
and your name is "brian"! cooincidence ?
Admin
No, you should spellcheck ...