Incorrect result with \0 in property name

RESOLVED FIXED in mozilla8

Status

()

Core
JavaScript Engine
RESOLVED FIXED
6 years ago
6 years ago

People

(Reporter: jandem, Assigned: evilpie)

Tracking

unspecified
mozilla8
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: [inbound])

Attachments

(1 attachment, 3 obsolete attachments)

(Reporter)

Description

6 years ago
js> [1, 2]["1" + "\0"] 
2
js> [1, 2]["1" + "\0aaaaa"] 
2

I don't know the exact ES5 section but I think this should print |undefined|. Opera, Chrome and Safari also return undefined.
The problem is js_StringIsIndex assuming "\0" is the end of a string.
Created attachment 549133 [details] [diff] [review]
v1
Assignee: general → evilpies
Status: NEW → ASSIGNED
Attachment #549133 - Flags: review?(jorendorff)
I changed, (previous == (MAXINDEX / 10) && c <= (MAXINDEX % 10)), otherwise we wouldn't allow the highest possible value.
Comment on attachment 549133 [details] [diff] [review]
v1

Based on the comment there, I think eliminating 2^32-1 is intentional. But then MAXINDEX is a misleading name.

Otherwise, looks good.

Clearing review.
Attachment #549133 - Flags: review?(jorendorff)
Created attachment 550188 [details] [diff] [review]
v2

The asserts in InitArrayElements scared me a bit :/, so i rather left them untouched.
Attachment #549133 - Attachment is obsolete: true
Attachment #550188 - Flags: review?(jorendorff)
Comment on attachment 550188 [details] [diff] [review]
v2

Review of attachment 550188 [details] [diff] [review]:
-----------------------------------------------------------------

Clearing the review flag again; Tom's going to post a new patch.

::: js/src/jsarray.cpp
@@ +124,2 @@
>  
>  static inline bool

Nit: House style is to avoid multiple blank lines together, so please delete two of these.

@@ -151,5 @@
> - * an atomized string.
> - */
> -bool
> -js_StringIsIndex(JSLinearString *str, jsuint *indexp)
> -{

Sorry, I know you asked about moving this on IRC and I thought it was a good idea, but now I think it's better to leave it where it is.

@@ +1322,5 @@
>  
>  static JSBool
>  InitArrayElements(JSContext *cx, JSObject *obj, jsuint start, jsuint count, Value *vector)
>  {
> +    const uint32 MAXINDEX = 4294967294u;

Heh! No, put MAX_ARRAY_INDEX in one place, perhaps jsarray.h; don't define it with the same value, and different names, in multiple places. :)

@@ +1327,1 @@
>      JS_ASSERT(count < MAXINDEX);

Since you changed MAXINDEX from 4294967295u to 4294967294u, the assertion has to change from < to <=. Likewise for any other places where MAXINDEX is used.

::: js/src/jsnum.cpp
@@ +1324,5 @@
> + *
> + */
> +bool
> +StringIsIndex(JSLinearString *str, jsuint *indexp)
> +{

On IRC just now we settled on naming this js::StringIsArrayIndex.
Attachment #550188 - Flags: review?(jorendorff)
Created attachment 550810 [details] [diff] [review]
v3
Attachment #550188 - Attachment is obsolete: true
Attachment #550810 - Flags: review?(jorendorff)
Attachment #550810 - Flags: review?(jorendorff)
Created attachment 550823 [details] [diff] [review]
v3 with test

So added test for this specific bug [0, 1, 2]['1\0'] would return 1. I also was curious whether or test suite catches wrong behavior around Array Indexes (to high or low max index) and luckily it does :)
Attachment #550810 - Attachment is obsolete: true
Attachment #550823 - Flags: review?(jorendorff)
Comment on attachment 550823 [details] [diff] [review]
v3 with test

Review of attachment 550823 [details] [diff] [review]:
-----------------------------------------------------------------

Please run benchmarks to make sure this doesn't slow anything down. It shouldn't, because this code shouldn't be hot anymore.

r=me with the minor fixes below.

::: js/src/jsarray.cpp
@@ +179,1 @@
>   */

Heh. Thanks for fixing this comment. While you're in here, please fix the JSVAL_IS_INT part too: we would also have to check JSVAL_IS_DOUBLE. And anyway it seems like that part of the comment fits better on js_CheckForStringIndex than here; move it if you want to.

Micro-nit: Delete the line here that just contains a "*".

@@ +184,5 @@
> +    uint32 length = str->length();
> +    const jschar *end = s + length;
> +
> +    if (length == 0 || length > sizeof("4294967294") || !JS7_ISDEC(*s))
> +        return false;

sizeof("4294967294") is 11, due to the '\0', but you want to return false if the string is longer than 10 characters.

::: js/src/tests/ecma_5/Array/index-with-null-character.js
@@ +4,5 @@
> + */
> +
> +var BUGNUMBER = 653175;
> +var summary = 'Incorrect result with \0 in property name';
> +print(BUGNUMBER + ': ' + summary);

These three lines are worthless. I would delete them, but it's totally up to you. Some JS hackers still include them.
Attachment #550823 - Flags: review?(jorendorff) → review+
http://hg.mozilla.org/integration/mozilla-inbound/rev/66ccc3cf04bc
Whiteboard: [inbound]
http://hg.mozilla.org/mozilla-central/rev/66ccc3cf04bc
Status: ASSIGNED → RESOLVED
Last Resolved: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla8
You need to log in before you can comment on or make changes to this bug.