News for hackers. Stuff that matters.
Re:Sizeof(char) is 1 by patio11 on Monday September 06, 2010 @01:53AM (#1666007)
OK, I'm not exactly a professional C programmer, but among those who are: is the use of sizeof(char) here more important than the copy operation that is going to write the terminating null past the area actually malloc'ed?

Because that sounds suspiciously like "potentially exploitable" to me.

Re:Sizeof(char) is 1 by ssp on Monday September 06, 2010 @02:26AM (#1666062)
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.

Re:Sizeof(char) is 1 by ww520 on Monday September 06, 2010 @04:30AM (#1666252)
In 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.
Re:Sizeof(char) is 1 by sid0 on Monday September 06, 2010 @04:36AM (#1666259)
Well, 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).
Re:Sizeof(char) is 1 by psyklic on Monday September 06, 2010 @02:05AM (#1666033)
(1) sizeof is evaluated at compile time, so it would evaluate to (1 * strlen(params->text)), which the compiler would then optimize to just strlen(params->text). So, sizeof() is not exploitable.

(2) A terminating null character is placed at the end of strings, not at the end of allocated memory. malloc does not alter the memory before it returns a pointer to it; it is up to the programmer to ensure he is writing in the proper area.

Re:Sizeof(char) is 1 by chc on Monday September 06, 2010 @02:08AM (#1666037)
The fact that it's strlen(params->text) is precisely why it's exploitable. If we intend to write strlen(params->text)+1 bytes and allocate less, we've set ourselves up for a buffer overrun.
Re:Sizeof(char) is 1 by masklinn on Monday September 06, 2010 @05:32AM (#1666330)
But that has nothing to do with the sizeof(). It's a completely independent issue.