September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,6 @@
L 2,=F'2147483647' 2**31-1
L 3,=F'1' 1
AR 2,3 add register3 to register2
BO OVERFLOW branch on overflow
....
OVERFLOW EQU *

View file

@ -0,0 +1,6 @@
IPM 1 Insert Program Mask
O 1,BITFPO unmask Fixed Overflow
SPM 1 Set Program Mask
...
DS 0F alignment
BITFPO DC BL1'00001000' bit20=1 [start at 16]

View file

@ -0,0 +1,78 @@
identification division.
program-id. overflowing.
data division.
working-storage section.
01 bit8-sized usage binary-char. *> standard
01 bit16-sized usage binary-short. *> standard
01 bit32-sized usage binary-long. *> standard
01 bit64-sized usage binary-double. *> standard
01 bit8-unsigned usage binary-char unsigned. *> standard
01 nebulous-size usage binary-c-long. *> extension
01 picture-size picture s999. *> standard
*> ***************************************************************
procedure division.
*> 32 bit signed integer
subtract 2147483647 from zero giving bit32-sized
display bit32-sized
subtract 1 from bit32-sized giving bit32-sized
ON SIZE ERROR display "32bit signed SIZE ERROR"
end-subtract
*> value was unchanged due to size error trap and trigger
display bit32-sized
display space
*> 8 bit unsigned, size tested, invalid results discarded
add -257 to zero giving bit8-unsigned
ON SIZE ERROR display "bit8-unsigned SIZE ERROR"
end-add
display bit8-unsigned
*> programmers can ignore the safety features
compute bit8-unsigned = -257
display "you asked for it: " bit8-unsigned
display space
*> fixed size
move 999 to picture-size
add 1 to picture-size
ON SIZE ERROR display "picture-sized SIZE ERROR"
end-add
display picture-size
*> programmers doing the following, inadvertently,
*> do not stay employed at banks for long
move 999 to picture-size
add 1 to picture-size
*> intermediate goes to 1000, left end truncated on storage
display "you asked for it: " picture-size
add 1 to picture-size
display "really? you want to keep doing this?: " picture-size
display space
*> C values are undefined by spec, only minimums givens
display "How many bytes in a C long? "
length of nebulous-size
", varies by platform"
display "Regardless, ON SIZE ERROR will catch any invalid result"
*> on a 64bit machine, C long of 8 bytes
add 1 to h'ffffffffffffffff' giving nebulous-size
ON SIZE ERROR display "binary-c-long SIZE ERROR"
end-add
display nebulous-size
*> value will still be in initial state, GnuCOBOL initializes to 0
*> value now goes to 1, no size error, that ship has sailed
add 1 to nebulous-size
ON SIZE ERROR display "binary-c-long size error"
end-add
display "error state is not persistent: ", nebulous-size
goback.
end program overflowing.

View file

@ -0,0 +1,29 @@
// version 1.0.5-2
/* Kotlin (like Java) does not have unsigned integer types but we can simulate
what would happen if we did have an unsigned 32 bit integer type using this extension function */
fun Long.toUInt(): Long = this and 0xffffffffL
@Suppress("INTEGER_OVERFLOW")
fun main(args: Array<String>) {
// The following 'signed' computations all produce compiler warnings that they will lead to an overflow
// which have been ignored
println("*** Signed 32 bit integers ***\n")
println(-(-2147483647 - 1))
println(2000000000 + 2000000000)
println(-2147483647 - 2147483647)
println(46341 * 46341)
println((-2147483647 - 1) / -1)
println("\n*** Signed 64 bit integers ***\n")
println(-(-9223372036854775807 - 1))
println(5000000000000000000 + 5000000000000000000)
println(-9223372036854775807 - 9223372036854775807)
println(3037000500 * 3037000500)
println((-9223372036854775807 - 1) / -1)
// Simulated unsigned computations, no overflow warnings as we're using the Long type
println("\n*** Unsigned 32 bit integers ***\n")
println((-4294967295L).toUInt())
println((3000000000L.toUInt() + 3000000000L.toUInt()).toUInt())
println((2147483647L - 4294967295L.toUInt()).toUInt())
println((65537L * 65537L).toUInt())
}

