From 156cb68b3ca73b105423addab966749982618bf1 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Sun, 11 Mar 2007 22:18:04 +0000 Subject: [PATCH] validate string to number conversion --- src/sigar_ptql.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/sigar_ptql.c b/src/sigar_ptql.c index a7894d1d..e4a0b443 100644 --- a/src/sigar_ptql.c +++ b/src/sigar_ptql.c @@ -30,6 +30,9 @@ #define strtoull strtoul /*XXX*/ #endif +#define strtonum_failed(src, ptr) \ + ((src == ptr) || (errno == ERANGE) || (*ptr != '\0')) + /* XXX need more specific errors */ #define SIGAR_PTQL_MALFORMED_QUERY 1 @@ -630,6 +633,7 @@ static int ptql_pid_match(sigar_t *sigar, ptql_branch_t *branch = (ptql_branch_t *)data; sigar_pid_t match_pid; + char *ptr; if (branch->flags == PTQL_PID_FILE) { char buffer[SIGAR_PATH_MAX+1]; @@ -639,7 +643,10 @@ static int ptql_pid_match(sigar_t *sigar, if (status != SIGAR_OK) { return status; } - match_pid = strtoull(buffer, NULL, 10); /*XXX validate*/ + match_pid = strtoull(buffer, &ptr, 10); + if (strtonum_failed(buffer, ptr)) { + return SIGAR_PTQL_MALFORMED_QUERY; + } } else if (branch->flags == PTQL_PID_SERVICE) { #ifdef WIN32 @@ -672,7 +679,7 @@ static int ptql_args_branch_init(ptql_parse_branch_t *parsed, branch->data.ui32 = strtol(parsed->attr, &end, 10); - if (end && *end) { + if (strtonum_failed(parsed->attr, end)) { /* conversion failed */ return SIGAR_PTQL_MALFORMED_QUERY; } @@ -988,9 +995,6 @@ static int ptql_branch_parse(char *query, ptql_parse_branch_t *branch) return SIGAR_OK; } -#define strtonum_failed(src, ptr) \ - ((src == ptr) || (errno == ERANGE) || (*ptr != '\0')) - static int ptql_branch_add(ptql_parse_branch_t *parsed, ptql_branch_list_t *branches) {