diff Sources/PythonString.cpp @ 45:ee76cced46a5

Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Aug 2020 18:13:10 +0200
parents fd58eb5749ed
children 23f3099bed47
line wrap: on
line diff
--- a/Sources/PythonString.cpp	Wed Jul 08 15:22:12 2020 +0200
+++ b/Sources/PythonString.cpp	Mon Aug 03 18:13:10 2020 +0200
@@ -21,13 +21,35 @@
 
 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
 
-PythonString::PythonString(PythonLock& lock,
-                           const std::string& utf8) :
-  string_(lock, PyUnicode_FromString(utf8.c_str()))
+
+void PythonString::SanityCheck()
 {
-  if (!string_.IsValid())
+  if (!string_->IsValid())
   {
-    OrthancPlugins::LogError("Cannot create a Python string");
+    OrthancPlugins::LogError("Cannot create a Python string, check that the string is properly encoded using UTF-8");
     ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError);
   }
 }
+
+
+PythonString::PythonString(PythonLock& lock,
+                           const std::string& utf8)
+{
+  string_.reset(new PythonObject(lock, PyUnicode_FromString(utf8.c_str())));
+  SanityCheck();
+}
+
+
+PythonString::PythonString(PythonLock& lock,
+                           const char* utf8)
+{
+  if (utf8 == NULL)
+  {
+    ORTHANC_PLUGINS_THROW_EXCEPTION(NullPointer);
+  }
+  else
+  {
+    string_.reset(new PythonObject(lock, PyUnicode_FromString(utf8)));
+    SanityCheck();
+  }
+}