diff options
author | Guy Harris <guy@alum.mit.edu> | 2004-07-16 23:35:46 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2004-07-16 23:35:46 +0000 |
commit | c27d80c9adce0ba21254a63ff644e45d982c87e5 (patch) | |
tree | 94dd014692ef7747c5785d24d581e9da63192f8a /epan/ftypes | |
parent | 1715f98d9c515ce36168e8642e9d561c38b88a22 (diff) |
I'm not sure "strtol()" and "strtoul()" are guaranteed to clear "errno"
on success, so we clear it before calling them.
Assign the value of "strtol()" to a "long" and assign the value of
"strtoul()" to an "unsigned long", as those are the return types for
those functions - "gint32" and "guint32" might not be large enough for
the return value on an LP64 platform.
Check for errno being EINVAL, as that can happen if the number isn't
valid.
Before assigning the value returned by "strtol()" or "strtoul()" to the
final destination, check whether it's in the right range for that
destination.
svn path=/trunk/; revision=11382
Diffstat (limited to 'epan/ftypes')
-rw-r--r-- | epan/ftypes/ftype-integer.c | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/epan/ftypes/ftype-integer.c b/epan/ftypes/ftype-integer.c index 9645a7b41a..7802b7b919 100644 --- a/epan/ftypes/ftype-integer.c +++ b/epan/ftypes/ftype-integer.c @@ -51,11 +51,13 @@ get_integer(fvalue_t *fv) static gboolean val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc) { + unsigned long value; char *endptr; - fv->value.integer = strtoul(s, &endptr, 0); + errno = 0; + value = strtoul(s, &endptr, 0); - if (endptr == s || *endptr != '\0') { + if (errno == EINVAL || endptr == s || *endptr != '\0') { /* This isn't a valid number. */ if (logfunc != NULL) logfunc("\"%s\" is not a valid number.", s); @@ -63,17 +65,31 @@ val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFu } if (errno == ERANGE) { if (logfunc != NULL) { - if (fv->value.integer == ULONG_MAX) { + if (value == ULONG_MAX) { logfunc("\"%s\" causes an integer overflow.", s); } else { + /* + * XXX - can "strtoul()" set errno to + * ERANGE without returning ULONG_MAX? + */ logfunc("\"%s\" is not an integer.", s); } } return FALSE; } + if (value > G_MAXUINT32) { + /* + * Fits in an unsigned long, but not in a guint32 + * (an unsigned long might be 64 bits). + */ + if (logfunc != NULL) + logfunc("\"%s\" causes an integer overflow.", s); + return FALSE; + } + fv->value.integer = value; return TRUE; } |