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 659781 - Variable result in type casting from uint8[] to int32
Variable result in type casting from uint8[] to int32
Status: RESOLVED NOTABUG
Product: vala
Classification: Core
Component: Basic Types
0.12.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2011-09-22 01:53 UTC by Leonardo Ferreira Fontenelle
Modified: 2015-06-13 12:04 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Proof-of-concept implementation of GSeekable for GBufferedInputStream (5.15 KB, patch)
2012-03-28 12:20 UTC, Maciej (Matthew) Piechotka
none Details | Review

Description Leonardo Ferreira Fontenelle 2011-09-22 01:53:34 UTC
$ cat test.vala
void main (string[] args) {
	uint8[] buffer = { 1, 0, 0, 0};
	stdout.printf("%d\n", (int32) buffer);
}
$ valac test.vala
$ ./test
165187424
$ ./test
155156320
$ ./test
160784224

I don't believe this is intended behavior, and I do believe converting from uint8[] to int could be very useful.
Comment 1 Simon Werbeck 2012-03-27 23:42:55 UTC
No, this is expected, since the pointer to the first element in the buffer is cast to an int32. For the record, this version works:

void main (string[] args) {
    uint8[] buffer = { 1, 0, 1, 0 };
    stdout.printf("%d\n", ((int32[]) buffer)[0]);
}

$ ./test
65537
$ ./test
65537
Comment 2 Maciej (Matthew) Piechotka 2012-03-27 23:50:00 UTC
(In reply to comment #0)
> $ cat test.vala
> void main (string[] args) {
>     uint8[] buffer = { 1, 0, 0, 0};
>     stdout.printf("%d\n", (int32) buffer);
> }
> $ valac test.vala
> $ ./test
> 165187424
> $ ./test
> 155156320
> $ ./test
> 160784224
> 

Is it a cast of a pointer to array to int32?

> I don't believe this is intended behavior, and I do believe converting from
> uint8[] to int could be very useful.

And source of subtle little vs. bigger endian bugs...

Could you provide a few motivational examples if you find it useful?
Comment 3 Leonardo Ferreira Fontenelle 2012-03-28 00:45:37 UTC
I wanted to read an int32 from a binary file. Being new to Vala and with virtually no C background, a lot of stuff is not intuitive to me.
Comment 4 Maciej (Matthew) Piechotka 2012-03-28 10:06:42 UTC
(In reply to comment #3)
> I wanted to read an int32 from a binary file. Being new to Vala and with
> virtually no C background, a lot of stuff is not intuitive to me.

You need to take care about endianness:
http://en.wikipedia.org/wiki/Endianness

Basicly if you have 4-digit decimal number 0123 you can store it:
 - In bigger-endian format: 0123
 - In little-endian format: 3210
 - In middle-endian: 1032

The bigger-endian is arguably more natural - however the casts may be more expensive (you need to shift the pointer instead of doing nothing). 

In any case you probably should use GLib.DataInputStream:

var file_input = file.read ();
var data_input = new DataInputStream(file_input);
data_input.byte_order = DataStreamByteOrder.BIGGER_ENDIAN; // Sane format[1]
var my_int = data_input.read_int32();

I would propose to close it as WONTFIX.

[1] It MUST match the specification of file format. However if you design the spec choose one that does not match the most popular platform (x86 is little-endian). The performance impact of conversion is negligible but it will help you avoid hard-to-track errors when you forgot to change it.

PS. Don't worry - many people with "C" background forget about this as well. However with growing ARM platform popularity (read - all mobile phones, tablets, ...) you should be aware of the portability issues.
Comment 5 Leonardo Ferreira Fontenelle 2012-03-28 10:28:56 UTC
I already took care of it, although not as elegantly as in comment #4. I'd like to use GLib.DataInputStream, but it doens't seem to allow me to seek arbitrary positions in the file. If casting from an array directly to a integer is not an option, than I guess this is reaaly WONTFIX.
Comment 6 Maciej (Matthew) Piechotka 2012-03-28 12:20:15 UTC
Created attachment 210776 [details] [review]
Proof-of-concept implementation of GSeekable for GBufferedInputStream

(In reply to comment #5)
> I already took care of it, although not as elegantly as in comment #4. I'd like
> to use GLib.DataInputStream, but it doens't seem to allow me to seek arbitrary
> positions in the file.

I don't know 'why not' - probably because noone implemented it.
I have attached proof-of-concept implementation of GSeekable for GBufferedInputStream. I will try to contact glib devs today and check if they are interesting [the patch need a) testing/checking for off-by-one errors b) implementation of truncation - I need to check the exact behaviour and c) copy'n'paste into GBufferedOutputStream]. I should be able to finish today but I prefer to first check if they are interesting.

> If casting from an array directly to a integer is not an
> option, than I guess this is reaaly WONTFIX.

I have not said that - I am not developer of Vala. But I don't think it is a good solution.
Comment 7 Maciej (Matthew) Piechotka 2012-03-29 09:16:19 UTC
I've filled bug #673034.
Comment 8 Leonardo Ferreira Fontenelle 2015-06-13 12:04:29 UTC
This bug report was opened because GLib.DataInputStream didn't implement the seek features from glib (comment #5), and my workaround didn't work because it was wrong (comment #1).

Now I have been corrected (comment #1) and GLib.DataInputStream does implement seek (bug #673034).

Thanks!

Closing as NOTABUG.