[cpp-sp] branch maint-3 updated: Use bound parameters.

Codeberg noreply at shibboleth.net
Mon Nov 30 14:29:22 UTC 2026


This is an automated email from the git hooks/post-receive script.

codeberg pushed a commit to branch maint-3
in repository cpp-sp.

View the commit online:
https://codeberg.org/Shibboleth/cpp-sp/commit/2e2b2191d199bf10ae323f3ee311a6f41682d610

The following commit(s) were added to refs/heads/maint-3 by this push:
     new 2e2b2191 Use bound parameters.
2e2b2191 is described below

commit 2e2b2191d199bf10ae323f3ee311a6f41682d610
Author: Scott Cantor <scott at restingparrotsoftware.com>
AuthorDate: Mon Nov 30 08:31:00 2026 -0500

    Use bound parameters.
---
 odbc-store/odbc-store.cpp | 74 +++++++++++++++++++++++------------------------
 1 file changed, 37 insertions(+), 37 deletions(-)

diff --git a/odbc-store/odbc-store.cpp b/odbc-store/odbc-store.cpp
index aae85204..d675c434 100644
--- a/odbc-store/odbc-store.cpp
+++ b/odbc-store/odbc-store.cpp
@@ -250,26 +250,25 @@ namespace {
         strftime(ret,32,"{ts '%Y-%m-%d %H:%M:%S'}",ptime);
     }
 
