Browse Source

dispatcher: add missing memory allocation checks, convert to memory log macros

Pantelis Kolatsis 2 years ago
parent
commit
6aee3bd7c8

+ 16 - 10
src/modules/dispatcher/dispatch.c

@@ -142,7 +142,7 @@ int ds_ping_active_init(void)
 		return 0;
 	_ds_ping_active = (int *)shm_malloc(sizeof(int));
 	if(_ds_ping_active == NULL) {
-		LM_ERR("no more shared memory\n");
+		SHM_MEM_ERROR;
 		return -1;
 	}
 	*_ds_ping_active = 1;
@@ -258,7 +258,7 @@ int ds_init_data(void)
 
 	ds_lists = (ds_set_t **)shm_malloc(2 * sizeof(ds_set_t *));
 	if(!ds_lists) {
-		LM_ERR("Out of memory\n");
+		SHM_MEM_ERROR;
 		return -1;
 	}
 	memset(ds_lists, 0, 2 * sizeof(ds_set_t *));
@@ -266,7 +266,8 @@ int ds_init_data(void)
 
 	p = (int *)shm_malloc(3 * sizeof(int));
 	if(!p) {
-		LM_ERR("Out of memory\n");
+		shm_free(ds_lists);
+		SHM_MEM_ERROR;
 		return -1;
 	}
 	memset(p, 0, 3 * sizeof(int));
@@ -305,7 +306,7 @@ int ds_set_attrs(ds_dest_t *dest, str *vattrs)
 	/* clone in shm */
 	dest->attrs.body.s = (char *)shm_malloc(sattrs.len + 1);
 	if(dest->attrs.body.s == NULL) {
-		LM_ERR("no more shm\n");
+		SHM_MEM_ERROR;
 		return -1;
 	}
 	memcpy(dest->attrs.body.s, sattrs.s, sattrs.len);
@@ -424,14 +425,14 @@ ds_dest_t *pack_dest(str iuri, int flags, int priority, str *attrs, int dload)
 	/* store uri */
 	dp = (ds_dest_t *)shm_malloc(sizeof(ds_dest_t));
 	if(dp == NULL) {
-		LM_ERR("no more memory!\n");
+		SHM_MEM_ERROR;
 		goto err;
 	}
 	memset(dp, 0, sizeof(ds_dest_t));
 
 	dp->uri.s = (char *)shm_malloc((uri.len + 1) * sizeof(char));
 	if(dp->uri.s == NULL) {
-		LM_ERR("no more memory!\n");
+		SHM_MEM_ERROR;
 		goto err;
 	}
 	strncpy(dp->uri.s, uri.s, uri.len);
@@ -634,12 +635,12 @@ int dp_init_relative_weights(ds_set_t *dset)
 	/* local copy to avoid synchronization problems */
 	ds_dests_flags = pkg_malloc(sizeof(int) * dset->nr);
 	if(ds_dests_flags == NULL) {
-		LM_ERR("no more pkg\n");
+		PKG_MEM_ERROR;
 		return -1;
 	}
 	ds_dests_rweights = pkg_malloc(sizeof(int) * dset->nr);
 	if(ds_dests_rweights == NULL) {
-		LM_ERR("no more pkg\n");
+		PKG_MEM_ERROR;
 		pkg_free(ds_dests_flags);
 		return -1;
 	}
@@ -770,7 +771,7 @@ int reindex_dests(ds_set_t *node)
 
 	dp0 = (ds_dest_t *)shm_malloc(node->nr * sizeof(ds_dest_t));
 	if(dp0 == NULL) {
-		LM_ERR("no more memory!\n");
+		SHM_MEM_ERROR;
 		goto err1;
 	}
 	memset(dp0, 0, node->nr * sizeof(ds_dest_t));
@@ -2232,7 +2233,7 @@ int ds_manage_route_algo13(ds_set_t *idx, ds_select_state_t *rstate) {
 	int active_priority = 0;
 	sorted_ds_t *ds_sorted = pkg_malloc(sizeof(sorted_ds_t) * idx->nr);
 	if(ds_sorted == NULL) {
-		LM_ERR("no more pkg\n");
+		PKG_MEM_ERROR;
 		return -1;
 	}
 
@@ -4038,6 +4039,11 @@ ds_set_t *ds_avl_insert(ds_set_t **root, int id, int *setn)
 	}
 	if(!node) {
 		node = shm_malloc(sizeof(ds_set_t));
+		if(!(node))
+		{
+			SHM_MEM_ERROR;
+			return NULL;
+		}
 		memset(node, 0, sizeof(ds_set_t));
 		node->id = id;
 		node->longer = AVL_NEITHER;

+ 14 - 1
src/modules/dispatcher/dispatcher.c

@@ -360,8 +360,19 @@ static int mod_init(void)
 
 	/* Initialize the counter */
 	ds_ping_reply_codes = (int **)shm_malloc(sizeof(unsigned int *));
+	if(!(ds_ping_reply_codes))
+	{
+		SHM_MEM_ERROR;
+		return -1;
+	}
 	*ds_ping_reply_codes = 0;
 	ds_ping_reply_codes_cnt = (int *)shm_malloc(sizeof(int));
+	if(!(ds_ping_reply_codes_cnt))
+	{
+		shm_free(ds_ping_reply_codes);
+		SHM_MEM_ERROR;
+		return -1;
+	}
 	*ds_ping_reply_codes_cnt = 0;
 	if(ds_ping_reply_codes_str.s) {
 		cfg_get(dispatcher, dispatcher_cfg, ds_ping_reply_codes_str) =
@@ -508,6 +519,8 @@ static int mod_init(void)
 
 	ds_rpc_reload_time = shm_malloc(sizeof(time_t));
 	if(ds_rpc_reload_time == NULL) {
+		shm_free(ds_ping_reply_codes);
+		shm_free(ds_ping_reply_codes_cnt);
 		SHM_MEM_ERROR;
 		return -1;
 	}
@@ -1187,7 +1200,7 @@ static int ds_parse_reply_codes()
 		ds_ping_reply_codes_new = (int *)shm_malloc(list_size * sizeof(int));
 		if(ds_ping_reply_codes_new == NULL) {
 			free_params(params_list);
-			LM_ERR("no more memory\n");
+			SHM_MEM_ERROR;
 			return -1;
 		}
 

+ 3 - 3
src/modules/dispatcher/ds_ht.c

@@ -44,7 +44,7 @@ ds_cell_t *ds_cell_new(str *cid, str *duid, int dset, unsigned int cellid)
 
 	cell = (ds_cell_t *)shm_malloc(msize);
 	if(cell == NULL) {
-		LM_ERR("no more shm\n");
+		SHM_MEM_ERROR;
 		return NULL;
 	}
 
@@ -79,7 +79,7 @@ ds_ht_t *ds_ht_init(unsigned int htsize, int expire, int initexpire)
 
 	dsht = (ds_ht_t *)shm_malloc(sizeof(ds_ht_t));
 	if(dsht == NULL) {
-		LM_ERR("no more shm\n");
+		SHM_MEM_ERROR;
 		return NULL;
 	}
 	memset(dsht, 0, sizeof(ds_ht_t));
@@ -89,7 +89,7 @@ ds_ht_t *ds_ht_init(unsigned int htsize, int expire, int initexpire)
 
 	dsht->entries = (ds_entry_t *)shm_malloc(dsht->htsize * sizeof(ds_entry_t));
 	if(dsht->entries == NULL) {
-		LM_ERR("no more shm.\n");
+		SHM_MEM_ERROR;
 		shm_free(dsht);
 		dsht = NULL;
 		return NULL;