Author: Whiteknight
Date: Wed Aug 6 16:04:14 2008
New Revision: 30073
Modified:
trunk/src/string.c
Log:
[CORE] fix to src/string.c:string_to_int(). If a NULL string is passed, the
code would segfault on a null pointer dereference. Now, if a NULL string is
passed, it returns 0. Not the prettiest fix ever.
Modified: trunk/src/string.c
==============================================================================
--- trunk/src/string.c (original)
+++ trunk/src/string.c Wed Aug 6 16:04:14 2008
@@ -1939,43 +1939,47 @@
INTVAL
string_to_int(SHIM_INTERP, ARGIN(const STRING *s))
{
- const char *start = s->strstart;
- const char * const end = start + s->bufused;
- int sign = 1;
- INTVAL in_number = 0;
- INTVAL i = 0;
+ if(s == NULL)
+ return 0;
+ {
+ const char *start = s->strstart;
+ const char * const end = start + s->bufused;
+ int sign = 1;
+ INTVAL in_number = 0;
+ INTVAL i = 0;
- PARROT_ASSERT(s);
+ PARROT_ASSERT(s);
- while (start < end) {
- const unsigned char c = *start;
+ while (start < end) {
+ const unsigned char c = *start;
- if (isdigit((unsigned char)c)) {
- in_number = 1;
- i = i * 10 + (c - '0');
- }
- else if (!in_number) {
- /* we've not yet seen any digits */
- if (c == '-') {
- sign = -1;
+ if (isdigit((unsigned char)c)) {
in_number = 1;
+ i = i * 10 + (c - '0');
}
- else if (c == '+')
- in_number = 1;
- else if (isspace((unsigned char)c))
- ;
- else
+ else if (!in_number) {
+ /* we've not yet seen any digits */
+ if (c == '-') {
+ sign = -1;
+ in_number = 1;
+ }
+ else if (c == '+')
+ in_number = 1;
+ else if (isspace((unsigned char)c))
+ ;
+ else
+ break;
+ }
+ else {
break;
+ }
+ ++start;
}
- else {
- break;
- }
- ++start;
- }
- i *= sign;
+ i *= sign;
- return i;
+ return i;
+ }
}