This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Re: Patch: FYI: important memory-trashing fix
- From: Mohan Embar <gnustuff at thisiscool dot com>
- To: tromey at redhat dot com
- Cc: GCC libjava patches <java-patches at gcc dot gnu dot org>, Bryce McKinlay <bryce at mckinlay dot net dot nz>
- Date: Thu, 13 Nov 2003 21:50:37 -0600
- Subject: Re: Patch: FYI: important memory-trashing fix
- Reply-to: gnustuff at thisiscool dot com
>>It turns out that in many cases we were passing the wrong length to
>>JvGetStringUTFRegion. Perhaps we should add a new API that doesn't
>>take a length argument at all, since that seems to be the most common
>>use by far.
>
>Wow. At least it's not going to be me that's at the wrong end
>of Bryce's scrutiny this time. Lots of luck!
>
>http://gcc.gnu.org/ml/java-patches/2003-q3/msg00376.html
>
>:)
P.S. With my handy-dandy macro, here is an example of how
gnu::awt::gtk::GtkButtonPeer::setLabel can be rewritten.
(I'm typing this off the top of my head, so there may be
slight errors):
Current CVS Version:
void
gnu::awt::gtk::GtkButtonPeer::setLabel (java::lang::String *label)
{
_Jv_GdkThreadLock sync;
jsize len = 0;
if (label)
len = JvGetStringUTFLength (label);
char buf[len + 1];
// FIXME: this can allocate an unbounded amount. Should use heap
// even though it is slower.
if (label)
JvGetStringUTFRegion (label, 0, label->length(), buf);
buf[len] = '\0';
// The button child is a label.
GtkBin *bin = GTK_BIN (ptr);
gtk_label_set_text (GTK_LABEL (bin->child), buf);
}
Rewritten version:
static const char*
safe_cstr(const char* buf)
{
return buf ? buf : "";
}
void
gnu::awt::gtk::GtkButtonPeer::setLabel (java::lang::String *label)
{
_Jv_GdkThreadLock sync;
JV_TEMP_UTF_STRING(utflabel, label);
// The button child is a label.
GtkBin *bin = GTK_BIN (ptr);
gtk_label_set_text (GTK_LABEL (bin->child), safe_cstr (utflabel));
}