소스 검색

- multiple fixes:
1) test memory allocations
2) return code overlapping in case of error (missing break)
3) bogus/ not needed NULL tests for result freeing
- credits goes to Bogdan


git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@4784 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt 17 년 전
부모
커밋
d65be2c006
1개의 변경된 파일14개의 추가작업 그리고 7개의 파일을 삭제
  1. 14 7
      modules/db_postgres/km_dbase.c

+ 14 - 7
modules/db_postgres/km_dbase.c

@@ -405,6 +405,11 @@ int db_postgres_store_result(const db_con_t* _con, db_res_t** _r)
 	int rc = 0;
 
 	*_r = db_new_result();
+	if (*_r==NULL) {
+		LM_ERR("failed to init new result\n");
+		rc = -1;
+		goto done;
+	}
 
 	while (1) {
 		if ((res = PQgetResult(CON_CONNECTION(_con)))) {
@@ -421,18 +426,20 @@ int db_postgres_store_result(const db_con_t* _con, db_res_t** _r)
 
 	switch(pqresult) {
 		case PGRES_COMMAND_OK:
-		/* Successful completion of a command returning no data (such as INSERT or UPDATE). */
+		/* Successful completion of a command returning no data
+		 * (such as INSERT or UPDATE). */
 		rc = 0;
 		break;
 
 		case PGRES_TUPLES_OK:
-			/* Successful completion of a command returning data (such as a SELECT or SHOW). */
+			/* Successful completion of a command returning data
+			 * (such as a SELECT or SHOW). */
 			if (db_postgres_convert_result(_con, *_r) < 0) {
 				LM_ERR("%p Error returned from convert_result()\n", _con);
-				if (*_r) db_free_result(*_r);
-
+				db_free_result(*_r);
 				*_r = 0;
 				rc = -4;
+				break;
 			}
 			rc =  0;
 			break;
@@ -441,7 +448,7 @@ int db_postgres_store_result(const db_con_t* _con, db_res_t** _r)
 			LM_ERR("%p - invalid query, execution aborted\n", _con);
 			LM_ERR("%p: %s\n", _con, PQresStatus(pqresult));
 			LM_ERR("%p: %s\n", _con, PQresultErrorMessage(CON_RESULT(_con)));
-			if (*_r) db_free_result(*_r);
+			db_free_result(*_r);
 			*_r = 0;
 			rc = -3;
 			break;
@@ -458,13 +465,13 @@ int db_postgres_store_result(const db_con_t* _con, db_res_t** _r)
 			LM_ERR("%p Probable invalid query\n", _con);
 			LM_ERR("%p: %s\n", _con, PQresStatus(pqresult));
 			LM_ERR("%p: %s\n", _con, PQresultErrorMessage(CON_RESULT(_con)));
-			if (*_r) db_free_result(*_r);
-
+			db_free_result(*_r);
 			*_r = 0;
 			rc = -4;
 			break;
 	}
 
+done:
 	free_query(_con);
 	return (rc);
 }