Explorar o código

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

Ovidiu Sas %!s(int64=12) %!d(string=hai) anos
pai
achega
af7d4496fe
Modificáronse 1 ficheiros con 12 adicións e 2 borrados
  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)
 {
 	long tmp;
+	char* p = NULL;
 
 	if (!_s || !_v) {
 	       LM_ERR("Invalid parameter value\n");
 	       return -1;
 	}
 
-	tmp = strtoul(_s, 0, 10);
+	tmp = strtoul(_s, &p, 10);
 	if ((tmp == ULONG_MAX && errno == ERANGE) || 
 	    (tmp < INT_MIN) || (tmp > UINT_MAX)) {
 		LM_ERR("Value out of range\n");
 		return -1;
 	}
+	if (p && *p != '\0') {
+		LM_ERR("Unexpected characters: [%s]\n", p);
+		return -2;
+	}
 
 	*_v = (int)tmp;
 	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)
 {
 	long long tmp;
+	char* p = NULL;
 
 	if (!_s || !_v) {
 	       LM_ERR("Invalid parameter value\n");
 	       return -1;
 	}
 
-	tmp = strtoll(_s, 0, 10);
+	tmp = strtoll(_s, &p, 10);
 	if (errno == ERANGE) {
 		LM_ERR("Value out of range\n");
 		return -1;
 	}
+	if (p && *p != '\0') {
+		LM_ERR("Unexpected characters: [%s]\n", p);
+		return -2;
+	}
 
 	*_v = tmp;
 	return 0;