LWN.net Logo

World-writable memory on Samsung Android phones

World-writable memory on Samsung Android phones

Posted Dec 20, 2012 18:48 UTC (Thu) by sorpigal (subscriber, #36106)
In reply to: World-writable memory on Samsung Android phones by apoelstra
Parent article: World-writable memory on Samsung Android phones

You left out "why"

test is an object of type string, and for a string object you can access characters by indexing into it in a looks-like-C kind of way:

   document.write(test[0]); // H

JS for .. in syntax iterates over *keys of an object*

From this you can tell that when I say

    test = {one: "hello", two: "world"};
    for (i in test) alert(i);

expected this will "one" and "two"

    test['two'];

And this will be "World"

The array object is no different, it just has keys that happen to be sequential numbers

    test = ['one', 'two'];
    for (i in test) alert(i); // 0, 1
    alert(test[1]); // two

In order for the string object to look like a C string and let you index characters it must, obviously from the above, appear to store each character at a numeric key corresponding to its index in the string.

The only strange part is why i+1 does string concatenation. I would think that the key would be numeric, but I suppose that there's some reason it isn't. If you need it to be you can use unary +

    test = "Hello";
    for (i in test) alert(+i+1); // 1, 2, 3, 4, 5 as expected


(Log in to post comments)

World-writable memory on Samsung Android phones

Posted Dec 21, 2012 2:57 UTC (Fri) by idupree (subscriber, #71169) [Link]

Keys in JavaScript objects are strings. Javascript arrays (and strings) are Javascript objects. Therefore, keys of Javascript arrays are strings (they are string representations of numbers, to be precise, or of non-numbers if you explicitly add non-number keys to an array object).

Yes, it really is that absurd. And no, it doesn't necessarily impact performance, because modern Javascript engines are often smart enough to undo this oddity without changing the language semantics.

World-writable memory on Samsung Android phones

Posted Dec 21, 2012 15:00 UTC (Fri) by sorpigal (subscriber, #36106) [Link]

it doesn't necessarily impact performance, because modern Javascript engines are often smart enough to undo this oddity without changing the language semantics

"Often" is not always; crazy isn't that far behind us.

I wonder, now, about the performance of code I've written where complex objects and functions have been used as keys. I imagined I was storing pointers, but if the key is really the stringification of the object... that can't be good.

I suppose it sort-of makes sense that somebody decided storing a numeric key as a string was less scary than storing it as a float, but the madness has to stop some time.

World-writable memory on Samsung Android phones

Posted Dec 21, 2012 3:06 UTC (Fri) by apoelstra (subscriber, #75205) [Link]

Thanks for explaining, sorpigal and idupree!

I was just telling what I observed - I really didn't have any idea what might have been going on behind it.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds