3 ms·
No, you are right. The copying is a bad bug. Using sizeof(char) is mostly a coding style question. The casting of the malloc() return value is something reason
by ssp 16y ago
No, you are right. The copying is a bad bug. Using sizeof(char) is mostly a coding style question.
The casting of the malloc() return value is something reasonable people can disagree about (by which I mean nerds can endlessly flame each other about). But here are some points:
* In C++, which Mozilla is written in, it is a compile time error to not cast, so most likely this blog post is just wrong. Conceivably, the file could have been a C file in an otherwise C++ project, but even in that case, it's at least understandable why the cast is there.
* If you have a macro that takes the type as a parameter:
#define alloc(type) ((type *)malloc(sizeof(type)))
then the casting is a good thing because it makes the compiler warn if you try to assign the result to the wrong pointer type.
* It is true that if you forget to include the stdlib.h header, the cast will silence a useful warning about converting int to pointer.
- ww520 16y agoIn the blog's comment, someone pointed out the same thing but the author then dismissed it as, "I’m a C programmer, not a C++ programmer, so I wouldn’t know anything about that." But considering Mozilla is a C++ project, it's unfair to critique it out of context.
- sid0 16y agoWell, the file he's quoting is C (note the lang:c filter). OTOH he's quoting Mozilla 1.7.7, which is positively ancient (Firefox 1.0.x I think).