[Asterisk-code-review] main/app: Fix leaks. (asterisk[15])

Jenkins2 asteriskteam at digium.com
Tue Dec 19 14:16:36 CST 2017


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/7654 )

Change subject: main/app: Fix leaks.
......................................................................

main/app: Fix leaks.

* ast_linear_stream would leak a file descriptor if it failed to allocate
  lin.
* ast_control_tone leaked zone and ts if ast_playtones_start failed.

Additionally added whitespace to ast_linear_stream, pulled assignments
out of conditionals for improved readability.

Change-Id: I6d1a10cf9161b1529d939b9b2d63ea36d395b657
---
M main/app.c
1 file changed, 22 insertions(+), 10 deletions(-)

Approvals:
  George Joseph: Looks good to me, but someone else must approve
  Benjamin Keith Ford: Looks good to me, but someone else must approve
  Sean Bright: Looks good to me, approved
  Jenkins2: Approved for Submit



diff --git a/main/app.c b/main/app.c
index f35c4b7..bdf243c 100644
--- a/main/app.c
+++ b/main/app.c
@@ -1027,30 +1027,42 @@
 {
 	struct linear_state *lin;
 	char tmpf[256];
-	int res = -1;
 	int autoclose = 0;
+
 	if (fd < 0) {
 		if (ast_strlen_zero(filename)) {
 			return -1;
 		}
+
 		autoclose = 1;
+
 		if (filename[0] == '/') {
 			ast_copy_string(tmpf, filename, sizeof(tmpf));
 		} else {
 			snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", ast_config_AST_DATA_DIR, "sounds", filename);
 		}
-		if ((fd = open(tmpf, O_RDONLY)) < 0) {
+
+		fd = open(tmpf, O_RDONLY);
+		if (fd < 0) {
 			ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", tmpf, strerror(errno));
 			return -1;
 		}
 	}
-	if ((lin = ast_calloc(1, sizeof(*lin)))) {
-		lin->fd = fd;
-		lin->allowoverride = allowoverride;
-		lin->autoclose = autoclose;
-		res = ast_activate_generator(chan, &linearstream, lin);
+
+	lin = ast_calloc(1, sizeof(*lin));
+	if (!lin) {
+		if (autoclose) {
+			close(fd);
+		}
+
+		return -1;
 	}
-	return res;
+
+	lin->fd = fd;
+	lin->allowoverride = allowoverride;
+	lin->autoclose = autoclose;
+
+	return ast_activate_generator(chan, &linearstream, lin);
 }
 
 static int control_streamfile(struct ast_channel *chan,
@@ -1349,10 +1361,10 @@
 	ts = ast_get_indication_tone(zone ? zone : ast_channel_zone(chan), tone_indication);
 
 	if (ast_playtones_start(chan, 0, ts ? ts->data : tone_indication, 0)) {
-		return -1;
+		res = -1;
 	}
 
-	for (;;) {
+	while (!res) {
 		struct ast_frame *fr;
 
 		if (ast_waitfor(chan, -1) < 0) {

-- 
To view, visit https://gerrit.asterisk.org/7654
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 15
Gerrit-MessageType: merged
Gerrit-Change-Id: I6d1a10cf9161b1529d939b9b2d63ea36d395b657
Gerrit-Change-Number: 7654
Gerrit-PatchSet: 1
Gerrit-Owner: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: Benjamin Keith Ford <bford at digium.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Sean Bright <sean.bright at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20171219/9b72e00d/attachment.html>


More information about the asterisk-code-review mailing list