Browse Source

lib/srdb1: while converting strings to int/bigint check for invalid characters

Ovidiu Sas 12 years ago
parent
commit
af7d4496fe
1 changed files with 12 additions and 2 deletions
  1. 12 2
      lib/srdb1/db_ut.c

+ 12 - 2
lib/srdb1/db_ut.c

@@ -73,18 +73,23 @@
 inline int db_str2int(const char* _s, int* _v)
 inline int db_str2int(const char* _s, int* _v)
 {
 {
 	long tmp;
 	long tmp;
+	char* p = NULL;
 
 
 	if (!_s || !_v) {
 	if (!_s || !_v) {
 	       LM_ERR("Invalid parameter value\n");
 	       LM_ERR("Invalid parameter value\n");
 	       return -1;
 	       return -1;
 	}
 	}
 
 
-	tmp = strtoul(_s, 0, 10);
+	tmp = strtoul(_s, &p, 10);
 	if ((tmp == ULONG_MAX && errno == ERANGE) || 
 	if ((tmp == ULONG_MAX && errno == ERANGE) || 
 	    (tmp < INT_MIN) || (tmp > UINT_MAX)) {
 	    (tmp < INT_MIN) || (tmp > UINT_MAX)) {
 		LM_ERR("Value out of range\n");
 		LM_ERR("Value out of range\n");
 		return -1;
 		return -1;
 	}
 	}
+	if (p && *p != '\0') {
+		LM_ERR("Unexpected characters: [%s]\n", p);
+		return -2;
+	}
 
 
 	*_v = (int)tmp;
 	*_v = (int)tmp;
 	return 0;
 	return 0;
@@ -94,17 +99,22 @@ inline int db_str2int(const char* _s, int* _v)
 inline int db_str2longlong(const char* _s, long long * _v)
 inline int db_str2longlong(const char* _s, long long * _v)
 {
 {
 	long long tmp;
 	long long tmp;
+	char* p = NULL;
 
 
 	if (!_s || !_v) {
 	if (!_s || !_v) {
 	       LM_ERR("Invalid parameter value\n");
 	       LM_ERR("Invalid parameter value\n");
 	       return -1;
 	       return -1;
 	}
 	}
 
 
-	tmp = strtoll(_s, 0, 10);
+	tmp = strtoll(_s, &p, 10);
 	if (errno == ERANGE) {
 	if (errno == ERANGE) {
 		LM_ERR("Value out of range\n");
 		LM_ERR("Value out of range\n");
 		return -1;
 		return -1;
 	}
 	}
+	if (p && *p != '\0') {
+		LM_ERR("Unexpected characters: [%s]\n", p);
+		return -2;
+	}
 
 
 	*_v = tmp;
 	*_v = tmp;
 	return 0;
 	return 0;