changeset 413:ff8d91a6a006

Fix for PR518, adds utility function for wrapping NPString in std::string.
author Adam Domurad <adomurad@redhat.com>
date Mon, 04 Jun 2012 12:07:33 -0400
parents 73f6e7fd1446
children bdc092f4b4ff
files AUTHORS ChangeLog plugin/icedteanp/IcedTeaJavaRequestProcessor.cc plugin/icedteanp/IcedTeaNPPlugin.cc plugin/icedteanp/IcedTeaPluginUtils.cc plugin/icedteanp/IcedTeaPluginUtils.h
diffstat 6 files changed, 60 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/AUTHORS	Fri Jun 01 16:44:32 2012 -0400
+++ b/AUTHORS	Mon Jun 04 12:07:33 2012 -0400
@@ -5,6 +5,7 @@
 Deepak Bhole <dbhole@redhat.com>
 Ricardo Martín Camarero <rickyepoderi@yahoo.es>
 Danesh Dadachanji <ddadacha@redhat.com>
+Adam Domurad <adomurad@redhat.com>
 Thomas Fitzsimmons <fitzsim@redhat.com>
 Mark Greenwood <mark@dcs.shef.ac.uk>
 Andrew John Hughes <ahughes@redhat.com>
--- a/ChangeLog	Fri Jun 01 16:44:32 2012 -0400
+++ b/ChangeLog	Mon Jun 04 12:07:33 2012 -0400
@@ -1,3 +1,19 @@
+2012-06-04  Adam Domurad  <adomurad@redhat.com>
+
+	Added self to AUTHORS.
+	This patch fixes PR518, ensures null termination of strings based off
+	of NPVariant results.
+	* plugin/icedteanp/IcedTeaPluginUtils.h: Added declaration of
+	NPVariantAsString
+	* plugin/icedteanp/IcedTeaPluginUtils.cc
+	(NPVariantAsString): New. Converts an NPVariant to a
+	std::string, assumes it is a valid NPString.
+	(isObjectJSArray): Now uses NPVariantAsString, minor cleanup.
+	* plugin/icedteanp/IcedTeaJavaRequestProcessor.cc
+	(plugin_get_documentbase): Now uses NPVariantAsString.
+	* plugin/icedteanp/IcedTeaNPPlugin.cc
+	(NPVariantToString): Now uses NPVariantAsString, minor cleanup.
+
 2012-06-01  Deepak Bhole <dbhole@redhat.com>
 
 	PR863: Error passing strings to applet methods in Chromium
--- a/plugin/icedteanp/IcedTeaJavaRequestProcessor.cc	Fri Jun 01 16:44:32 2012 -0400
+++ b/plugin/icedteanp/IcedTeaJavaRequestProcessor.cc	Mon Jun 04 12:07:33 2012 -0400
@@ -904,11 +904,7 @@
     } 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))
     {
 
--- a/plugin/icedteanp/IcedTeaNPPlugin.cc	Fri Jun 01 16:44:32 2012 -0400
+++ b/plugin/icedteanp/IcedTeaNPPlugin.cc	Mon Jun 04 12:07:33 2012 -0400
@@ -1093,17 +1093,10 @@
   browser_functions.getproperty(instance, NPVARIANT_TO_OBJECT(location), 
                                href_id, &href);
 
+  std::string href_str = IcedTeaPluginUtilities::NPVariantAsString(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
-
-  gchar** parts = g_strsplit (href_str, "/", -1);
+  gchar** parts = g_strsplit (href_str.c_str(), "/", -1);
   guint parts_sz = g_strv_length (parts);
 
   std::string location_str;
@@ -1119,8 +1112,6 @@
   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);
--- a/plugin/icedteanp/IcedTeaPluginUtils.cc	Fri Jun 01 16:44:32 2012 -0400
+++ b/plugin/icedteanp/IcedTeaPluginUtils.cc	Mon Jun 04 12:07:33 2012 -0400
@@ -661,11 +661,8 @@
     }
     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 @@
 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))
   {
-    snprintf(str, NUM_STR_BUFFER_SIZE, "%p", variant);
+    result->append(IcedTeaPluginUtilities::NPVariantAsString(variant));
+    was_string_already = true;
+  }
+  else if (NPVARIANT_IS_VOID(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 @@
     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 @@
     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.
--- a/plugin/icedteanp/IcedTeaPluginUtils.h	Fri Jun 01 16:44:32 2012 -0400
+++ b/plugin/icedteanp/IcedTeaPluginUtils.h	Mon Jun 04 12:07:33 2012 -0400
@@ -209,6 +209,9 @@
     	/* 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);