Jelajahi Sumber

pipelimit: init cpu load metrics and check returned value of fscanf()

Daniel-Constantin Mierla 1 tahun lalu
induk
melakukan
2da8060f67

+ 10 - 7
src/modules/pipelimit/pipelimit.c

@@ -214,13 +214,16 @@ int get_num_cpus()
 /* not using /proc/loadavg because it only works when our_timer_interval == theirs */
 static int get_cpuload(double *load)
 {
-	static long long o_user, o_nice, o_sys, o_idle, o_iow, o_irq, o_sirq, o_stl;
-	long long n_user, n_nice, n_sys, n_idle, n_iow, n_irq, n_sirq, n_stl;
+	static long long o_user = 0, o_nice = 0, o_sys = 0, o_idle = 0, o_iow = 0,
+					 o_irq = 0, o_sirq = 0, o_stl = 0;
+	long long n_user = 0, n_nice = 0, n_sys = 0, n_idle = 0, n_iow = 0,
+			  n_irq = 0, n_sirq = 0, n_stl = 0;
 	static int first_time = 1;
 	FILE *f = fopen("/proc/stat", "r");
-	double vload;
-	int ncpu;
+	double vload = 0.0;
+	int ncpu = 0;
 	static int errormsg = 0;
+	int n = 0;
 
 	if(!f) {
 		/* Only write this error message five times. Otherwise you will annoy
@@ -231,9 +234,9 @@ static int get_cpuload(double *load)
 		}
 		return -1;
 	}
-	if(fscanf(f, "cpu  %lld%lld%lld%lld%lld%lld%lld%lld", &n_user, &n_nice,
-			   &n_sys, &n_idle, &n_iow, &n_irq, &n_sirq, &n_stl)
-			< 0) {
+	n = fscanf(f, "cpu  %lld%lld%lld%lld%lld%lld%lld%lld", &n_user, &n_nice,
+			&n_sys, &n_idle, &n_iow, &n_irq, &n_sirq, &n_stl);
+	if(n < 8) {
 		LM_ERR("could not parse load information\n");
 		fclose(f);
 		return -1;

+ 5 - 4
src/modules/pipelimit/pl_ht.c

@@ -56,6 +56,7 @@ str_map_t algo_names[] = {
 int pl_init_htable(unsigned int hsize)
 {
 	int i;
+	int j;
 
 	if(_pl_pipes_ht != NULL)
 		return -1;
@@ -80,10 +81,10 @@ int pl_init_htable(unsigned int hsize)
 	for(i = 0; i < _pl_pipes_ht->htsize; i++) {
 		if(lock_init(&_pl_pipes_ht->slots[i].lock) == 0) {
 			LM_ERR("cannot initialize lock[%d]\n", i);
-			i--;
-			while(i >= 0) {
-				lock_destroy(&_pl_pipes_ht->slots[i].lock);
-				i--;
+			j = i - 1;
+			while(j >= 0) {
+				lock_destroy(&_pl_pipes_ht->slots[j].lock);
+				j--;
 			}
 			shm_free(_pl_pipes_ht->slots);
 			shm_free(_pl_pipes_ht);

+ 1 - 1
src/modules/pipelimit/pl_statistics.c

@@ -184,7 +184,7 @@ int get_socket_list_from_proto_and_family(
 		return 0;
 	}
 
-	*ipList = pkg_malloc(numberOfSockets * (num_ip_octets + 1) * sizeof(int));
+	*ipList = pkg_malloc(sizeof(int) * numberOfSockets * (num_ip_octets + 1));
 
 	/* We couldn't allocate memory for the IP List.  So all we can do is
 	 * fail. */