Program exit code has incorrect modulo

Bug #552413 reported by Matt Giuca
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Mars
Fix Released
Low
Matt Giuca

Bug Description

The current algorithm for setting the return value of the program is to take the result of main (a bignum) and convert it into a machine-size int (the OS typically mods it into a byte):

r := r % MAX_INT

This is incorrect as MAX_INT maps to 0, -1 maps to 0, and MAX_INT+1 maps to 1.

MAX_INT and -1 should map to MAX_INT; MAX_INT+1 should map to 0.

Thus the formula should be:

r := r % (MAX_INT+1)

Related branches

Revision history for this message
Matt Giuca (mgiuca) wrote :

Fixed in trunk r991.

Changed in mars:
status: In Progress → Fix Committed
Revision history for this message
Matt Giuca (mgiuca) wrote :

This new behaviour isn't technically *quite* correct, as negative integers are rounded to positive ints. Most operating systems will further round this to an unsigned byte, but we should return the correct code to the OS. Integers in range INT_MIN to INT_MAX should be preserved, not made positive. Therefore, use the 'rem' operator, not 'mod' (truncating division, not floored).

Changed in mars:
status: Fix Committed → Triaged
Revision history for this message
Matt Giuca (mgiuca) wrote :

Actually, rem isn't correct either. This needs to be plain stupid integer truncation -- i.e., MAX_INT and MIN_INT should be preserved, MAX_INT+1 should map to MIN_INT, and MIN_INT-1 should map to MAX_INT.

There doesn't appear to be a function in Mercury for converting an integer to an int with truncation (filed bug #141 with Mercury). In the meantime, write one.

Revision history for this message
Matt Giuca (mgiuca) wrote :

Fixed again in trunk r992.

Changed in mars:
status: Triaged → Fix Committed
Matt Giuca (mgiuca)
Changed in mars:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.