From 3a45053d393b828faa8a5db3febc1d28e6952ce7 Mon Sep 17 00:00:00 2001
From: Adam Domurad <adomurad@redhat.com>
Date: Mon, 4 Jun 2012 12:07:33 -0400
Subject: Fix for PR518, adds utility function for wrapping NPString in
 std::string.

---
 plugin/icedteanp/IcedTeaJavaRequestProcessor.cc |  6 +-
 plugin/icedteanp/IcedTeaNPPlugin.cc             | 15 +----
 plugin/icedteanp/IcedTeaPluginUtils.cc          | 75 ++++++++++++-------------
 plugin/icedteanp/IcedTeaPluginUtils.h           |  3 +
 4 files changed, 43 insertions(+), 56 deletions(-)

(limited to 'plugin/icedteanp')

diff --git a/plugin/icedteanp/IcedTeaJavaRequestProcessor.cc b/plugin/icedteanp/IcedTeaJavaRequestProcessor.cc
index 1431bcc..c20a317 100644
--- a/plugin/icedteanp/IcedTeaJavaRequestProcessor.cc
+++ b/plugin/icedteanp/IcedTeaJavaRequestProcessor.cc
@@ -904,11 +904,7 @@ createJavaObjectFromVariant(NPP instance, NPVariant variant, std::string* id)
     } else if (NPVARIANT_IS_STRING(variant))
     {
     	className = "java.lang.String";
-#if MOZILLA_VERSION_COLLAPSED < 1090200
-    	stringArg.append(NPVARIANT_TO_STRING(variant).utf8characters, NPVARIANT_TO_STRING(variant).utf8length);
-#else
-    	stringArg.append(NPVARIANT_TO_STRING(variant).UTF8Characters, NPVARIANT_TO_STRING(variant).UTF8Length);
-#endif
+    	stringArg = IcedTeaPluginUtilities::NPVariantAsString(variant);
     } else if (NPVARIANT_IS_OBJECT(variant))
     {
 
diff --git a/plugin/icedteanp/IcedTeaNPPlugin.cc b/plugin/icedteanp/IcedTeaNPPlugin.cc
index 52c1793..d132780 100644
--- a/plugin/icedteanp/IcedTeaNPPlugin.cc
+++ b/plugin/icedteanp/IcedTeaNPPlugin.cc
@@ -1093,17 +1093,10 @@ plugin_get_documentbase (NPP instance)
   browser_functions.getproperty(instance, NPVARIANT_TO_OBJECT(location), 
                                href_id, &href);
 
-  // Strip everything after the last "/"
-  char *href_str;
-#if MOZILLA_VERSION_COLLAPSED < 1090200
-  href_str = (char*) malloc(sizeof(char)*NPVARIANT_TO_STRING(href).utf8length + 1);
-  snprintf(href_str, NPVARIANT_TO_STRING(href).utf8length+1, "%s", NPVARIANT_TO_STRING(href).utf8characters);
-#else
-  href_str = (char*) malloc(sizeof(char)*NPVARIANT_TO_STRING(href).UTF8Length + 1);
-  snprintf(href_str, NPVARIANT_TO_STRING(href).UTF8Length+1, "%s", NPVARIANT_TO_STRING(href).UTF8Characters);
-#endif
+  std::string href_str = IcedTeaPluginUtilities::NPVariantAsString(href);
 
-  gchar** parts = g_strsplit (href_str, "/", -1);
+  // Strip everything after the last "/"
+  gchar** parts = g_strsplit (href_str.c_str(), "/", -1);
   guint parts_sz = g_strv_length (parts);
 
   std::string location_str;
@@ -1119,8 +1112,6 @@ plugin_get_documentbase (NPP instance)
   browser_functions.releasevariantvalue(&href);
   browser_functions.releasevariantvalue(&location);
   g_strfreev(parts);
-  free(href_str);
-  href_str = NULL;
  cleanup_done:
   PLUGIN_DEBUG ("plugin_get_documentbase return\n");
   PLUGIN_DEBUG("plugin_get_documentbase returning: %s\n", documentbase_copy);
diff --git a/plugin/icedteanp/IcedTeaPluginUtils.cc b/plugin/icedteanp/IcedTeaPluginUtils.cc
index 2cbfd6e..32e597b 100644
--- a/plugin/icedteanp/IcedTeaPluginUtils.cc
+++ b/plugin/icedteanp/IcedTeaPluginUtils.cc
@@ -661,11 +661,8 @@ IcedTeaPluginUtilities::printNPVariant(NPVariant variant)
     }
     else if (NPVARIANT_IS_STRING(variant))
     {
-#if MOZILLA_VERSION_COLLAPSED < 1090200
-    	PLUGIN_DEBUG("STRING: %s (length=%d)\n", NPVARIANT_TO_STRING(variant).utf8characters, NPVARIANT_TO_STRING(variant).utf8length);
-#else
-    	PLUGIN_DEBUG("STRING: %s (length=%d)\n", NPVARIANT_TO_STRING(variant).UTF8Characters, NPVARIANT_TO_STRING(variant).UTF8Length);
-#endif
+    	std::string str = IcedTeaPluginUtilities::NPVariantAsString(variant);
+    	PLUGIN_DEBUG("STRING: %s (length=%d)\n", str.c_str(), str.size());
     }
     else
     {
@@ -676,52 +673,44 @@ IcedTeaPluginUtilities::printNPVariant(NPVariant variant)
 void
 IcedTeaPluginUtilities::NPVariantToString(NPVariant variant, std::string* result)
 {
-  char str[NUM_STR_BUFFER_SIZE]; // enough for everything except string
-  char* largestr = NULL;
-  if (NPVARIANT_IS_VOID(variant))
+  char conv_str[NUM_STR_BUFFER_SIZE]; // conversion buffer
+  bool was_string_already = false;
+
+  if (NPVARIANT_IS_STRING(variant))
+  {
+    result->append(IcedTeaPluginUtilities::NPVariantAsString(variant));
+    was_string_already = true;
+  }
+  else if (NPVARIANT_IS_VOID(variant))
   {
-    snprintf(str, NUM_STR_BUFFER_SIZE, "%p", variant);
+    snprintf(conv_str, NUM_STR_BUFFER_SIZE, "%p", variant);
   }
   else if (NPVARIANT_IS_NULL(variant))
   {
-    snprintf(str, NUM_STR_BUFFER_SIZE, "NULL");
+    snprintf(conv_str, NUM_STR_BUFFER_SIZE, "NULL");
   }
   else if (NPVARIANT_IS_BOOLEAN(variant))
   {
     if (NPVARIANT_TO_BOOLEAN(variant))
-      snprintf(str, NUM_STR_BUFFER_SIZE, "true");
+      snprintf(conv_str, NUM_STR_BUFFER_SIZE, "true");
     else
-      snprintf(str, NUM_STR_BUFFER_SIZE, "false");
+      snprintf(conv_str, NUM_STR_BUFFER_SIZE, "false");
   }
   else if (NPVARIANT_IS_INT32(variant))
   {
-    snprintf(str, NUM_STR_BUFFER_SIZE, "%d", NPVARIANT_TO_INT32(variant));
+    snprintf(conv_str, NUM_STR_BUFFER_SIZE, "%d", NPVARIANT_TO_INT32(variant));
   }
   else if (NPVARIANT_IS_DOUBLE(variant))
   {
-    snprintf(str, NUM_STR_BUFFER_SIZE, "%f", NPVARIANT_TO_DOUBLE(variant));
-  }
-  else if (NPVARIANT_IS_STRING(variant))
-  {
-#if MOZILLA_VERSION_COLLAPSED < 1090200
-    size_t buffersize = sizeof(char)*NPVARIANT_TO_STRING(variant).utf8length+1;
-    largestr = (char*) malloc(buffersize);
-    snprintf(str, buffersize, "%s", NPVARIANT_TO_STRING(variant).utf8characters);
-#else
-    size_t buffersize = sizeof(char)*NPVARIANT_TO_STRING(variant).UTF8Length+1;
-    largestr = (char*) malloc(buffersize);
-    snprintf(str, buffersize, "%s", NPVARIANT_TO_STRING(variant).UTF8Characters);
-#endif
+    snprintf(conv_str, NUM_STR_BUFFER_SIZE, "%f", NPVARIANT_TO_DOUBLE(variant));
   }
   else
   {
-    snprintf(str, NUM_STR_BUFFER_SIZE, "[Object %p]", variant);
+    snprintf(conv_str, NUM_STR_BUFFER_SIZE, "[Object %p]", variant);
   }
-  if (largestr != NULL){
-    result->append(largestr);
-    free(largestr);
-  } else {
-    result->append(str);
+
+  if (!was_string_already){
+    result->append(conv_str);
   }
 }
 
@@ -861,13 +850,7 @@ IcedTeaPluginUtilities::isObjectJSArray(NPP instance, NPObject* object)
     browser_functions.invoke(instance, constructor, toString, NULL, 0, &constructor_str);
     IcedTeaPluginUtilities::printNPVariant(constructor_str);
 
-    std::string constructor_name = std::string();
-
-#if MOZILLA_VERSION_COLLAPSED < 1090200
-    constructor_name.append(NPVARIANT_TO_STRING(constructor_str).utf8characters, NPVARIANT_TO_STRING(constructor_str).utf8length);
-#else
-    constructor_name.append(NPVARIANT_TO_STRING(constructor_str).UTF8Characters, NPVARIANT_TO_STRING(constructor_str).UTF8Length);
-#endif
+    std::string constructor_name = IcedTeaPluginUtilities::NPVariantAsString(constructor_str);
 
     PLUGIN_DEBUG("Constructor for NPObject is %s\n", constructor_name.c_str());
 
@@ -910,6 +893,20 @@ IcedTeaPluginUtilities::decodeURL(const gchar* url, gchar** decoded_url)
     PLUGIN_DEBUG("SENDING URL: %s\n", *decoded_url);
 }
 
+/* Copies a variant data type into a C++ string */
+std::string
+IcedTeaPluginUtilities::NPVariantAsString(NPVariant variant)
+{
+#if MOZILLA_VERSION_COLLAPSED < 1090200
+  return std::string((
+    NPVARIANT_TO_STRING(variant).utf8characters,
+    NPVARIANT_TO_STRING(variant).utf8ength);
+#else
+  return std::string(
+    NPVARIANT_TO_STRING(variant).UTF8Characters,
+    NPVARIANT_TO_STRING(variant).UTF8Length);
+#endif
+}
 
 /**
  * Posts a function for execution on the plug-in thread and wait for result.
diff --git a/plugin/icedteanp/IcedTeaPluginUtils.h b/plugin/icedteanp/IcedTeaPluginUtils.h
index 9382a94..48250d1 100644
--- a/plugin/icedteanp/IcedTeaPluginUtils.h
+++ b/plugin/icedteanp/IcedTeaPluginUtils.h
@@ -209,6 +209,9 @@ class IcedTeaPluginUtilities
     	/* Converts the given integer to a string */
     	static void itoa(int i, std::string* result);
 
+    	/* Copies a variant data type into a C++ string */
+    	static std::string NPVariantAsString(NPVariant variant);
+
     	/* Frees the given vector and the strings that its contents point to */
     	static void freeStringPtrVector(std::vector<std::string*>* v);
 
-- 
cgit v1.2.3