* Ross Ridge: > Florian Weimer writes: >>I don't think this check is correct. Consider num = 0x33333334 and >>size = 6. It seems that the check is difficult to perform efficiently >>unless the architecture provides unsigned multiplication with overflow >>detection, or an instruction to implement __builtin_clz. > > This should work instead: > > inline size_t __compute_size(size_t num, size_t size) { > if (num > ~size_t(0) / size) > return ~size_t(0); > return num * size; > }
Yeah, but that division is fairly expensive if it can't be performed at compile time. OTOH, if __compute_size is inlined in all places, code size does increase somewhat.