diff mbox

[ndctl] ndctl: fix static analysis report, unchecked sscanf

Message ID 150696061891.21689.14927545469089860105.stgit@dwillia2-desk3.amr.corp.intel.com (mailing list archive)
State Accepted
Commit 9dc0d660414d
Headers show

Commit Message

Dan Williams Oct. 2, 2017, 4:10 p.m. UTC
Static analysis flags some occasions of unchecked sscanf return value.

Cc: Yasunori Goto <y-goto@jp.fujitsu.com>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 util/json.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Gotou, Yasunori/五島 康文 Oct. 3, 2017, 1:52 a.m. UTC | #1
> Static analysis flags some occasions of unchecked sscanf return value.
> 
> Cc: Yasunori Goto <y-goto@jp.fujitsu.com>
> Signed-off-by: Dan Williams <dan.j.williams@intel.com>
> ---
>  util/json.c |    7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/util/json.c b/util/json.c
> index d1ee351546b9..0c92c3b49c71 100644
> --- a/util/json.c
> +++ b/util/json.c
> @@ -374,11 +374,12 @@ static int compare_dimm_number(const void *p1, const void *p2)
>  	const char *dimm2_name = ndctl_dimm_get_devname(dimm2);
>  	int num1, num2;
>  
> -	sscanf(dimm1_name, "nmem%d", &num1);
> -	sscanf(dimm2_name, "nmem%d", &num2);
> +	if (sscanf(dimm1_name, "nmem%d", &num1) != 1)
> +		num1 = 0;
> +	if (sscanf(dimm2_name, "nmem%d", &num2) != 1)
> +		num2 = 0;
>  
>  	return num1 - num2;
> -
>  }
>  
>  static struct json_object *badblocks_to_jdimms(struct ndctl_region *region,
> 
> 

Good catch. Thanks!


Reviewed-by: Yasunori Goto<y-goto@jp.fujitsu.com>
diff mbox

Patch

diff --git a/util/json.c b/util/json.c
index d1ee351546b9..0c92c3b49c71 100644
--- a/util/json.c
+++ b/util/json.c
@@ -374,11 +374,12 @@  static int compare_dimm_number(const void *p1, const void *p2)
 	const char *dimm2_name = ndctl_dimm_get_devname(dimm2);
 	int num1, num2;
 
-	sscanf(dimm1_name, "nmem%d", &num1);
-	sscanf(dimm2_name, "nmem%d", &num2);
+	if (sscanf(dimm1_name, "nmem%d", &num1) != 1)
+		num1 = 0;
+	if (sscanf(dimm2_name, "nmem%d", &num2) != 1)
+		num2 = 0;
 
 	return num1 - num2;
-
 }
 
 static struct json_object *badblocks_to_jdimms(struct ndctl_region *region,