[Asterisk-code-review] cdr_pgsql: fix error in connection string (...asterisk[master])

Friendly Automation asteriskteam at digium.com
Tue Jun 11 08:03:09 CDT 2019


Friendly Automation has submitted this change and it was merged. ( https://gerrit.asterisk.org/c/asterisk/+/11443 )

Change subject: cdr_pgsql: fix error in connection string
......................................................................

cdr_pgsql: fix error in connection string

Fixes an error occurring in function pgsql_reconnect() caused when value of
hostname is blank. Which in turn will cause the connection string to look
like this: "host= port=xx", which creates a sintax error. This fix now checks
if the corresponding values for host, port, dbname, and user are blank. Note
that since this is a reconnect function the database library will replace any
missing value pairs with default ones.

ASTERISK-28435

Change-Id: I0a921f99bbd265768be08cd492f04b30855b8423
---
M cdr/cdr_pgsql.c
1 file changed, 19 insertions(+), 7 deletions(-)

Approvals:
  Benjamin Keith Ford: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, but someone else must approve
  Sean Bright: Looks good to me, approved
  Friendly Automation: Approved for Submit



diff --git a/cdr/cdr_pgsql.c b/cdr/cdr_pgsql.c
index a4919d5..1a3f9d6 100644
--- a/cdr/cdr_pgsql.c
+++ b/cdr/cdr_pgsql.c
@@ -184,15 +184,27 @@
 		conn = NULL;
 	}
 
-	ast_str_set(&conn_info, 0, "host=%s port=%s dbname=%s user=%s",
-		pghostname, pgdbport, pgdbname, pgdbuser);
-
-	if (!ast_strlen_zero(pgappname)) {
-		ast_str_append(&conn_info, 0, " application_name=%s", pgappname);
+	if (!ast_strlen_zero(pghostname)) {
+		ast_str_append(&conn_info, 0, "host=%s ", pghostname);
 	}
-
+	if (!ast_strlen_zero(pgdbport)) {
+		ast_str_append(&conn_info, 0, "port=%s ", pgdbport);
+	}
+	if (!ast_strlen_zero(pgdbname)) {
+		ast_str_append(&conn_info, 0, "dbname=%s ", pgdbname);
+	}
+	if (!ast_strlen_zero(pgdbuser)) {
+		ast_str_append(&conn_info, 0, "user=%s ", pgdbuser);
+	}
+	if (!ast_strlen_zero(pgappname)) {
+		ast_str_append(&conn_info, 0, "application_name=%s ", pgappname);
+	}
 	if (!ast_strlen_zero(pgpassword)) {
-		ast_str_append(&conn_info, 0, " password=%s", pgpassword);
+		ast_str_append(&conn_info, 0, "password=%s", pgpassword);
+	}
+	if (ast_str_strlen(conn_info) == 0) {
+		ast_log(LOG_ERROR, "Connection string is blank.\n");
+		return;
 	}
 
 	conn = PQconnectdb(ast_str_buffer(conn_info));

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/11443
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I0a921f99bbd265768be08cd492f04b30855b8423
Gerrit-Change-Number: 11443
Gerrit-PatchSet: 2
Gerrit-Owner: Christian Savinovich <csavinovich at digium.com>
Gerrit-Reviewer: Benjamin Keith Ford <bford at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: Sean Bright <sean.bright at gmail.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190611/68840300/attachment.html>


More information about the asterisk-code-review mailing list