Skip to contentSkip to conversation

ndctl: Fix the NDCTL_TIMEOUT environment variable parsing

PATCH Superseded Shivaprasad G Bhat 2 participants 2 messages Sep 21, 2022 – Sep 28, 2022 libnvdimm: non-volatile memory device

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ndctl: Fix the NDCTL_TIMEOUT environment variable parsing

The strtoul(x, y, size) returns empty string on y when the x is "only" number with no other suffix strings. The code is checking if !null instead of comparing with empty string.

---
 ndctl/lib/libndctl.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ndctl/lib/libndctl.c +1 -1
index ad54f06..b0287e8 100644
@@ -334,7 +334,7 @@ NDCTL_EXPORT int ndctl_new(struct ndctl_ctx **ctx)
 		char *end;
 
 		tmo = strtoul(env, &end, 0);
-		if (tmo < ULONG_MAX && !end)
+		if (tmo < ULONG_MAX && strcmp(end, "") == 0)
 			c->timeout = tmo;
 		dbg(c, "timeout = %ld\n", tmo);
 	}
#1 lore raw

Hi Shiva,

Thanks for fixing this. Minor review comment below:

Quoted 14 lines · Shivaprasad G Bhat

The strtoul(x, y, size) returns empty string on y when the x is "only" number with no other suffix strings. The code is checking if !null instead of comparing with empty string.

---
 ndctl/lib/libndctl.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ndctl/lib/libndctl.c +0 -0
index ad54f06..b0287e8 100644

ndctl/lib/libndctl.c NDCTL_EXPORT int ndctl_new(struct ndctl_ctx **ctx) · line 337

Quoted 5 lines
ndctl/lib/libndctl.c +1 -1
@@ -334,7 +334,7 @@ NDCTL_EXPORT int ndctl_new(struct ndctl_ctx **ctx)
 		char *end;
 
 		tmo = strtoul(env, &end, 0);
-		if (tmo < ULONG_MAX && !end)
+		if (tmo < ULONG_MAX && strcmp(end, "") == 0)

Using strcmp would be better avoided in new code. Instead you can check for the valid string to parse in strtoull() with simply checking against *end == '\0' or !*end.

Quote for STRTOUL(3):

"if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid."

Quoted 7 lines
ndctl/lib/libndctl.c +0 -0
 			c->timeout = tmo;
 		dbg(c, "timeout = %ld\n", tmo);
 	}
Signature · 2 lines
Cheers
~ Vaibhav