4 ms·
This was interesting, but I don't get this part of the example code: if (w == 0 || h <= SIZE_MAX / w) To me, that condition is just super-strange: if w is
by unwind 9y ago
This was interesting, but I don't get this part of the example code:
if (w == 0 || h <= SIZE_MAX / w)
To me, that condition is just super-strange: if w is zero, the product w x h is zero too, so the entire allocation becomes pointless. Surely it should be:
if (w != 0 && h <= SIZE_MAX / w)
or something? Having these kinds of discussions based on code that is confusing in itself just becomes too much, for me.
- dingo_bat 9y agoYou're right. The way you wrote is how I would have too. Author has written it weirdly.
- gsg 9y agoMy guess would be that they were thinking h <= SIZE_MAX / w, and added the most obvious logic to avoid a division by zero.
- tom_ 9y agomalloc(0) is implementation-defined: https://port70.net/~nsz/c/c11/n1570.html#7.22.3p1 https://port70.net/~nsz/c/c11/n1570.html#7.22.3p1 One might, I suppose, decide that new_image will be exactly equivalent to the system malloc in this respect, in which case malloc(0) should be called when appropriate.
- TheRealPomax 9y agopointless, but not wrong: calloc on a zero width image should still yield a memory pointer, even if it points to a zero length stretch of memory. It would be strictly wrong to return a null pointer just because (one of) the dimensions happens to be zero. It might read a little funny, but it's actually quite correct with respects to what the code path should be expected to do. Valid dimensions should yield a valid pointer. Even if they're validly zero =)
- 8xde0wcNwpslOw 9y ago>It would be strictly wrong to return a null pointer just because (one of) the dimensions happens to be zero. Strictly wrong according to whom? At least not any C standard I know of. Besides, even if the function did return a non-null pointer value (which it is allowed to do; the behaviour is implementation-defined) you aren't allowed to dereference it any more than a null pointer, making it hardly any more "valid", either.
- TheRealPomax 9y agoAccording to code design mostly. It's not a syntax issue (both are fine syntax) but a logic issue. You should be able to trust that as long as your dimensions are valid, any for loop over the resulting list works. And in graphics contexts, zero is typically a perfectly valid value to set one (or both) dimensions to (negative values, however, are not). Of course, you _can_ say "I think 0 is an invalid dimension" but given that plenty of frameworks and applications consider 0 just fine, that's kind of an unnecessary restriction for the sake of writing a conditional that is arguably irrelevantly "easier" to read.
- meuk 9y agoYou could argue that a null pointer would be a correct pointer, since you are not allowed to dereference it anyway (since there is no data to access). It will probably break some assertions and assumptions though. Edit: I see that 8xde0wcNwpslOw (what a username) made the same point already.
- meuk 9y agoI think overflow occurs when w * h > SIZE_MAX So you just want to make sure that w > 0 and w * h <= SIZE_MAX. But if w * h is large enough, it overflows, which would result in w * h <= SIZE_MAX being evaluated to true. I agree that it is confusing though.
- Bromskloss 9y agoWhy disallow zero-pixel images?