From: Richard W.M. Jones Date: Tue, 23 Aug 2011 15:12:27 +0000 (+0100) Subject: Coverity: Check return value from sscanf in bindtests calls. X-Git-Tag: 1.10.9~22 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=403af82609a30fff9bcd61d027c67de20f220a57;p=libguestfs.git Coverity: Check return value from sscanf in bindtests calls. (cherry picked from commit 4c76aaa391d591c5917a06b0277fb9eb7d6d790f) --- diff --git a/generator/generator_bindtests.ml b/generator/generator_bindtests.ml index ebe2e24..859f189 100644 --- a/generator/generator_bindtests.ml +++ b/generator/generator_bindtests.ml @@ -109,11 +109,17 @@ print_strings (char *const *argv) pr " return 0;\n" | RInt _ -> pr " int r;\n"; - pr " sscanf (val, \"%%d\", &r);\n"; + pr " if (sscanf (val, \"%%d\", &r) != 1) {\n"; + pr " error (g, \"%%s: expecting int argument\", \"%s\");\n" name; + pr " return -1;\n"; + pr " }\n"; pr " return r;\n" | RInt64 _ -> pr " int64_t r;\n"; - pr " sscanf (val, \"%%\" SCNi64, &r);\n"; + pr " if (sscanf (val, \"%%\" SCNi64, &r) != 1) {\n"; + pr " error (g, \"%%s: expecting int64 argument\", \"%s\");\n" name; + pr " return -1;\n"; + pr " }\n"; pr " return r;\n" | RBool _ -> pr " return STREQ (val, \"true\");\n" @@ -129,7 +135,10 @@ print_strings (char *const *argv) | RStringList _ -> pr " char **strs;\n"; pr " int n, i;\n"; - pr " sscanf (val, \"%%d\", &n);\n"; + pr " if (sscanf (val, \"%%d\", &n) != 1) {\n"; + pr " error (g, \"%%s: expecting int argument\", \"%s\");\n" name; + pr " return NULL;\n"; + pr " }\n"; pr " strs = safe_malloc (g, (n+1) * sizeof (char *));\n"; pr " for (i = 0; i < n; ++i) {\n"; pr " strs[i] = safe_malloc (g, 16);\n"; @@ -143,14 +152,22 @@ print_strings (char *const *argv) pr " return r;\n" | RStructList (_, typ) -> pr " struct guestfs_%s_list *r;\n" typ; + pr " uint32_t len;\n"; + pr " if (sscanf (val, \"%%\" SCNu32, &len) != 1) {\n"; + pr " error (g, \"%%s: expecting uint32 argument\", \"%s\");\n" name; + pr " return NULL;\n"; + pr " }\n"; pr " r = safe_calloc (g, sizeof *r, 1);\n"; - pr " sscanf (val, \"%%d\", &r->len);\n"; + pr " r->len = len;\n"; pr " r->val = safe_calloc (g, r->len, sizeof *r->val);\n"; pr " return r;\n" | RHashtable _ -> pr " char **strs;\n"; pr " int n, i;\n"; - pr " sscanf (val, \"%%d\", &n);\n"; + pr " if (sscanf (val, \"%%d\", &n) != -1) {\n"; + pr " error (g, \"%%s: expecting int argument\", \"%s\");\n" name; + pr " return NULL;\n"; + pr " }\n"; pr " strs = safe_malloc (g, (n*2+1) * sizeof (*strs));\n"; pr " for (i = 0; i < n; ++i) {\n"; pr " strs[i*2] = safe_malloc (g, 16);\n";