- 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
The return value of sprintf is the number of characters written...
Admin
bonus points? that depends.... is it because we're assuming that "N" is indeed an integer value? :D
(Given some of the horrid programming we see on this site, it wouldn't at all surprise me to see someone trying to use a char value as an array size.)
FRIST!
Admin
I believe that should read "sprintf"
Admin
Why putting in the \n\r in the first place? tsktsktsk, and what about buffer overflow?
captcha: stinky - like a kinky malinki
Admin
I think it shows a complete and utter lack of understanding of the sprintf and its syntax... And for the bonus points: of course the length can be read from N. But it isn't needed in the first place.
Admin
You'll get bonus points if you can figure out why I think strlen is redundant.
I know! sprintf returns the number or characters printed, so it could be simplified to:
(Just kidding...)
Admin
Admin
I think the issue is that he is manually attaching the carriage return, then stripping it out right after.
Hence, One Step Forward... (One Step Back).
Admin
The comment says remove the carriage return but it really removes the newline.
Also that thing with them being put in by the previous line.
Admin
And of course the comment says "remove carrigage return", while the charater at n-2 is the LINE FEED (\n). Carriage return is at n-1.
Admin
buffer[len-2] = 0;
What is that attempting to do??? Replace one element of the array with the number 0? Doesn't it just leave the rest of the array alone (which would be the rest of the carriage return line feed)?
And shouldn't this be done with a 2D array? Maybe it has something to do with saving space in an embedded system, but if someone names a key or value with a ":" you appear to be screwed.
I think I got herpes just from reading this. IT BURNS!
Admin
The actual issue is that the \r returns to the beginning of the string before writing the final 0 - which means that strlen will always return 0. So the code writes an effectively empty string, then corrupts the memory just before it.
Admin
Really this should be:
bounds checking is good
Admin
I think the 0 was trying to move the null-termination forward. But it's still not null, it's just the integer value 0. Null should be \0.
And the strlen is extraneous because the initial string is a null-terminated string, so it'll always return N as the size.
I think. It's been a while since I've done C.
Admin
Plus, it's \r\n, not \n\r.
(On the other side, i've lately seen embedded code which had a /n/r in place. Sometimes i wonder how these devices can work at all.)
Admin
This word, null....i do not think it means what you think it means.
Admin
According to http://www.cppreference.com/stdio/sprintf.html sprintf returns the number of characters read. (How could it know the total size of the buffer?)
Oh, and snprintf isn't part of the standard C library, as far as I can tell. Many embedded devices are restricted to ANSI C (or less).
Admin
No assigning zero is correct. Actually '\0' == (char) 0 is true.
Admin
I know! It's not i18n compliant; the code should be using wchar_t and wsprintf.
Admin
Hmm, there are a few wtf, but all minor imho:
A few responses of people (some mentioned before):
So a bit of a wtf, but not a great one IMHO
Admin
Hmmm I just posted a reply in which I stated it was nearly always true. But you're right. In fact, if you look at code you may assume to be valid, you often run accross something like this: while(buf[x++] = buf2[y++]); or: if(*buf)
Which would, of course, only work if the null-terminator is actually 0.
Admin
Ok - i'm scared... comments are "The Real WTF":
120377: "is it because we're assuming that "N" is indeed an integer value? :D" 120381: "of course the length can be read from N. But it isn't needed in the first place." 120388: "Replace one element of the array with the number 0? Doesn't it just leave the rest of the array alone (which would be the rest of the carriage return line feed)? And shouldn't this be done with a 2D array?" 120389: "The actual issue is that the \r returns to the beginning of the string before writing the final 0 - which means that strlen will always return 0. So the code writes an effectively empty string, then corrupts the memory just before it." 120392: "But it's still not null, it's just the integer value 0. Null should be \0. And the strlen is extraneous because the initial string is a null-terminated string, so it'll always return N as the size"
I'm sorry - you all fail :) strlen counts chars up to '\0' (==0) and doesn't care about "\r\n" and will return value, which is already known from sprintf() What's wrong with you guys today? (V-day hit hard, or sth?)
Addendum (2007-02-14 09:52):
Admin
This website is entertaining, funny to read.
The comments often leave me unsure whether to laugh or scream loudly and move to a little cabin in the woods, staying well away from the IT business for ever.
There's always an early poster who gets it, but his post is ignored, and followed by a long thread, where at least 9 out of 10 posts are of the form:
<insert insult> The (actual issue|real WTF) is: <insert utter nonsense>I don't think I'll be reading the comments on this site again
Admin
sprinf(buffer, "%s: %s\n\r",header,value);
The real wtf is that he should use sprintf
Admin
People,, the WTF is that at the line
sprinf(buffer, "%s: %s\n\r",header,value);
He WRITES a CR, then removes it. If he didn't want the CR, the line should be
sprinf(buffer, "%s: %s\n",header,value);
Mind that he removes, the character before the last, wich is the \r, NOT the \n.
No, bound checking is unnecessary if you know, FOR SURE, that you won't overstep the string.
akatherder, yes, he's doing exacly that. You see, the 0, or \0, is the character that marks the end of the string. A LOT of C functions will operate the string untill they find a \0. strlen, for example, will count the number of characters until it finds a \0, thus returning the number of characters in the string. Mind that the \0 IS part of the string and should "fit" inside the buffer.
Admin
Admin
Admin
Admin
WTF is a "sprinf" ?
Admin
Seriously, could people actually be as illiterate (incompetent) as the spelling (comments) on this forum would suggest? (God, I hope not shudders )
Admin
Are you on something?
Admin
Hmmmn I always thought it was CR LF... 0d0a...
Admin
WTF would we do to handle a language that someone designed to print diagonally on the page? Wheee!
Admin
Yes. I'm on having been too distracted after a long day at work to think properly and also on having tested this the wrong way before posting.
Admin
...I mean, it's a joke! A joke!
Admin
What on earth are you talking about? 2D array what, huh? And the reason for putting in the "number 0" is effectively null terminating the c string. A perfectly valid thing to do in many cases.
Sigh. Reading some of these comments is even more painful than the original code.
CAPTCHA: burned, just like my pee. :-(
Admin
Bomb the country where the language originated into submission.
Admin
Hmm shouldn't the OS handle that? Oh wait it's embedded..
Admin
Hmm shouldn't the OS handle that? Oh wait it's embedded..
Admin
This should do, no need for the "-1":
From the snprintf manpage:
Admin
Damn. My sarcasm detector is not working properly today. I can't tell if you were serious or not.
If you were serious, that was really funny. But if that was supposed to be a joke, it was so absurd that it wasn't funny. 8)
Admin
This could be written as one line:
Admin
At first I thought the code was retarded and it would just replace the \r with a "0". That's pretty clever, although my lack of C knowledge probably makes it seem more impressive than it is. At any rate it's handy to know.
Admin
What's even funnier is having links like the one below on this site:
http://thedailywtf.com/Articles/One_Step_Forward_0x2e__0x2e__0x2e_.aspx
Admin
Although in this case, I think it is possible (especially in embedded systems) that sprintf does not obey the standard. Used to be all sorts of different return values from the printf functions from different libraries. In fact, as recently as MS's VC++ 6 for the *nprintf() functions, returned -1 if the length was exceeded, while GNU libc returned the length that would've been printed. I haven't used VC 7, I don't know about that. So really, the only wtf here is putting in \n\r (backwards) and then removing it. Not being skeptical of the portability of the return value by using strlen().
Admin
When the char array is allocated: char buffer[N]; it is not initialized, so you have N characters in the array but they are not necessarily set to '\0'.
So calling strlen(buffer) will count from buffer[0] until the first '\0' encountered, which may be well after buffer[N].
Not only is calling strlen() redundant, but it's downright dangerous here.
Admin
The real WTF is that someone gave these clowns a C-99 compiler, while my platform still doesn't have one.
Admin
I thought strlen only stopped 'counting' when it found a '\0' .. would have to see how that works ... I guess he may have initialized the entire array with '\0' but then I'd have to wonder if that funcion is reusable.
To sum up, I'd have used strtok instead if I were to remove the first ocurrence of '\n'. But maybe I would have done everything in a different way.
Admin
Return value Upon successful return, these functions return the number of characters printed (not including the trailing ’\0’ used to end output to strings). The functions snprintf() and vsnprintf() do not write more than size bytes (including the trailing ’\0’). If the output was truncated due to this limit then the return value is the number of characters (not including the trailing ’\0’) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. (See also below under NOTES.) If an output error is encountered, a negative value is returned
Admin
The real WTF is the use of printf, which must be interpreted at runtime, rather than using a manually-coded "format." Even better would have been if the buffers had been made physically proximate, so that there would be no need to concatenate them.