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.
Commit
3e1e91da26268cb72994d82d8a34f9029307014df203a6dd7210d8d866afc9b5
Parent
856b85ede27ef1a…
1 file changed
+19
-10
M
src/th.c
+19
-10
| --- src/th.c | ||
| +++ src/th.c | ||
| @@ -2166,31 +2166,37 @@ | ||
| 2166 | 2166 | } |
| 2167 | 2167 | } |
| 2168 | 2168 | } |
| 2169 | 2169 | |
| 2170 | 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; | |
| 2171 | 2177 | int iRes = 0; |
| 2172 | 2178 | switch( pExpr->pOp->eOp ) { |
| 2173 | - case OP_MULTIPLY: iRes = iLeft*iRight; break; | |
| 2179 | + case OP_MULTIPLY: iRes = (int)(uLeft*uRight); break; | |
| 2174 | 2180 | case OP_DIVIDE: |
| 2175 | 2181 | if( !iRight ){ |
| 2176 | 2182 | Th_ErrorMessage(interp, "Divide by 0:", zLeft, nLeft); |
| 2177 | 2183 | rc = TH_ERROR; |
| 2178 | 2184 | goto finish; |
| 2179 | 2185 | } |
| 2180 | - iRes = iLeft/iRight; | |
| 2186 | + iRes = iRight==(-1) ? (int)(0u-uLeft) : iLeft/iRight; | |
| 2181 | 2187 | break; |
| 2182 | 2188 | case OP_MODULUS: |
| 2183 | 2189 | if( !iRight ){ |
| 2184 | 2190 | Th_ErrorMessage(interp, "Modulo by 0:", zLeft, nLeft); |
| 2185 | 2191 | rc = TH_ERROR; |
| 2186 | 2192 | goto finish; |
| 2187 | 2193 | } |
| 2188 | - iRes = iLeft%iRight; | |
| 2194 | + iRes = iRight==(-1) ? 0 : iLeft%iRight; | |
| 2189 | 2195 | 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; | |
| 2192 | 2198 | case OP_LEFTSHIFT: { |
| 2193 | 2199 | iRes = (int)(((unsigned int)iLeft)<<(iRight&0x1f)); |
| 2194 | 2200 | break; |
| 2195 | 2201 | } |
| 2196 | 2202 | case OP_RIGHTSHIFT: iRes = iLeft>>(iRight&0x1f); break; |
| @@ -2203,11 +2209,11 @@ | ||
| 2203 | 2209 | case OP_BITWISE_AND: iRes = iLeft&iRight; break; |
| 2204 | 2210 | case OP_BITWISE_XOR: iRes = iLeft^iRight; break; |
| 2205 | 2211 | case OP_BITWISE_OR: iRes = iLeft|iRight; break; |
| 2206 | 2212 | case OP_LOGICAL_AND: iRes = iLeft&&iRight; break; |
| 2207 | 2213 | 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; | |
| 2209 | 2215 | case OP_UNARY_PLUS: iRes = +iLeft; break; |
| 2210 | 2216 | case OP_BITWISE_NOT: iRes = ~iLeft; break; |
| 2211 | 2217 | case OP_LOGICAL_NOT: iRes = !iLeft; break; |
| 2212 | 2218 | default: assert(!"Internal error"); |
| 2213 | 2219 | } |
| @@ -2822,14 +2828,17 @@ | ||
| 2822 | 2828 | ** If successful, store the result in *piOut and return TH_OK. |
| 2823 | 2829 | ** |
| 2824 | 2830 | ** If the string cannot be converted to an integer, return TH_ERROR. |
| 2825 | 2831 | ** If the interp argument is not NULL, leave an error message in the |
| 2826 | 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. | |
| 2827 | 2836 | */ |
| 2828 | 2837 | int Th_ToInt(Th_Interp *interp, const char *z, int n, int *piOut){ |
| 2829 | 2838 | int i = 0; |
| 2830 | - int iOut = 0; | |
| 2839 | + unsigned int iOut = 0; | |
| 2831 | 2840 | int base = 10; |
| 2832 | 2841 | int (*isdigit)(char) = th_isdigit; |
| 2833 | 2842 | |
| 2834 | 2843 | if( n<0 ){ |
| 2835 | 2844 | n = th_strlen(z); |
| @@ -2866,18 +2875,18 @@ | ||
| 2866 | 2875 | }else if( c>='A' ){ |
| 2867 | 2876 | c -= 'A'-10; |
| 2868 | 2877 | }else{ |
| 2869 | 2878 | c -= '0'; |
| 2870 | 2879 | } |
| 2871 | - iOut = iOut * base + c; | |
| 2880 | + iOut = iOut * (unsigned int)base + (unsigned int)c; | |
| 2872 | 2881 | } |
| 2873 | 2882 | |
| 2874 | 2883 | if( n>0 && z[0]=='-' ){ |
| 2875 | - iOut *= -1; | |
| 2884 | + iOut = 0u - iOut; | |
| 2876 | 2885 | } |
| 2877 | 2886 | |
| 2878 | - *piOut = iOut; | |
| 2887 | + *piOut = (int)iOut; | |
| 2879 | 2888 | return TH_OK; |
| 2880 | 2889 | } |
| 2881 | 2890 | |
| 2882 | 2891 | /* |
| 2883 | 2892 | ** Try to convert the string passed as arguments (z, n) to a double. |
| 2884 | 2893 |
| --- 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 |