-    class SQLString {
-        const char* m_src;
-        string m_copy;
-    public:
-        SQLString(const char* src) : m_src(src) {
-            if (strchr(src, '\\') || strchr(src, '\'')) {
-                m_copy = src;
-                replace_all(m_copy, "\\", "\\\\");
-                replace_all(m_copy, "'", "\\'");
-            }
+    SQLRETURN bindAndExecute(SQLHSTMT stmt, const string& q, const char* context, const char* key=nullptr)
+    {
+        SQLLEN ind = SQL_NTS;
+
+        SQLRETURN sr = SQLPrepare(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+        if (SQL_SUCCEEDED(sr)) {
+            sr = SQLBindParam(stmt, 1, SQL_C_CHAR, SQL_VARCHAR, max<size_t>(strlen(context), 1), 0, const_cast<char*>(context), &ind);
         }
 
-        operator const char*() const {
-            return tostr();
+        if (SQL_SUCCEEDED(sr) && key) {
+            sr = SQLBindParam(stmt, 2, SQL_C_CHAR, SQL_VARCHAR, max<size_t>(strlen(key), 1), 0, const_cast<char*>(key), &ind);
         }
 
-        const char* tostr() const {
-            return m_copy.empty() ? m_src : m_copy.c_str();
+        if (SQL_SUCCEEDED(sr)) {
+            sr = SQLExecute(stmt);
         }
-    };
+
+        return sr;
+    }
 };
 
 ODBCStorageService::ODBCStorageService(const DOMElement* e) : m_log(Category::getInstance("XMLTooling.StorageService")),
@@ -554,8 +553,6 @@ int ODBCStorageService::readRow(const char *table, const char* context, const ch
     // Prepare and exectute select statement.
     char timebuf[32];
     timestampFromTime(time(nullptr), timebuf);
-    SQLString scontext(context);
-    SQLString skey(key);
     string q("SELECT version");
     if (pexpiration)
         q += ",expires";
@@ -563,11 +560,11 @@ int ODBCStorageService::readRow(const char *table, const char* context, const ch
         pvalue->erase();
         q = q + ",CASE version WHEN " + lexical_cast<string>(version) + " THEN null ELSE value END";
     }
-    q = q + " FROM " + table + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "' AND expires > " + timebuf;
+    q = q + " FROM " + table + " WHERE context=? AND id=? AND expires > " + timebuf;
     if (m_log.isDebugEnabled())
         m_log.debug("SQL: %s", q.c_str());
 
-    SQLRETURN sr=SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    SQLRETURN sr = bindAndExecute(stmt, q, context, key);
     if (!SQL_SUCCEEDED(sr)) {
         m_log.error("error searching for (t=%s, c=%s, k=%s)", table, context, key);
         log_error(stmt, SQL_HANDLE_STMT);
@@ -636,14 +633,12 @@ int ODBCStorageService::updateRow(const char *table, const char* context, const
     // First, fetch the current version for later, which also ensures the record still exists.
     char timebuf[32];
     timestampFromTime(time(nullptr), timebuf);
-    SQLString scontext(context);
-    SQLString skey(key);
     string q("SELECT version FROM ");
-    q = q + table + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "' AND expires > " + timebuf;
+    q = q + table + " WHERE context=? AND id=? AND expires > " + timebuf;
 
     m_log.debug("SQL: %s", q.c_str());
 
-    sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    sr = bindAndExecute(stmt, q, context, key);
     if (!SQL_SUCCEEDED(sr)) {
         m_log.error("error searching for (t=%s, c=%s, k=%s)", table, context, key);
         log_error(stmt, SQL_HANDLE_STMT);
@@ -685,7 +680,7 @@ int ODBCStorageService::updateRow(const char *table, const char* context, const
         q = q + "expires = " + timebuf;
     }
 
-    q = q + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "'";
+    q = q + " WHERE context=? AND id=?";
 
     sr = SQLPrepare(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
     if (!SQL_SUCCEEDED(sr)) {
@@ -709,6 +704,16 @@ int ODBCStorageService::updateRow(const char *table, const char* context, const
         m_log.debug("SQLBindParam succeeded (value = %s)", value);
     }
 
+    SQLUSMALLINT pnum = value ? 2 : 1;
+    sr = SQLBindParam(stmt, pnum, SQL_C_CHAR, SQL_VARCHAR, COLSIZE_CONTEXT, 0, const_cast<char*>(context), &b_ind);
+    if (SQL_SUCCEEDED(sr))
+        sr = SQLBindParam(stmt, pnum + 1, SQL_C_CHAR, SQL_VARCHAR, COLSIZE_ID, 0, const_cast<char*>(key), &b_ind);
+    if (!SQL_SUCCEEDED(sr)) {
+        m_log.error("SQLBindParam failed (t=%s, c=%s, k=%s)", table, context, key);
+        log_error(stmt, SQL_HANDLE_STMT);
+        throw IOException("ODBC StorageService failed to update record.");
+    }
+
     int attempts = 3;
     pair<bool,bool> logres;
     do {
@@ -740,12 +745,10 @@ bool ODBCStorageService::deleteRow(const char *table, const char *context, const
     SQLHSTMT stmt = getHSTMT(conn);
 
     // Prepare and execute delete statement.
-    SQLString scontext(context);
-    SQLString skey(key);
-    string q = string("DELETE FROM ") + table + " WHERE context='" + scontext.tostr() + "' AND id='" + skey.tostr() + "'";
+    string q = string("DELETE FROM ") + table + " WHERE context=? AND id=?";
     m_log.debug("SQL: %s", q.c_str());
 
-    SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    SQLRETURN sr = bindAndExecute(stmt, q, context, key);
      if (sr == SQL_NO_DATA)
         return false;
     else if (!SQL_SUCCEEDED(sr)) {
@@ -818,12 +821,11 @@ void ODBCStorageService::updateContext(const char *table, const char* context, t
     char nowbuf[32];
     timestampFromTime(time(nullptr), nowbuf);
 
-    SQLString scontext(context);
-    string q = string("UPDATE ") + table + " SET expires = " + timebuf + " WHERE context='" + scontext.tostr() + "' AND expires > " + nowbuf;
+    string q = string("UPDATE ") + table + " SET expires = " + timebuf + " WHERE context=? AND expires > " + nowbuf;
 
     m_log.debug("SQL: %s", q.c_str());
 
-    SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    SQLRETURN sr = bindAndExecute(stmt, q, context);
     if ((sr != SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
         m_log.error("error updating records (t=%s, c=%s)", table, context ? context : "all");
         log_error(stmt, SQL_HANDLE_STMT);
@@ -846,15 +848,14 @@ void ODBCStorageService::reap(const char *table, const char* context)
     timestampFromTime(time(nullptr), nowbuf);
     string q;
     if (context) {
-        SQLString scontext(context);
-        q = string("DELETE FROM ") + table + " WHERE context='" + scontext.tostr() + "' AND expires <= " + nowbuf;
+        q = string("DELETE FROM ") + table + " WHERE context=? AND expires <= " + nowbuf;
     }
     else {
         q = string("DELETE FROM ") + table + " WHERE expires <= " + nowbuf;
     }
     m_log.debug("SQL: %s", q.c_str());
 
-    SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    SQLRETURN sr = context ? bindAndExecute(stmt, q, context) : SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
     if ((sr != SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
         m_log.error("error expiring records (t=%s, c=%s)", table, context ? context : "all");
         log_error(stmt, SQL_HANDLE_STMT);
@@ -873,11 +874,10 @@ void ODBCStorageService::deleteContext(const char *table, const char* context)
     SQLHSTMT stmt = getHSTMT(conn);
 
     // Prepare and execute delete statement.
-    SQLString scontext(context);
-    string q = string("DELETE FROM ") + table + " WHERE context='" + scontext.tostr() + "'";
+    string q = string("DELETE FROM ") + table + " WHERE context=?";
     m_log.debug("SQL: %s", q.c_str());
 
-    SQLRETURN sr = SQLExecDirect(stmt, (SQLCHAR*)q.c_str(), SQL_NTS);
+    SQLRETURN sr = bindAndExecute(stmt, q, context);
     if ((sr != SQL_NO_DATA) && !SQL_SUCCEEDED(sr)) {
         m_log.error("error deleting context (t=%s, c=%s)", table, context);
         log_error(stmt, SQL_HANDLE_STMT);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list