Patchwork [14/15] libnsdb: Stricter type checking

login
register
mail settings
Submitter Chuck Lever
Date Oct. 25, 2011, 4:50 p.m.
Message ID <20111025165026.18584.42748.stgit@seurat.1015granger.net>
Download mbox | patch
Permalink /patch/121755/
State Accepted
Headers show

Comments

Chuck Lever - Oct. 25, 2011, 4:50 p.m.
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(-)

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;