by chetanahuja · 12y ago · 0 comments
[Now I wait for a deluge or replies about how it's "idiomatic" C. All I have to say to that is, s/ma// ]
--- libc-a/memset.c
+++ libc-b/memset.c
@@ -1,6 +1,6 @@
void *memset(void *_p, unsigned v, unsigned count)
{
unsigned char *p = _p;
- while(count-- > 0) *p++ = 0;
+ while(count-- > 0) *p++ = v;
return _p;
}
[Edited later to use the code mode]I assume you wanted your code to look like this:
void *memset(void *_p, unsigned v, unsigned count) {
unsigned char *p = _p;
- while(count-- > 0) *p++ = 0;
+ while(count-- > 0) *p++ = v;
return _p;
}
In which case, I'm not sure what you're complaining about. Is it that memset here has a non-standard signature (v is supposed to be int, not unsigned)? Or that the "before" code ignored v? Or the un-braced loop body on the same line? Or is the some other problem I'm overlooking (which is possible, it's been about a decade since C was my main day-to-day language)?[Edited to try to coax better text formatting from HN]
Oh I know, the writer obviously wants very very badly to write this while loop as a one-liner. That's the start of all the other badness. As a reader of this code now, I have to keep every possible combination of post-increment side effects and precedence rules in mind before this code makes sense.
And yes, I know code like this is quoted in books as example of how cleverly you can write "powerful" one-liners. As far as I'm concerned, it's just a dumb show of alpha-coder pride and it's not a surprise to me that a serious bug was buried here.
Finally, the same people who write that kind of code also write things like f(x++, (x+1)) without realizing that this is undefined in C.
Also, as an added bonus, postincrement/decrement sometimes creates extra unwanted copies in C++ so you need to use the pre- versions.
Compilers are good enough to handle tight loops like this even on very low optimization settings. Better to write that stuff out.
I have found that as I program more, I kind of agree with Crockford that ++, and -- are evil and should always be replaced by pp += 1; What that does is force you to avoid actions in conditional expressions and pull the mutators inside the block. It also forces you to be explicit about when you expect the increments to happen.
Those are fundamental things that should be second-nature to you if you claim to really know C, just as a mathematician would know that exponentiation has higher precedence than multiplication which has higher precedence than addition.
> Did p get incremented or did p get incremented?
There's an easy and concise mnemonic for the precedence of ++/-- and dereference - just remember that this is a strcpy, as shown in K&R:
while(*dst++ = *src++)
;
So here it is the pointer that is incremented, and not what it points to.> Finally, the same people who write that kind of code also write things like f(x++, (x+1)) without realizing that this is undefined in C.
Those are the people who think they know C, but actually don't; it's an issue of not knowing what you don't know, and in such situations you should either seek to find the answer, or be conservative.
That's a bogus argument. There's an easy way to write code that's readable and maintainable by mixed teams and then there's this macho way of showing off to no particular advantage.
- just remember that this is a strcpy, as shown in K&R: while(dst++ = src++) ;
Or -- just don't. Just write code that doesn't rely on every reader to mentally clear through a dense thicket of precedence and side-effects to understand your strcpy and get your job done in a pragmatic fashion with 4 lines of easily understood C code instead of 1 line that makes your heart glow with momentary pride.
NEVER WRITE THIS CODE! EVER!
This is the code that gives us the classic buffer overflow vulnerability all because checking for a length isn't elegant.
Also, how does this code work if what one of those pointers points to are declared volatile? What if both pointers point to volatile things? What happens if one of those pointers crosses through 0 because it rolled over from max?
Thanks. I doubt you could have provided a better example of the precise problem.
Questions like this and others (eg., does the pointer deref operation applies to incremented or non-incremented value of p?) that you have to answer before you can make sense of the code. The cognitive overload on the reader's brain is at least part of the reason that a bug like that went past code-review in memset function on a major platform. MEMSET !! Was the needless cleverness and terseness of the code worth it?
P.S. The answer to your question is No. The variable count as used in the body of this function is local to this function (it's passed by value... so it's a copy of what the caller passed).
You have an interesting take on C.
And I don't see what's 'clever' about decrementing a count instead of incrementing a count.
It would be just as easy to assign 0 in a larger function, and I see no way of reducing the cognitive load. This line isn't doing anything complex or weird.
Can't tell if this question is referring to something I said? If so, I'd like to know what I said that implied such a thing.
C++ added pass by reference (&), which can be a handy bit of syntactic sugar.
What you're saying is correct in the practical sense. But ggreer's answer is the real technically detailed explanation (everything is passed by value, including pointers to memory. But then you can manipulate that memory you have the pointer to. And that makes it effectively pass-by-reference). Also, C++ adds a language defined pass-by-reference operation... so there's that.