summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2010-04-07 21:18:17 +0000
committersmatz <smatz@openttd.org>2010-04-07 21:18:17 +0000
commitdea165c3be592124ce450709aa41fa3db6a9ea2c (patch)
tree55840228c32fac588095f77f02438dbe86432b0a
parent5eb1e3d033f57fbcb4a696fbfa490a801a8604b2 (diff)
downloadopenttd-dea165c3be592124ce450709aa41fa3db6a9ea2c.tar.xz
(svn r19578) -Codechange: do not accept commas at invalid places in ParseIntList()
-rw-r--r--src/settings.cpp39
1 files changed, 29 insertions, 10 deletions
diff --git a/src/settings.cpp b/src/settings.cpp
index dd9c57c7b..a50c240c0 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -158,19 +158,38 @@ static uint32 LookupManyOfMany(const char *many, const char *str)
* @return returns the number of items found, or -1 on an error */
int ParseIntList(const char *p, int *items, int maxitems)
{
- int n = 0, v;
- char *end;
+ int n = 0; // number of items read so far
+ bool comma = false; // do we accept comma?
+
+ while (*p != '\0') {
+ switch (*p) {
+ case ',':
+ /* Do not accept multiple commas between numbers */
+ if (!comma) return -1;
+ comma = false;
+ /* FALL THROUGH */
+ case ' ':
+ p++;
+ break;
- for (;;) {
- v = strtol(p, &end, 0);
- if (p == end || n == maxitems) return -1;
- p = end;
- items[n++] = v;
- if (*p == '\0') break;
- if (*p != ',' && *p != ' ') return -1;
- p++;
+ default: {
+ if (n == maxitems) return -1; // we don't accept that many numbers
+ char *end;
+ long v = strtol(p, &end, 0);
+ if (p == end) return -1; // invalid character (not a number)
+ if (sizeof(int) < sizeof(long)) v = ClampToI32(v);
+ items[n++] = v;
+ p = end; // first non-number
+ comma = true; // we accept comma now
+ break;
+ }
+ }
}
+ /* If we have read comma but no number after it, fail.
+ * We have read comma when (n != 0) and comma is not allowed */
+ if (n != 0 && !comma) return -1;
+
return n;
}