Fossil SCM

Make integer arithmetic in TH1 wrap around by definition, by doing it on unsigned ints: the conversion of a number in Th_ToInt(), and addition, subtraction, multiplication and negation in expressions. The results are as before; the signed overflow that produced them was undefined. Dividing the smallest integer by -1, or taking its remainder, no longer stops fossil with a floating-point exception on x86.

dmestas 2026-11-16 18:04 UTC small-fixes-2
Commit 3e1e91da26268cb72994d82d8a34f9029307014df203a6dd7210d8d866afc9b5
1 file changed +19 -10
+19 -10
--- src/th.c
+++ src/th.c
@@ -2166,31 +2166,37 @@
21662166
}
21672167
}
21682168
}
21692169
21702170
if( rc==TH_OK && eArgType==ARG_INTEGER ){
2171
+ /* Integer arithmetic wraps around. It is done on unsigned ints, for
2172
+ ** which that is defined. Dividing by -1 is negation, written out
2173
+ ** because the machine instruction traps on some processors when
2174
+ ** the result does not fit. */
2175
+ unsigned int uLeft = (unsigned int)iLeft;
2176
+ unsigned int uRight = (unsigned int)iRight;
21712177
int iRes = 0;
21722178
switch( pExpr->pOp->eOp ) {
2173
- case OP_MULTIPLY: iRes = iLeft*iRight; break;
2179
+ case OP_MULTIPLY: iRes = (int)(uLeft*uRight); break;
21742180
case OP_DIVIDE:
21752181
if( !iRight ){
21762182
Th_ErrorMessage(interp, "Divide by 0:", zLeft, nLeft);
21772183
rc = TH_ERROR;
21782184
goto finish;
21792185
}
2180
- iRes = iLeft/iRight;
2186
+ iRes = iRight==(-1) ? (int)(0u-uLeft) : iLeft/iRight;
21812187
break;
21822188
case OP_MODULUS:
21832189
if( !iRight ){
21842190
Th_ErrorMessage(interp, "Modulo by 0:", zLeft, nLeft);
21852191
rc = TH_ERROR;
21862192
goto finish;
21872193
}
2188
- iRes = iLeft%iRight;
2194
+ iRes = iRight==(-1) ? 0 : iLeft%iRight;
21892195
break;
2190
- case OP_ADD: iRes = iLeft+iRight; break;
2191
- case OP_SUBTRACT: iRes = iLeft-iRight; break;
2196
+ case OP_ADD: iRes = (int)(uLeft+uRight); break;
2197
+ case OP_SUBTRACT: iRes = (int)(uLeft-uRight); break;
21922198
case OP_LEFTSHIFT: {
21932199
iRes = (int)(((unsigned int)iLeft)<<(iRight&0x1f));
21942200
break;
21952201
}
21962202
case OP_RIGHTSHIFT: iRes = iLeft>>(iRight&0x1f); break;
@@ -2203,11 +2209,11 @@
22032209
case OP_BITWISE_AND: iRes = iLeft&iRight; break;
22042210
case OP_BITWISE_XOR: iRes = iLeft^iRight; break;
22052211
case OP_BITWISE_OR: iRes = iLeft|iRight; break;
22062212
case OP_LOGICAL_AND: iRes = iLeft&&iRight; break;
22072213
case OP_LOGICAL_OR: iRes = iLeft||iRight; break;
2208
- case OP_UNARY_MINUS: iRes = -iLeft; break;
2214
+ case OP_UNARY_MINUS: iRes = (int)(0u-uLeft); break;
22092215
case OP_UNARY_PLUS: iRes = +iLeft; break;
22102216
case OP_BITWISE_NOT: iRes = ~iLeft; break;
22112217
case OP_LOGICAL_NOT: iRes = !iLeft; break;
22122218
default: assert(!"Internal error");
22132219
}
@@ -2822,14 +2828,17 @@
28222828
** If successful, store the result in *piOut and return TH_OK.
28232829
**
28242830
** If the string cannot be converted to an integer, return TH_ERROR.
28252831
** If the interp argument is not NULL, leave an error message in the
28262832
** interpreter result too.
2833
+**
2834
+** A value too large for an int wraps around. The arithmetic is done on
2835
+** an unsigned int, for which that is defined.
28272836
*/
28282837
int Th_ToInt(Th_Interp *interp, const char *z, int n, int *piOut){
28292838
int i = 0;
2830
- int iOut = 0;
2839
+ unsigned int iOut = 0;
28312840
int base = 10;
28322841
int (*isdigit)(char) = th_isdigit;
28332842
28342843
if( n<0 ){
28352844
n = th_strlen(z);
@@ -2866,18 +2875,18 @@
28662875
}else if( c>='A' ){
28672876
c -= 'A'-10;
28682877
}else{
28692878
c -= '0';
28702879
}
2871
- iOut = iOut * base + c;
2880
+ iOut = iOut * (unsigned int)base + (unsigned int)c;
28722881
}
28732882
28742883
if( n>0 && z[0]=='-' ){
2875
- iOut *= -1;
2884
+ iOut = 0u - iOut;
28762885
}
28772886
2878
- *piOut = iOut;
2887
+ *piOut = (int)iOut;
28792888
return TH_OK;
28802889
}
28812890
28822891
/*
28832892
** Try to convert the string passed as arguments (z, n) to a double.
28842893
--- src/th.c
+++ src/th.c
@@ -2166,31 +2166,37 @@
2166 }
2167 }
2168 }
2169
2170 if( rc==TH_OK && eArgType==ARG_INTEGER ){
 
 
 
 
 
 
2171 int iRes = 0;
2172 switch( pExpr->pOp->eOp ) {
2173 case OP_MULTIPLY: iRes = iLeft*iRight; break;
2174 case OP_DIVIDE:
2175 if( !iRight ){
2176 Th_ErrorMessage(interp, "Divide by 0:", zLeft, nLeft);
2177 rc = TH_ERROR;
2178 goto finish;
2179 }
2180 iRes = iLeft/iRight;
2181 break;
2182 case OP_MODULUS:
2183 if( !iRight ){
2184 Th_ErrorMessage(interp, "Modulo by 0:", zLeft, nLeft);
2185 rc = TH_ERROR;
2186 goto finish;
2187 }
2188 iRes = iLeft%iRight;
2189 break;
2190 case OP_ADD: iRes = iLeft+iRight; break;
2191 case OP_SUBTRACT: iRes = iLeft-iRight; break;
2192 case OP_LEFTSHIFT: {
2193 iRes = (int)(((unsigned int)iLeft)<<(iRight&0x1f));
2194 break;
2195 }
2196 case OP_RIGHTSHIFT: iRes = iLeft>>(iRight&0x1f); break;
@@ -2203,11 +2209,11 @@
2203 case OP_BITWISE_AND: iRes = iLeft&iRight; break;
2204 case OP_BITWISE_XOR: iRes = iLeft^iRight; break;
2205 case OP_BITWISE_OR: iRes = iLeft|iRight; break;
2206 case OP_LOGICAL_AND: iRes = iLeft&&iRight; break;
2207 case OP_LOGICAL_OR: iRes = iLeft||iRight; break;
2208 case OP_UNARY_MINUS: iRes = -iLeft; break;
2209 case OP_UNARY_PLUS: iRes = +iLeft; break;
2210 case OP_BITWISE_NOT: iRes = ~iLeft; break;
2211 case OP_LOGICAL_NOT: iRes = !iLeft; break;
2212 default: assert(!"Internal error");
2213 }
@@ -2822,14 +2828,17 @@
2822 ** If successful, store the result in *piOut and return TH_OK.
2823 **
2824 ** If the string cannot be converted to an integer, return TH_ERROR.
2825 ** If the interp argument is not NULL, leave an error message in the
2826 ** interpreter result too.
 
 
 
2827 */
2828 int Th_ToInt(Th_Interp *interp, const char *z, int n, int *piOut){
2829 int i = 0;
2830 int iOut = 0;
2831 int base = 10;
2832 int (*isdigit)(char) = th_isdigit;
2833
2834 if( n<0 ){
2835 n = th_strlen(z);
@@ -2866,18 +2875,18 @@
2866 }else if( c>='A' ){
2867 c -= 'A'-10;
2868 }else{
2869 c -= '0';
2870 }
2871 iOut = iOut * base + c;
2872 }
2873
2874 if( n>0 && z[0]=='-' ){
2875 iOut *= -1;
2876 }
2877
2878 *piOut = iOut;
2879 return TH_OK;
2880 }
2881
2882 /*
2883 ** Try to convert the string passed as arguments (z, n) to a double.
2884
--- src/th.c
+++ src/th.c
@@ -2166,31 +2166,37 @@
2166 }
2167 }
2168 }
2169
2170 if( rc==TH_OK && eArgType==ARG_INTEGER ){
2171 /* Integer arithmetic wraps around. It is done on unsigned ints, for
2172 ** which that is defined. Dividing by -1 is negation, written out
2173 ** because the machine instruction traps on some processors when
2174 ** the result does not fit. */
2175 unsigned int uLeft = (unsigned int)iLeft;
2176 unsigned int uRight = (unsigned int)iRight;
2177 int iRes = 0;
2178 switch( pExpr->pOp->eOp ) {
2179 case OP_MULTIPLY: iRes = (int)(uLeft*uRight); break;
2180 case OP_DIVIDE:
2181 if( !iRight ){
2182 Th_ErrorMessage(interp, "Divide by 0:", zLeft, nLeft);
2183 rc = TH_ERROR;
2184 goto finish;
2185 }
2186 iRes = iRight==(-1) ? (int)(0u-uLeft) : iLeft/iRight;
2187 break;
2188 case OP_MODULUS:
2189 if( !iRight ){
2190 Th_ErrorMessage(interp, "Modulo by 0:", zLeft, nLeft);
2191 rc = TH_ERROR;
2192 goto finish;
2193 }
2194 iRes = iRight==(-1) ? 0 : iLeft%iRight;
2195 break;
2196 case OP_ADD: iRes = (int)(uLeft+uRight); break;
2197 case OP_SUBTRACT: iRes = (int)(uLeft-uRight); break;
2198 case OP_LEFTSHIFT: {
2199 iRes = (int)(((unsigned int)iLeft)<<(iRight&0x1f));
2200 break;
2201 }
2202 case OP_RIGHTSHIFT: iRes = iLeft>>(iRight&0x1f); break;
@@ -2203,11 +2209,11 @@
2209 case OP_BITWISE_AND: iRes = iLeft&iRight; break;
2210 case OP_BITWISE_XOR: iRes = iLeft^iRight; break;
2211 case OP_BITWISE_OR: iRes = iLeft|iRight; break;
2212 case OP_LOGICAL_AND: iRes = iLeft&&iRight; break;
2213 case OP_LOGICAL_OR: iRes = iLeft||iRight; break;
2214 case OP_UNARY_MINUS: iRes = (int)(0u-uLeft); break;
2215 case OP_UNARY_PLUS: iRes = +iLeft; break;
2216 case OP_BITWISE_NOT: iRes = ~iLeft; break;
2217 case OP_LOGICAL_NOT: iRes = !iLeft; break;
2218 default: assert(!"Internal error");
2219 }
@@ -2822,14 +2828,17 @@
2828 ** If successful, store the result in *piOut and return TH_OK.
2829 **
2830 ** If the string cannot be converted to an integer, return TH_ERROR.
2831 ** If the interp argument is not NULL, leave an error message in the
2832 ** interpreter result too.
2833 **
2834 ** A value too large for an int wraps around. The arithmetic is done on
2835 ** an unsigned int, for which that is defined.
2836 */
2837 int Th_ToInt(Th_Interp *interp, const char *z, int n, int *piOut){
2838 int i = 0;
2839 unsigned int iOut = 0;
2840 int base = 10;
2841 int (*isdigit)(char) = th_isdigit;
2842
2843 if( n<0 ){
2844 n = th_strlen(z);
@@ -2866,18 +2875,18 @@
2875 }else if( c>='A' ){
2876 c -= 'A'-10;
2877 }else{
2878 c -= '0';
2879 }
2880 iOut = iOut * (unsigned int)base + (unsigned int)c;
2881 }
2882
2883 if( n>0 && z[0]=='-' ){
2884 iOut = 0u - iOut;
2885 }
2886
2887 *piOut = (int)iOut;
2888 return TH_OK;
2889 }
2890
2891 /*
2892 ** Try to convert the string passed as arguments (z, n) to a double.
2893

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button