Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Date: Fri, 15 Feb 2019 12:21:15 -0500
From: Rich Felker <dalias@...c.org>
To: "jounijl@...oo.co.uk" <jounijl@...oo.co.uk>
Cc: musl@...ts.openwall.com
Subject: Re: "Arithmetic exception" with modulus operator '%'

On Fri, Feb 15, 2019 at 10:31:22AM +0000, jounijl@...oo.co.uk wrote:
> 
> Exactly. To be complite:
> 
> The host machine prints: "Floating point exception" and outputs a
> core file. Uses: /lib/libc.so.7
> The Alpine prints: "Arithmetic exception".  Uses: /lib/ld-musl-x86_64.so.1
> Solaris 10 prints: "Arithmetic exception". Uses: /lib/libc.so.1 ;
> /lib/libm.so.2
> Ubuntu prints: "Floating point exception" and outputs a core file.
> Uses: /lib/x86_64-linux-gnu/libc.so.6
> 
> To the question "what do you except":
> Of course the behaviour is similar to others and this is correct. As
> in programs the behaviour would be best like this: number%zero would
> be the number it self when number/zero is undefined or infinity
> (maby set the number to the largest known number). To change this,
> some mathematical evaluation would be needed. Answer: mod 0:
> Convenient would be the number it self ?

This has nothing to do with musl or library implementation; what
you're asking for is a *compiler* that defines certain undefined
behavior in a particular way. Even if you had such a thing, writing C
code in order to depend on nonstandard behavior of a particular
compiler would not be a reasonable thing to do.

A better way to achieve the same thing would be just writing a
function that does what you want:

int my_mod(int a, int b)
{
	if (!b) return a;
	else if (b==-1) return 0;
	else return a%b;
}

and using that instead of using the % operator directly. If you need
it to work in constant expression contexts, you could use a macro
instead:

#define MY_MOD(a,b) (!(b) ? (a) : (b)==-1 ? 0 : (a)%(b))

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.