Преглед на файлове

* modules/lcr: fixed crash when module initialization failed

* Added checks that shared memory has been really allocated before
  trying to free it.  Fixes crash when during failed module
  initialization.
Juha Heinanen преди 16 години
родител
ревизия
f9477aa2be
променени са 1 файла, в които са добавени 10 реда и са изтрити 10 реда
  1. 10 10
      modules/lcr/lcr_mod.c

+ 10 - 10
modules/lcr/lcr_mod.c

@@ -221,9 +221,11 @@ static int_str defunct_gw_avp;
 static int     lcr_id_avp_type;
 static int     lcr_id_avp_type;
 static int_str lcr_id_avp;
 static int_str lcr_id_avp;
 
 
-struct gw_info **gwtp;	/* Pointer to gw table pointer table */
+/* Pointer to gw table pointer table */
+struct gw_info **gwtp = (struct gw_info **)NULL;
 
 
-struct lcr_info ***lcrtp;  /* Pointer to lcr hash table pointer table */
+/* Pointer to lcr hash table pointer table */
+struct lcr_info ***lcrtp = (struct lcr_info ***)NULL;
 
 
 
 
 /*
 /*
@@ -571,10 +573,6 @@ static int mod_init(void)
     }
     }
     lcr_dbf.close(dbh);
     lcr_dbf.close(dbh);
 
 
-    /* Reset all shm pointers */
-    gwtp = (struct gw_info **)NULL;
-    lcrtp = (struct lcr_info ***)NULL;
-
     /* Allocate gw related shared memory */
     /* Allocate gw related shared memory */
     /* gw table pointer table, index 0 points to temp gw table  */
     /* gw table pointer table, index 0 points to temp gw table  */
     gwtp = (struct gw_info **)shm_malloc(sizeof(struct gw_info *) *
     gwtp = (struct gw_info **)shm_malloc(sizeof(struct gw_info *) *
@@ -583,6 +581,7 @@ static int mod_init(void)
 	LM_ERR("no memory for gw table pointer table\n");
 	LM_ERR("no memory for gw table pointer table\n");
 	goto err;
 	goto err;
     }
     }
+    memset(gwtp, 0, sizeof(struct gw_info *) * (lcr_count + 1));
     /* gw tables */
     /* gw tables */
     for (i = 0; i <= lcr_count; i++) {
     for (i = 0; i <= lcr_count; i++) {
 	gwtp[i] = (struct gw_info *)shm_malloc(sizeof(struct gw_info) *
 	gwtp[i] = (struct gw_info *)shm_malloc(sizeof(struct gw_info) *
@@ -602,6 +601,7 @@ static int mod_init(void)
 	LM_ERR("no memory for lcr hash table pointer table\n");
 	LM_ERR("no memory for lcr hash table pointer table\n");
 	goto err;
 	goto err;
     }
     }
+    memset(lcrtp, 0, sizeof(struct lcr_info **) * (lcr_count + 1));
     /* lcr hash tables */
     /* lcr hash tables */
     /* Last entry in hash table contains list of different prefix lengths */
     /* Last entry in hash table contains list of different prefix lengths */
     for (i = 0; i <= lcr_count; i++) {
     for (i = 0; i <= lcr_count; i++) {
@@ -677,18 +677,18 @@ static void free_shared_memory(void)
 {
 {
     int i;
     int i;
     for (i = 0; i <= lcr_count; i++) {
     for (i = 0; i <= lcr_count; i++) {
-	if (gwtp[i]) {
+	if (gwtp && gwtp[i]) {
 	    shm_free(gwtp[i]);
 	    shm_free(gwtp[i]);
 	    gwtp[i] = 0;
 	    gwtp[i] = 0;
 	}
 	}
-	if (lcrtp[i]) {
+	if (lcrtp && lcrtp[i]) {
 	    lcr_hash_table_contents_free(lcrtp[i]);
 	    lcr_hash_table_contents_free(lcrtp[i]);
 	    shm_free(lcrtp[i]);
 	    shm_free(lcrtp[i]);
 	    lcrtp[i] = 0;
 	    lcrtp[i] = 0;
 	}
 	}
     }
     }
-    shm_free(gwtp);
-    shm_free(lcrtp);
+    if (gwtp) shm_free(gwtp);
+    if (lcrtp) shm_free(lcrtp);
     if (reload_lock) {
     if (reload_lock) {
 	lock_destroy(reload_lock);
 	lock_destroy(reload_lock);
 	lock_dealloc(reload_lock);
 	lock_dealloc(reload_lock);