After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 604682 - Casting from string to uint8 results in huge memory allocation
Casting from string to uint8 results in huge memory allocation
Status: RESOLVED OBSOLETE
Product: vala
Classification: Core
Component: Basic Types
0.7.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on: 555754
Blocks:
 
 
Reported: 2009-12-16 07:02 UTC by Darren Warner
Modified: 2018-05-22 13:26 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Darren Warner 2009-12-16 07:02:31 UTC
If I have something similar to the following:

uint8[] str = (uint8[])"Hello World!\n";

valac generates a call to _vala_array_dup#, passing in -1 as the array length. This results in a g_memdup of (-1 * sizeof(guint8)), which will almost certainly fail.

There should probably be a string length calculation in there.
Comment 1 zarevucky.jiri 2009-12-16 09:11:43 UTC
Yep, manual casting of String to uint8[] doesn't work like it should. You have to use method String#to_utf8() to do that.
Comment 2 Frederik Zipp 2010-10-23 10:10:28 UTC
Strings have a .data property returning a byte array since Vala 0.9.5:

    uint8[] buffer = "Hello World!\n".data;

Of course, this does not fix the casting bug.
Comment 3 Rico Tzschichholz 2017-11-28 09:39:20 UTC
I am tending to mark this invalid since assigning anything to an owned variable will require a ref/copy invocation.

Therefore to avoid a copy it is required to do it like:

unowned uint8[] str = (uint8[]) "Hello World!\n";
unowned uint8[] buffer = "Hello World!\n".data;
Comment 4 Al Thomas 2017-11-28 19:58:58 UTC
(In reply to Rico Tzschichholz from comment #3)
> I am tending to mark this invalid since assigning anything to an owned
> variable will require a ref/copy invocation.

I don't think the bug should be marked as invalid. It is a reasonable thing to assume will work. If someone is expert in casting and ownership then maybe they will understand why they get problems at runtime, but that is not the Vala "vision" is it?

If you mean Vala should report that the cast in invalid, then maybe. An error with a helpful message about using the .data property would probably be OK. After all in terms of outcome (uint8[]) "Hello World!\n!; is just an alternative syntax for "Hello World!\n".data;

Ideally though I don't see a problem with supporting the two forms, but it is whether a technical solution for using g_strdup instead of g_memdup is easily implemented.
Comment 5 Jürg Billeter 2017-11-28 20:36:46 UTC
At the very least it seems sensible to report an error when the compiler can easily recognize that the code will not work. I expect it to be simple to reject g_memdup in cases where the array length has internally be set to -1. Or do you see any issue with this?

Whether to properly set the array length for casts from string to uint8[] or not is an independent question. I can't think of a reason to keep -1 instead of calling strlen() out of the top of my head but maybe there was something that I fail to remember.
Comment 6 GNOME Infrastructure Team 2018-05-22 13:26:58 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/65.