| 1 |
(* Copyright (C) 2014 Free Software Foundation, Inc. *) |
| 2 |
(* This file is part of GNU Modula-2. |
| 3 |
|
| 4 |
GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 5 |
the terms of the GNU General Public License as published by the Free |
| 6 |
Software Foundation; either version 3, or (at your option) any later |
| 7 |
version. |
| 8 |
|
| 9 |
GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 |
for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along |
| 15 |
with gm2; see the file COPYING. If not, write to the Free Software |
| 16 |
Foundation, 51 Franklin Street, Fifth Floor, |
| 17 |
Boston, MA 02110-1301, USA. *) |
| 18 |
|
| 19 |
IMPLEMENTATION MODULE EndFor ; |
| 20 |
|
| 21 |
FROM libc IMPORT exit, printf ; |
| 22 |
|
| 23 |
|
| 24 |
PROCEDURE CheckLongintInteger (des: LONGINT; inc: INTEGER) ; |
| 25 |
VAR |
| 26 |
lg : LONGINT ; |
| 27 |
room, desn: LONGINT ; |
| 28 |
BEGIN |
| 29 |
lg := VAL(LONGINT, inc) ; |
| 30 |
IF inc>=0 |
| 31 |
THEN |
| 32 |
IF des>=0 |
| 33 |
THEN |
| 34 |
room := MAX(LONGINT)-des ; |
| 35 |
IF lg>room |
| 36 |
THEN |
| 37 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 38 |
exit (2) |
| 39 |
END |
| 40 |
ELSE |
| 41 |
(* inc can never cause an overflow given its type *) |
| 42 |
END |
| 43 |
ELSE |
| 44 |
(* inc < 0 *) |
| 45 |
IF des>=0 |
| 46 |
THEN |
| 47 |
(* inc can never cause an underflow given its type *) |
| 48 |
ELSE |
| 49 |
(* des < 0 *) |
| 50 |
IF des=MIN(LONGINT) |
| 51 |
THEN |
| 52 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 53 |
exit (4) |
| 54 |
ELSE |
| 55 |
desn := -des; |
| 56 |
room := MIN(LONGINT)+desn ; |
| 57 |
lg := -lg ; |
| 58 |
IF lg>room |
| 59 |
THEN |
| 60 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 61 |
exit (5) |
| 62 |
END |
| 63 |
END |
| 64 |
END |
| 65 |
END |
| 66 |
END CheckLongintInteger ; |
| 67 |
|
| 68 |
|
| 69 |
PROCEDURE CheckLongintLongint (des: LONGINT; inc: LONGINT) ; |
| 70 |
VAR |
| 71 |
lg : LONGINT ; |
| 72 |
room, desn: LONGINT ; |
| 73 |
BEGIN |
| 74 |
IF inc>=0 |
| 75 |
THEN |
| 76 |
IF des>=0 |
| 77 |
THEN |
| 78 |
lg := inc ; |
| 79 |
room := MAX(LONGINT)-des ; |
| 80 |
IF lg>room |
| 81 |
THEN |
| 82 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 83 |
exit (2) |
| 84 |
END |
| 85 |
ELSE |
| 86 |
(* inc can never cause an overflow given its type *) |
| 87 |
END |
| 88 |
ELSE |
| 89 |
(* inc < 0 *) |
| 90 |
IF des>=0 |
| 91 |
THEN |
| 92 |
(* inc can never cause an underflow given its type *) |
| 93 |
ELSE |
| 94 |
IF des=MIN(LONGINT) |
| 95 |
THEN |
| 96 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 97 |
exit (4) |
| 98 |
ELSE |
| 99 |
IF inc=MIN(LONGINT) |
| 100 |
THEN |
| 101 |
IF des=0 |
| 102 |
THEN |
| 103 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 104 |
exit (5) |
| 105 |
END |
| 106 |
ELSE |
| 107 |
(* des < 0 *) |
| 108 |
desn := -des; |
| 109 |
room := MIN(LONGINT)+desn ; |
| 110 |
lg := -inc ; |
| 111 |
IF lg>room |
| 112 |
THEN |
| 113 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 114 |
exit (6) |
| 115 |
END |
| 116 |
END |
| 117 |
END |
| 118 |
END |
| 119 |
END |
| 120 |
END CheckLongintLongint ; |
| 121 |
|
| 122 |
|
| 123 |
PROCEDURE CheckCardinalInteger (des: CARDINAL; inc: INTEGER) ; |
| 124 |
VAR |
| 125 |
room: CARDINAL ; |
| 126 |
lg : CARDINAL ; |
| 127 |
BEGIN |
| 128 |
IF inc>=0 |
| 129 |
THEN |
| 130 |
IF des>=0 |
| 131 |
THEN |
| 132 |
lg := VAL(CARDINAL, inc) ; |
| 133 |
room := MAX(CARDINAL)-des ; |
| 134 |
IF lg>room |
| 135 |
THEN |
| 136 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 137 |
exit (2) |
| 138 |
END |
| 139 |
ELSE |
| 140 |
(* inc can never cause an overflow given its type *) |
| 141 |
END |
| 142 |
ELSE |
| 143 |
(* inc < 0 *) |
| 144 |
IF des>VAL(CARDINAL, MAX(INTEGER)) |
| 145 |
THEN |
| 146 |
(* inc can never cause an underflow given its range *) |
| 147 |
ELSE |
| 148 |
(* des <= MAX(INTEGER) *) |
| 149 |
IF des = CARDINAL (MAX (INTEGER)) |
| 150 |
THEN |
| 151 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 152 |
exit (4) |
| 153 |
ELSE |
| 154 |
IF inc=MIN(INTEGER) |
| 155 |
THEN |
| 156 |
IF des=0 |
| 157 |
THEN |
| 158 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 159 |
exit (5) |
| 160 |
END |
| 161 |
ELSE |
| 162 |
lg := VAL(CARDINAL, -inc) ; |
| 163 |
IF lg>des |
| 164 |
THEN |
| 165 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 166 |
exit (5) |
| 167 |
END |
| 168 |
END |
| 169 |
END |
| 170 |
END |
| 171 |
END |
| 172 |
END CheckCardinalInteger ; |
| 173 |
|
| 174 |
|
| 175 |
PROCEDURE CheckCardinalCardinal (des: CARDINAL; inc: CARDINAL) ; |
| 176 |
BEGIN |
| 177 |
IF MAX(CARDINAL)-des<inc |
| 178 |
THEN |
| 179 |
printf("increment exceeds range at end of FOR loop\n") ; |
| 180 |
exit (2) |
| 181 |
END |
| 182 |
END CheckCardinalCardinal ; |
| 183 |
|
| 184 |
|
| 185 |
END EndFor. |