Browse code

nats: make sure pkg & shm memory is allocated; check pointers before freeing memory

Emmanuel Schmidbauer authored on 28/06/2021 11:30:40
Showing 1 changed files
... ...
@@ -245,6 +245,10 @@ int init_worker(
245 245
 		if(s0->url != NULL && num_servers < NATS_MAX_SERVERS) {
246 246
 			len = strlen(s0->url);
247 247
 			sc = shm_malloc(len + 1);
248
+			if(!sc) {
249
+				LM_ERR("no shm memory left\n");
250
+				return -1;
251
+			}
248 252
 			strcpy(sc, s0->url);
249 253
 			sc[len] = '\0';
250 254
 			worker->init_nats_servers[num_servers++] = sc;
... ...
@@ -368,30 +372,38 @@ int nats_destroy_workers()
368 372
 	nats_consumer_worker_t *worker;
369 373
 	for(i = 0; i < _nats_proc_count; i++) {
370 374
 		worker = &nats_workers[i];
371
-		natsSubscription_Unsubscribe(worker->subscription);
372
-		natsSubscription_Destroy(worker->subscription);
373
-		natsConnection_Close(worker->conn);
374
-		natsConnection_Destroy(worker->conn);
375
-		natsOptions_Destroy(worker->opts);
376
-		if(worker->uvLoop != NULL) {
377
-			uv_loop_close(worker->uvLoop);
378
-		}
379
-		nats_Close();
380
-		if(worker->subject != NULL) {
381
-			shm_free(worker->subject);
382
-		}
383
-		if(worker->queue_group != NULL) {
384
-			shm_free(worker->queue_group);
385
-		}
386
-		if(worker->on_message != NULL) {
387
-			shm_free(worker->on_message);
388
-		}
389
-		for(s = 0; s < NATS_MAX_SERVERS; s++) {
390
-			if(worker->init_nats_servers[s]) {
391
-				shm_free(worker->init_nats_servers[s]);
375
+		if(worker != NULL) {
376
+			if(worker->subscription != NULL) {
377
+				natsSubscription_Unsubscribe(worker->subscription);
378
+				natsSubscription_Destroy(worker->subscription);
379
+			}
380
+			if(worker->conn != NULL) {
381
+				natsConnection_Close(worker->conn);
382
+				natsConnection_Destroy(worker->conn);
383
+			}
384
+			if(worker->opts != NULL) {
385
+				natsOptions_Destroy(worker->opts);
386
+			}
387
+			if(worker->uvLoop != NULL) {
388
+				uv_loop_close(worker->uvLoop);
389
+			}
390
+			nats_Close();
391
+			if(worker->subject != NULL) {
392
+				shm_free(worker->subject);
392 393
 			}
394
+			if(worker->queue_group != NULL) {
395
+				shm_free(worker->queue_group);
396
+			}
397
+			if(worker->on_message != NULL) {
398
+				shm_free(worker->on_message);
399
+			}
400
+			for(s = 0; s < NATS_MAX_SERVERS; s++) {
401
+				if(worker->init_nats_servers[s]) {
402
+					shm_free(worker->init_nats_servers[s]);
403
+				}
404
+			}
405
+			shm_free(worker);
393 406
 		}
394
-		shm_free(worker);
395 407
 	}
396 408
 	return 0;
397 409
 }
... ...
@@ -420,6 +432,10 @@ int _init_nats_server_url_add(modparam_t type, void *val)
420 432
 		return -1;
421 433
 	}
422 434
 	value = pkg_malloc(len + 1);
435
+	if(!value) {
436
+		LM_ERR("no pkg memory left\n");
437
+		return -1;
438
+	}
423 439
 	strcpy(value, url);
424 440
 	value[len] = '\0';
425 441
 	if(init_nats_server_url_add(url) < 0) {
... ...
@@ -434,6 +450,11 @@ int _init_nats_sub_add(modparam_t type, void *val)
434 450
 	char *sub = (char *)val;
435 451
 	int len = strlen(sub);
436 452
 	char *s = pkg_malloc(len + 1);
453
+	if(!s) {
454
+		LM_ERR("no pkg memory left\n");
455
+		return -1;
456
+	}
457
+
437 458
 	strcpy(s, sub);
438 459
 	s[len] = '\0';
439 460
 	if(init_nats_sub_add(s) < 0) {
... ...
@@ -529,6 +550,10 @@ int init_nats_sub_add(char *sc)
529 550
 
530 551
 	len = strlen(sc);
531 552
 	s = pkg_malloc(len + 1);
553
+	if(!s) {
554
+		LM_ERR("no pkg memory left\n");
555
+		return -1;
556
+	}
532 557
 	strcpy(s, sc);
533 558
 	s[len] = '\0';
534 559