View file

@ -0,0 +1,94 @@
import macros, strutils
macro toIntVal(s: static[string]): untyped =
result = parseExpr(s)
proc `/`(x,y:int64): float = return (x.float / y.float)
const
strInt32 = ["-(-2147483647-1)",
"2_000_000_000 + 2_000_000_000",
"-2147483647 - 2147483647",
"46341 * 46341",
"(-2147483647-1) / -1"]
shouldBInt32 = ["2147483648",
"4000000000",
"-4294967294",
"2147488281",
"2147483648"]
strInt64 = ["-(-9_223372_036854_775807-1) ",
"5_000000_000000_000000+5_000000_000000_000000",
"-9_223372_036854_775807 - 9_223372_036854_775807",
"3037_000500 * 3037_000500",
"(-9_223372_036854_775807-1) / -1"]
shouldBInt64 = ["9223372036854775808",
"10000000000000000000",
"-18446744073709551614",
"9223372037000250000",
"9223372036854775808"]
strUInt32 = ["-4294967295",
"3000_000000 +% 3000_000000",
"2147483647 -% 4294967295",
"65537 *% 65537"]
shouldBUInt32 = ["-4294967295",
"6000000000",
"-2147483648",
"4295098369"]
strUInt64 = ["-18446744073709551615",
"10_000000_000000_000000 +% 10_000000_000000_000000",
"9_223372_036854_775807 -% 18_446744_073709_551615",
"4294967296 * 4294967296",
"4294967296 *% 4294967296",] # testing * and *%
shouldBUInt64 = ["-18446744073709551615",
"20000000000000000000",
"-9223372036854775808",
"18446744073709551616",
"18446744073709551616"]
#
# use compile time macro to convert string expr to numeric value
#
var
resInt32: seq[string] = @[$toIntVal(strInt32[0]),
$toIntVal(strInt32[1]),
$toIntVal(strInt32[2]),
$toIntVal(strInt32[3]),
$toIntVal(strInt32[4])]
resInt64: seq[string] = @[$toIntVal(strInt64[0]),
$toIntVal(strInt64[1]),
$toIntVal(strInt64[2]),
$toIntVal(strInt64[3]),
$toIntVal(strInt64[4])]
resUInt32: seq[string] = @[$toIntVal(strUInt32[0]),
$toIntVal(strUInt32[1]),
$toIntVal(strUInt32[2]),
$toIntVal(strUInt32[3])]
resUInt64: seq[string] = @["18446744073709551615 out of valid range",
"10_000000_000000_000000 out of valid range",
"18_446744_073709_551615 out of valid range",
$toIntVal(strUInt64[3]),
$toIntVal(strUInt64[4])]
proc main() =
# output format:
#
# stringExpr -> calculatedValueAsAString (expectedValueAsAString)
echo "-- INT32 --"
for i in 0..<resInt32.len:
echo align(strInt32[i], 35), " -> ", align($resInt32[i], 15), " (", $shouldBInt32[i],")"
echo "-- INT64 --"
for i in 0..<resInt64.len:
echo align(strInt64[i], 55), " -> ", align($resInt64[i], 25), " (", $shouldBInt64[i],")"
echo "-- UINT32 --"
for i in 0..<resUInt32.len:
echo align(strUInt32[i], 35), " -> ", align($resUInt32[i], 20), " (", $shouldBUInt32[i],")"
echo "-- UINT64 --"
for i in 0..<resUInt64.len:
echo align(strUInt64[i], 55), " -> ", align($resUInt64[i], 42), " (", $shouldBUInt64[i],")"
main()

View file

@ -0,0 +1,14 @@
'Binary Integer overflow - vbs
i=(-2147483647-1)/-1
wscript.echo i
i0=32767 '=32767 Integer (Fixed) type=2
i1=2147483647 '=2147483647 Long (Fixed) type=3
i2=-(-2147483647-1) '=2147483648 Double (Float) type=5
wscript.echo Cstr(i0) & " : " & typename(i0) & " , " & vartype(i0) & vbcrlf _
& Cstr(i1) & " : " & typename(i1) & " , " & vartype(i1) & vbcrlf _
& Cstr(i2) & " : " & typename(i2) & " , " & vartype(i2)
ii=2147483648-2147483647
if vartype(ii)<>3 or vartype(ii)<>2 then wscript.echo "Integer overflow type=" & typename(ii)
i1=1000000000000000-1 '1E+15-1
i2=i1+1 '1E+15
wscript.echo Cstr(i1) & " , " & Cstr(i2)

