Joe Buck writes: > inline size_t __compute_size(size_t num, size_t size) { > size_t product = num * size; > return product >= num ? product : ~size_t(0); > }
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; } Ross Ridge