[sr-dev] git:master: db_postgres: null-terminate blob buffers

Daniel-Constantin Mierla miconda at gmail.com
Sun Oct 3 21:55:48 CEST 2010


Module: sip-router
Branch: master
Commit: 7370685a2eb6d27d6775c8cffa59f6f17cdb76b5
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=7370685a2eb6d27d6775c8cffa59f6f17cdb76b5

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Sun Oct  3 21:51:35 2010 +0200

db_postgres: null-terminate blob buffers

- reapply part of patch done for FS#77: fix BLOBs with postgres
- fix BLOBs with postgres, also add a null-termination as the other
SQL databases in order to be able to work with the presence modules
- change: do not increment the lenght of blob
- initial fix by Henning Westerholt, commit
  2e56720769913c55cacc83fe3a31f44f072e7590

---

 modules/db_postgres/km_val.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/modules/db_postgres/km_val.c b/modules/db_postgres/km_val.c
index cb3fe9f..b2e97c0 100644
--- a/modules/db_postgres/km_val.c
+++ b/modules/db_postgres/km_val.c
@@ -55,9 +55,11 @@
  * \param _l string length
  * \return 0 on success, negative on error
  */
-int db_postgres_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l)
+int db_postgres_str2val(const db_type_t _t, db_val_t* _v, const char* _s,
+		const int _l)
 {
-	/* use common function for non BLOB, NULL setting and input parameter checking */
+	/* use common function for non BLOB, NULL setting and input
+	 * parameter checking */
 	if ( _t != DB1_BLOB || _s == NULL || _v == NULL) {
 		return db_str2val(_t, _v, _s, _l, 1);
 	} else {
@@ -67,21 +69,24 @@ int db_postgres_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const
 		 * The string is stored in new allocated memory, which we could
 		 * not free later thus we need to copy it to some new memory here.
 		 */
- 		tmp_s = (char*)PQunescapeBytea((unsigned char*)_s, (size_t*)(void*)&(VAL_BLOB(_v).len));
+		tmp_s = (char*)PQunescapeBytea((unsigned char*)_s,
+					(size_t*)(void*)&(VAL_BLOB(_v).len));
 		if(tmp_s==NULL) {
 			LM_ERR("PQunescapeBytea failed\n");
 			return -7;
 		}
-		VAL_BLOB(_v).s = pkg_malloc(VAL_BLOB(_v).len);
+		VAL_BLOB(_v).s = pkg_malloc(VAL_BLOB(_v).len + 1);
 		if (VAL_BLOB(_v).s == NULL) {
 			LM_ERR("no private memory left\n");
 			PQfreemem(tmp_s);
 			return -8;
 		}
-		LM_DBG("allocate %d bytes memory for BLOB at %p", VAL_BLOB(_v).len, VAL_BLOB(_v).s);
+		LM_DBG("allocate %d+1 bytes memory for BLOB at %p",
+				VAL_BLOB(_v).len, VAL_BLOB(_v).s);
 		memcpy(VAL_BLOB(_v).s, tmp_s, VAL_BLOB(_v).len);
 		PQfreemem(tmp_s);
 
+		VAL_BLOB(_v).s[VAL_BLOB(_v).len] = '\0';
 		VAL_TYPE(_v) = DB1_BLOB;
 		VAL_FREE(_v) = 1;
 




More information about the sr-dev mailing list