View file

@ -0,0 +1 @@
Dim i As Integer '32-bit signed integer

View file

@ -0,0 +1 @@
Dim i As ULong '64-bit unsigned integer

View file

@ -0,0 +1,3 @@
i = -18446744073709551615
i = 10000000000000000000 + 10000000000000000000
i = 9223372036854775807 - 18446744073709551615

View file

@ -0,0 +1 @@
i = 4294967296 * 4294967296

View file

@ -0,0 +1 @@
i = 4294967296 : i = i * i

View file

@ -0,0 +1,7 @@
Dim i As Integer '32-bit signed integer
Try
i = -2147483647 : i = -(i - 1)
Debug.Print(i)
Catch ex As Exception
Debug.Print("Exception raised : " & ex.Message)
End Try

View file

@ -0,0 +1,9 @@
i = -(-2147483647 - 1)
i = 0 - (-2147483647 - 1)
i = -(-2147483647L - 1)
i = -(-2147483647 - 2)
i = 2147483647 + 1
i = 2000000000 + 2000000000
i = -2147483647 - 2147483647
i = 46341 * 46341
i = (-2147483647 - 1) / -1

View file

@ -0,0 +1,2 @@
i = -Int(-2147483647 - 1)
i = -2147483647: i = -(i - 1)

View file

@ -0,0 +1 @@
Dim i As UInteger '32-bit unsigned integer

View file

@ -0,0 +1,4 @@
i = -4294967295
i = 3000000000 + 3000000000
i = 2147483647 - 4294967295
i = 65537 * 65537

View file

@ -0,0 +1 @@
i = 3000000000 : i = i + i

View file

@ -0,0 +1 @@
Dim i As Long '64-bit signed integer

View file

@ -0,0 +1,5 @@
i = -(-9223372036854775807 - 1)
i = 5000000000000000000 + 5000000000000000000
i = -9223372036854775807 - 9223372036854775807
i = 3037000500 * 3037000500
i = (-9223372036854775807 - 1) / -1

View file

@ -0,0 +1 @@
i = -9223372036854775807 : i = -(i - 1)

View file

@ -0,0 +1,12 @@
'Binary Integer overflow - vb6 - 28/02/2017
Dim i As Long '32-bit signed integer
i = -(-2147483647 - 1) '=-2147483648 ?! bug
i = -Int(-2147483647 - 1) '=-2147483648 ?! bug
i = 0 - (-2147483647 - 1) 'Run-time error '6' : Overflow
i = -2147483647: i = -(i - 1) 'Run-time error '6' : Overflow
i = -(-2147483647 - 2) 'Run-time error '6' : Overflow
i = 2147483647 + 1 'Run-time error '6' : Overflow
i = 2000000000 + 2000000000 'Run-time error '6' : Overflow
i = -2147483647 - 2147483647 'Run-time error '6' : Overflow
i = 46341 * 46341 'Run-time error '6' : Overflow
i = (-2147483647 - 1) / -1 'Run-time error '6' : Overflow

View file

@ -0,0 +1,4 @@
i=0
On Error Resume Next
i = 2147483647 + 1
Debug.Print i 'i=0

View file

@ -0,0 +1,6 @@
i=0
On Error GoTo overflow
i = 2147483647 + 1
...
overflow:
Debug.Print "Error: " & Err.Description '-> Error: Overflow

View file

@ -0,0 +1,3 @@
On Error GoTo 0
i = 2147483647 + 1 'Run-time error '6' : Overflow
Debug.Print i

View file

@ -0,0 +1,6 @@
print("Signed 64-bit:\n");
println(-(-9223372036854775807-1));
println(5000000000000000000+5000000000000000000);
println(-9223372036854775807 - 9223372036854775807);
println(3037000500 * 3037000500);
println((-9223372036854775807-1) / -1);