fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

C backend: potential overflow in nano_time intrinsic

Open fridis opened this issue 3 years ago • 1 comments

The code

          return CExpr.call("clock", new List<>())
            .mul(CExpr.uint64const(1_000_000_000))
            .div(CExpr.ident("CLOCKS_PER_SEC"))
            .ret();

may result in an overflow, see the documentatrion of clock:

The C standard allows for arbitrary values at the start of the program; subtract the value returned from a call to clock() at the start of the program to get maximum portability.

Fairly unlikely, but awful if it happens...

fridis avatar Mar 31 '22 07:03 fridis

This was recently changed to:

https://github.com/tokiwa-software/fuzion/blob/8d00a84bed98f42a7699096e1e8808150df6329e/src/dev/flang/be/c/Intrinsics.java#L687-L703

but this seems to be equivalent to what Go for example is doing at:

https://cs.opensource.google/go/go/+/master:src/runtime/time_linux_amd64.s

So I believe this is fine.

maxteufel avatar Mar 31 '23 11:03 maxteufel

Code has meanwhile been changed.

michaellilltokiwa avatar Oct 16 '24 07:10 michaellilltokiwa