From 0a6343909fcf8b193a1b517b3a16eabd4ae89a83 Mon Sep 17 00:00:00 2001
From: Sergei Golubchik <serg@mariadb.org>
Date: Sat, 1 Apr 2023 15:58:14 +0200
Subject: [PATCH] ensure that STRING_WITH_LEN is only used with string literals

This is allowed:

  STRING_WITH_LEN("string literal")

This is not:

  char *str = "pointer to string";
  ... STRING_WITH_LEN(str) ..

In C++ this is also allowed:

  const char str[] = "string literal";
  ... STRING_WITH_LEN(str) ...
---
 include/m_string.h                            | 19 ++++++++++++++++---
 sql/debug_sync.cc                             |  2 +-
 .../PerconaFT/portability/toku_debug_sync.h   |  9 ++++++---
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/include/m_string.h b/include/m_string.h
index 4a06b90c1aa..0133d81f6df 100644
--- a/include/m_string.h
+++ b/include/m_string.h
@@ -198,9 +198,22 @@ extern ulonglong strtoull(const char *str, char **ptr, int base);
 
 #include <mysql/plugin.h>
 
-#define STRING_WITH_LEN(X) (X), ((size_t) (sizeof(X) - 1))
-#define USTRING_WITH_LEN(X) ((uchar*) X), ((size_t) (sizeof(X) - 1))
-#define C_STRING_WITH_LEN(X) ((char *) (X)), ((size_t) (sizeof(X) - 1))
+#ifdef __cplusplus
+#include <type_traits>
+template<typename T> inline const char *_swl_check(T s)
+{
+  static_assert(std::is_same<T, const char (&)[sizeof(T)]>::value
+             || std::is_same<T, const char [sizeof(T)]>::value,
+             "Wrong argument for STRING_WITH_LEN()");
+  return s;
+}
+#define STRING_WITH_LEN(X) _swl_check<decltype(X)>(X), ((size_t) (sizeof(X) - 1))
+#else
+#define STRING_WITH_LEN(X) (X ""), ((size_t) (sizeof(X) - 1))
+#endif
+
+#define USTRING_WITH_LEN(X) (uchar*) STRING_WITH_LEN(X)
+#define C_STRING_WITH_LEN(X) (char *) STRING_WITH_LEN(X)
 #define LEX_STRING_WITH_LEN(X) (X).str, (X).length
 
 typedef struct st_mysql_const_lex_string LEX_CSTRING;
diff --git a/sql/debug_sync.cc b/sql/debug_sync.cc
index 16ff4abafe1..5cb8fb715bf 100644
--- a/sql/debug_sync.cc
+++ b/sql/debug_sync.cc
@@ -1292,7 +1292,7 @@ uchar *debug_sync_value_ptr(THD *thd)
 
   if (opt_debug_sync_timeout)
   {
-    static char on[]= "ON - current signal: '"; 
+    static const char on[]= "ON - current signal: '";
 
     // Ensure exclusive access to debug_sync_global.ds_signal
     mysql_mutex_lock(&debug_sync_global.ds_mutex);
diff --git a/storage/tokudb/PerconaFT/portability/toku_debug_sync.h b/storage/tokudb/PerconaFT/portability/toku_debug_sync.h
index affe3054214..ff99c99d81e 100644
--- a/storage/tokudb/PerconaFT/portability/toku_debug_sync.h
+++ b/storage/tokudb/PerconaFT/portability/toku_debug_sync.h
@@ -64,9 +64,12 @@ inline void toku_debug_sync(struct tokutxn *txn, const char *sync_point_name) {
     void *client_extra;
     THD *thd;
 
-    toku_txn_get_client_id(txn, &client_id, &client_extra);
-    thd = reinterpret_cast<THD *>(client_extra);
-    DEBUG_SYNC(thd, sync_point_name);
+    if (debug_sync_service)
+    {
+      toku_txn_get_client_id(txn, &client_id, &client_extra);
+      thd = reinterpret_cast<THD *>(client_extra);
+      debug_sync_service(thd, sync_point_name, strlen(sync_point_name));
+    }
 }
 
 #else // defined(ENABLED_DEBUG_SYNC)