diff --git a/src/driver/toks_driver_ctx.c b/src/driver/toks_driver_ctx.c index 0567be9..d8ee050 100644 --- a/src/driver/toks_driver_ctx.c +++ b/src/driver/toks_driver_ctx.c @@ -90,6 +90,29 @@ static int toks_driver_usage( return TOKS_USAGE; } +static int64_t toks_arg_to_int64(struct argv_entry * entry) +{ + int64_t ret; + const char * ch; + + for (ret=0, ch=entry->arg; *ch && (ret>=0); ch++) { + if ((*ch < '0') || (*ch >'9')) + return (-1); + + ret *= 10; + ret += (*ch - '0'); + } + + return ret; +} + +static int32_t toks_arg_to_int32(struct argv_entry * entry) +{ + int64_t ret = toks_arg_to_int64(entry); + return (ret >= 0) && (ret <= 0x7fffffff) + ? ret : (-1); +} + static int32_t toks_query_performance_counters_failover(nt_filetime * ticks) { (void)ticks; @@ -259,7 +282,6 @@ int toks_get_driver_ctx( nt_guid svcguid; const char * program; const char * refstr; - const char * ch; int ntokens; int32_t tokpid; int32_t tsyspid; @@ -337,46 +359,21 @@ int toks_get_driver_ctx( break; case TAG_PID: - pid = entry; - - for (tokpid=0, ch=entry->arg; *ch && (tokpid>=0); ch++) - if ((*ch < '0') || (*ch >'9')) - tokpid = -1; - else - tokpid = tokpid * 10 + (*ch - '0'); + tokpid = toks_arg_to_int32((pid=entry)); break; case TAG_SYSPID: - syspid = entry; - - for (tsyspid=0, ch=entry->arg; *ch && (tsyspid>=0); ch++) - if ((*ch < '0') || (*ch >'9')) - tsyspid = -1; - else - tsyspid = tsyspid * 10 + (*ch - '0'); + tsyspid = toks_arg_to_int32((syspid=entry)); break; case TAG_TIMEOUT: - msecs = entry; - - for (timeout=0, ch=entry->arg; *ch && (timeout>=0); ch++) - if ((*ch < '0') || (*ch >'9')) - timeout = -2; - else if ((ch - entry->arg) > 15) - timeout = -2; - else - timeout = timeout * 10 + (*ch - '0'); - + timeout = toks_arg_to_int64((msecs=entry)); + timeout = (timeout < 0) ? (-2) : timeout; break; case TAG_TOKENS: - for (ntokens=0, ch=entry->arg; *ch && (ntokens>=0); ch++) - if ((*ch < '0') || (*ch >'9')) - ntokens = -1; - else if (ntokens >= 1000) - ntokens = -1; - else - ntokens = ntokens * 10 + (*ch - '0'); + ntokens = toks_arg_to_int32(entry); + ntokens = (ntokens > 9999) ? (-1) : ntokens; break; case TAG_CONNECT: