Mercurial > cpython-lru_cache-weakref
annotate Python/mystrtoul.c @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 6f6f965c5e2d |
| children | b6c139544c16 |
| rev | line source |
|---|---|
| 1449 | 1 |
| 9656 | 2 #include "Python.h" |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
3 |
|
8634
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
4 #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE) |
|
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
5 #define _SGI_MP_SOURCE |
|
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
6 #endif |
|
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
7 |
| 2310 | 8 /* Convert a possibly signed character to a nonnegative int */ |
| 9 /* XXX This assumes characters are 8 bits wide */ | |
| 10 #ifdef __CHAR_UNSIGNED__ | |
| 11 #define Py_CHARMASK(c) (c) | |
| 12 #else | |
| 13 #define Py_CHARMASK(c) ((c) & 0xff) | |
| 14 #endif | |
| 15 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
16 /* strtol and strtoul, renamed to avoid conflicts */ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
17 |
| 1449 | 18 /* |
| 19 ** strtoul | |
| 20 ** This is a general purpose routine for converting | |
| 21 ** an ascii string to an integer in an arbitrary base. | |
| 22 ** Leading white space is ignored. If 'base' is zero | |
| 23 ** it looks for a leading 0, 0x or 0X to tell which | |
| 24 ** base. If these are absent it defaults to 10. | |
| 25 ** Base must be 0 or between 2 and 36 (inclusive). | |
| 26 ** If 'ptr' is non-NULL it will contain a pointer to | |
| 27 ** the end of the scan. | |
| 28 ** Errors due to bad pointers will probably result in | |
| 29 ** exceptions - we don't check for them. | |
| 30 */ | |
| 31 | |
| 32 #include <ctype.h> | |
|
10530
d834ecdf8c26
Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents:
9656
diff
changeset
|
33 #ifndef DONT_HAVE_ERRNO_H |
| 1449 | 34 #include <errno.h> |
|
10530
d834ecdf8c26
Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents:
9656
diff
changeset
|
35 #endif |
| 1449 | 36 |
| 37 unsigned long | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
38 PyOS_strtoul(register char *str, char **ptr, int base) |
| 1449 | 39 { |
| 40 register unsigned long result; /* return value of the function */ | |
| 41 register int c; /* current input character */ | |
| 42 register unsigned long temp; /* used in overflow testing */ | |
| 43 int ovf; /* true if overflow occurred */ | |
| 44 | |
| 45 result = 0; | |
| 46 ovf = 0; | |
| 47 | |
| 48 /* catch silly bases */ | |
| 49 if (base != 0 && (base < 2 || base > 36)) | |
| 50 { | |
| 51 if (ptr) | |
| 52 *ptr = str; | |
| 53 return 0; | |
| 54 } | |
| 55 | |
| 56 /* skip leading white space */ | |
| 2310 | 57 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 58 str++; |
| 59 | |
| 60 /* check for leading 0 or 0x for auto-base or base 16 */ | |
| 61 switch (base) | |
| 62 { | |
| 63 case 0: /* look for leading 0, 0x or 0X */ | |
| 64 if (*str == '0') | |
| 65 { | |
| 66 str++; | |
| 67 if (*str == 'x' || *str == 'X') | |
| 68 { | |
| 69 str++; | |
| 70 base = 16; | |
| 71 } | |
| 72 else | |
| 73 base = 8; | |
| 74 } | |
| 75 else | |
| 76 base = 10; | |
| 77 break; | |
| 78 | |
| 79 case 16: /* skip leading 0x or 0X */ | |
| 80 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X')) | |
| 81 str += 2; | |
| 82 break; | |
| 83 } | |
| 84 | |
| 85 /* do the conversion */ | |
| 4596 | 86 while ((c = Py_CHARMASK(*str)) != '\0') |
| 1449 | 87 { |
| 88 if (isdigit(c) && c - '0' < base) | |
| 89 c -= '0'; | |
| 90 else | |
| 91 { | |
| 92 if (isupper(c)) | |
| 93 c = tolower(c); | |
| 94 if (c >= 'a' && c <= 'z') | |
| 95 c -= 'a' - 10; | |
| 96 else /* non-"digit" character */ | |
| 97 break; | |
| 98 if (c >= base) /* non-"digit" character */ | |
| 99 break; | |
| 100 } | |
| 101 temp = result; | |
| 102 result = result * base + c; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
103 #ifndef MPW |
|
6849
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
104 if(base == 10) { |
|
7979
cc47733b4dd2
Address warnings issued by the MSVC++ compiler
Guido van Rossum <guido@python.org>
parents:
6849
diff
changeset
|
105 if(((long)(result - c) / base != (long)temp)) /* overflow */ |
|
6849
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
106 ovf = 1; |
|
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
107 } |
|
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
108 else { |
|
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
109 if ((result - c) / base != temp) /* overflow */ |
|
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
110 ovf = 1; |
|
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
111 } |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
112 #endif |
| 1449 | 113 str++; |
| 114 } | |
| 115 | |
| 116 /* set pointer to point to the last character scanned */ | |
| 117 if (ptr) | |
| 118 *ptr = str; | |
| 119 if (ovf) | |
| 120 { | |
|
5287
5af766a1b531
Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents:
4596
diff
changeset
|
121 result = (unsigned long) ~0L; |
| 1449 | 122 errno = ERANGE; |
| 123 } | |
| 124 return result; | |
| 125 } | |
| 126 | |
| 127 long | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
128 PyOS_strtol(char *str, char **ptr, int base) |
| 1449 | 129 { |
| 130 long result; | |
| 131 char sign; | |
| 132 | |
| 2310 | 133 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 134 str++; |
| 135 | |
| 136 sign = *str; | |
| 137 if (sign == '+' || sign == '-') | |
| 138 str++; | |
| 139 | |
|
5440
9eaa2caed1c3
Oops, missed some renamings.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
140 result = (long) PyOS_strtoul(str, ptr, base); |
| 1449 | 141 |
| 142 /* Signal overflow if the result appears negative, | |
| 143 except for the largest negative integer */ | |
| 144 if (result < 0 && !(sign == '-' && result == -result)) { | |
| 145 errno = ERANGE; | |
| 146 result = 0x7fffffff; | |
| 147 } | |
| 148 | |
| 149 if (sign == '-') | |
| 150 result = -result; | |
| 151 | |
| 152 return result; | |
| 153 } |
