[ndctl] libndctl: Fix a potentially non NUL-terminated string operation
diff mbox series

Message ID 20190920180608.8662-1-vishal.l.verma@intel.com
State Accepted
Commit 3dbfbd85bda272b4531461b1ebe9155224759fa9
Headers show
Series
  • [ndctl] libndctl: Fix a potentially non NUL-terminated string operation
Related show

Commit Message

Verma, Vishal L Sept. 20, 2019, 6:06 p.m. UTC
Static analysis warns that pread() doesn't NUL-terminate buffers, and
that we shouldn't pass it directly to strcmp. The sysfs string should
normally have the right termination, but for correctness in the library,
we shouldn't rely on that. Replace the strcmp() calls in question with
an explicit strncmp().

Fixes: 3c0c7db045ec ("ndctl: add a wait-overwrite command")
Cc: Dave Jiang <dave.jiang@intel.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 ndctl/lib/dimm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Dave Jiang Sept. 20, 2019, 6:14 p.m. UTC | #1
On 9/20/19 11:06 AM, Vishal Verma wrote:
> Static analysis warns that pread() doesn't NUL-terminate buffers, and
> that we shouldn't pass it directly to strcmp. The sysfs string should
> normally have the right termination, but for correctness in the library,
> we shouldn't rely on that. Replace the strcmp() calls in question with
> an explicit strncmp().
> 
> Fixes: 3c0c7db045ec ("ndctl: add a wait-overwrite command")
> Cc: Dave Jiang <dave.jiang@intel.com>
> Cc: Dan Williams <dan.j.williams@intel.com>
> Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>

Reviewed-by: Dave Jiang <dave.jiang@intel.com>

> ---
>  ndctl/lib/dimm.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/ndctl/lib/dimm.c b/ndctl/lib/dimm.c
> index 2f145be..17344f0 100644
> --- a/ndctl/lib/dimm.c
> +++ b/ndctl/lib/dimm.c
> @@ -825,7 +825,7 @@ NDCTL_EXPORT int ndctl_dimm_wait_overwrite(struct ndctl_dimm *dimm)
>  			break;
>  		}
>  
> -		if (strcmp(buf, "overwrite") == 0) {
> +		if (strncmp(buf, "overwrite", 9) == 0) {
>  			rc = poll(&fds, 1, -1);
>  			if (rc < 0) {
>  				rc = -errno;
> @@ -839,7 +839,7 @@ NDCTL_EXPORT int ndctl_dimm_wait_overwrite(struct ndctl_dimm *dimm)
>  			}
>  			fds.revents = 0;
>  		} else {
> -			if (strcmp(buf, "disabled") == 0)
> +			if (strncmp(buf, "disabled", 8) == 0)
>  				rc = 1;
>  			break;
>  		}
>

Patch
diff mbox series

diff --git a/ndctl/lib/dimm.c b/ndctl/lib/dimm.c
index 2f145be..17344f0 100644
--- a/ndctl/lib/dimm.c
+++ b/ndctl/lib/dimm.c
@@ -825,7 +825,7 @@  NDCTL_EXPORT int ndctl_dimm_wait_overwrite(struct ndctl_dimm *dimm)
 			break;
 		}
 
-		if (strcmp(buf, "overwrite") == 0) {
+		if (strncmp(buf, "overwrite", 9) == 0) {
 			rc = poll(&fds, 1, -1);
 			if (rc < 0) {
 				rc = -errno;
@@ -839,7 +839,7 @@  NDCTL_EXPORT int ndctl_dimm_wait_overwrite(struct ndctl_dimm *dimm)
 			}
 			fds.revents = 0;
 		} else {
-			if (strcmp(buf, "disabled") == 0)
+			if (strncmp(buf, "disabled", 8) == 0)
 				rc = 1;
 			break;
 		}