Found by Coverty in LibreOffice

Rikki Cattermole via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 10 02:23:07 PST 2014


On 10/11/2014 11:04 p.m., bearophile wrote:
> Some of the bugs that the static analysis tool Coverty has found in the
> C++ code of LibreOffice:
>
> http://cgit.freedesktop.org/libreoffice/core/log/?qt=grep&q=coverity
>
>
> Cases like this remind me that division operator shouldn't accept a
> divisor of generic integer/floating/multi-precision type, but a type
> that lacks a zero:
>
> http://cgit.freedesktop.org/libreoffice/core/commit/?id=4a83b67e3c3dc8bceb6602ce155f2463f72f4855
>
>
> http://cgit.freedesktop.org/libreoffice/core/commit/?id=7ca34b04c0915cb00345afa1ba7cfc736f82f9a1
>
>
> http://cgit.freedesktop.org/libreoffice/core/commit/?id=c61b2066660fb0130f0b0f9f51b48e3799625b83
>
>
> Bye,
> bearophile

Interesting.
Code like this could be rather interesting.

alias NonZero = AbituaryRestriction!("value == 0", "Value cannot be 0");
template AbituaryRestriction(string restriction, string reason) {
	struct AbituaryRestriction(T) {
		T value;
		alias value this;
	
		this(T value) {
			this.value = value;
		}
		
		invariant() {
			if (mixin(restriction)) {
				throw new Exception(reason);	
			}
		}
		
		void opAssign(float value) {
			this.value = value;	
		}
	}
}

void main() {
	NonZero!float ft = 0.5f;
	ft = 0;
}


More information about the Digitalmars-d mailing list