diff mbox

[4/7] libnsdb: Stricter type checking

Message ID 20111103152845.2445.24530.stgit@degas.1015granger.net
State Accepted
Headers show

Commit Message

Chuck Lever Nov. 3, 2011, 3:28 p.m. UTC
For integer LDAP values, replace atoi(3) with something more robust.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---

 src/libnsdb/ldap.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
diff mbox

Patch

diff --git a/src/libnsdb/ldap.c b/src/libnsdb/ldap.c
index acd9672..618b62a 100644
--- a/src/libnsdb/ldap.c
+++ b/src/libnsdb/ldap.c
@@ -31,6 +31,7 @@ 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include <memory.h>
 #include <signal.h>
 #include <termios.h>
@@ -216,14 +217,24 @@  nsdb_parse_singlevalue_bool(char *attr, struct berval **values, _Bool *result)
 FedFsStatus
 nsdb_parse_singlevalue_int(char *attr, struct berval **values, int *result)
 {
+	char *endptr;
+	long tmp;
+
 	if (values[1] != NULL) {
 		xlog(L_ERROR, "%s: Expecting only one value for attribute %s",
 			__func__, attr);
 		return FEDFS_ERR_NSDB_RESPONSE;
 	}
 
-	/* XXX: Better value type checking, please */
-	*result = atoi(values[0]->bv_val);
+	errno = 0;
+	tmp = strtol(values[0]->bv_val, &endptr, 10);
+	if (errno != 0 || *endptr != '\0' || tmp < INT_MIN || tmp > INT_MAX) {
+		xlog(D_CALL, "%s: Attribute %s contains out-of-range value %.*s",
+			__func__, attr, values[0]->bv_len, values[0]->bv_val);
+		return FEDFS_ERR_NSDB_RESPONSE;
+	}
+
+	*result = (int)tmp;
 	xlog(D_CALL, "%s: Attribute %s contains value %d",
 		__func__, attr, *result);
 	return FEDFS_OK;