2014.12.27 23:06 "[Tiff] [PATCH] tiff2ps: fix grayscale with unassociated alpha (and other extrasamples != 0)", by Yuriy M. Kaminskiy

2015.01.05 19:44 "Re: [Tiff] [PATCH] tif_luv, tif_pixarlog, ppm2tiff: get rid of duplicates of TIFFSafeMultiply", by Olivier Paquet

2014-12-31 11:04 GMT-05:00 Bob Friesenhahn <bfriesen@simple.dallas.tx.us>:

Whatever is done, it would be good if the approach is normalized rather than creating many one-of solutions in the code.

I agree, as soon as we can figure out what to normalize it too :-)

For now, I have fixed checkAdd64 since it was plain nonsense. I have left the _ms functions alone since, as Yuriy pointed out, the multiply is also unreliable because of undefined behavior (of signed int overflow). This means TIFFSafeMultiply with tmsize_t is not a proper solution either as currently written.

I think the easist fix for those would be to do the math with unsigned integers after validation that the input is nonnegative. Full check of signed integer multiply is also possible but it is more code than I'm comfortable putting in a macro.

There is value gained from using pre-processor macros in that they do not influence the values type. Hard-coded functions may produce an unexpected result if the function prototype converts the input value to a different type. For example, if size_t is assumed, then there could be a problem if the computation is intentionally using types larger than size_t, or a signed type.

Macros can be misused in many ways as well. As pointed out above, you don't know the data type in the macro and the code needed to detect overflow should be different for signed/unsigned data. It gets even worse if you accidentally mix signed and unsigned. I'm not sure C offers any good solution to prevent programmer error here. It all comes down to using the macro/function properly.

With that said, as libtiff has fairly few use cases, I think we could write different macros to handle them (ie. tmsize_t multiply, uint32 multiply, uint64 multiply, ...). Hopefully someone will have a better idea.