This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gcjx] Patch: Copy over message string in exception classes


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

  While trying to bootstrap gcjx, I noticed that the error message
for a missing JAR in the CLASSPATH ("No such file or directory")
was being mangled in the output.

Upon investigation, it turns out that string::c_str() is good
only till the string object exists and if you want to use it
beyond that, you should make a copy of the string.

I wanted to use xstrdup() from libiberty for this, but I did
not want to introduce a dependency on libiberty inside gcjx.
So I created a new function copy_str() in util.hh and have
used it in format_repr to initialise the PLAN member, as shown
in the attached patch.

Verified that the patch resolves the mangling issue.

OK?

Thanks,
Ranjit.

PS: For an ostringstream, "message << *p" seems to work (where
p is a char*) , but I can't find an explicit definition for "char"
and the "<<" operator ((though I can for the other primitive types),
just a put() method. But this seems to work, so I did not touch
format_repr::get_message() - I was originally suspecting this to
be the cause of the mangling.

- --
Ranjit Mathew       Email: rmathew AT gmail DOT com

Bangalore, INDIA.     Web: http://ranjitmathew.hostingzero.com/




-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDIvBLYb1hx2wRS48RAi/zAJ0cq9WA8NAfQGAyqpaIAsucf+dW1gCffTj5
wVLHfp/oSkLNRc96AcmzJu0=
=kHUy
-----END PGP SIGNATURE-----
Index: ChangeLog
from  Ranjit Mathew  <rmathew@gcc.gnu.org>

	* util.hh (copy_str): New function similar to strdup().
	* format/format.hh (~format_repr): delete memory pointed to by PLAN.
	* format/format.cc (format_repr::format_repr): Use a duplicate string
	for PLAN, not the original string.

Index: util.hh
===================================================================
--- util.hh	2005-09-10 18:21:36.000000000 +0530
+++ util.hh	2005-09-10 18:30:16.000000000 +0530
@@ -24,8 +24,23 @@
 
 #include <assert.h>
 
+#include <cstdlib>
+#include <cstring>
+
 class model_type;
 
+/// Copies the contents of the given C-style string into dynamically
+/// allocated memory and returns a pointer to it.
+inline char*
+copy_str (const char* str)
+{
+  int len = strlen (str);
+  char* n_str = new char[len+ 1];
+  strcpy (n_str, str);
+  n_str[len] = '\0';
+  return n_str;
+}
+
 inline std::string
 get_simple_name (const std::list<std::string> &id)
 {
Index: format/format.hh
===================================================================
--- format/format.hh	2005-09-10 18:34:27.000000000 +0530
+++ format/format.hh	2005-09-10 18:37:59.000000000 +0530
@@ -99,6 +99,7 @@ public:
 
   virtual ~format_repr ()
   {
+    delete[] plan;
   }
 
   void set_location (const location &w)
Index: format/format.cc
===================================================================
--- format/format.cc	2005-09-10 14:06:24.000000000 +0530
+++ format/format.cc	2005-09-10 19:41:52.000000000 +0530
@@ -25,7 +25,7 @@
 format_repr::format_repr (format_type t, location w, const char *fmt)
   : refc (0),
     where (w),
-    plan (fmt),
+    plan (copy_str (fmt)),
     subst_count (0),
     type (t)
 {

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]