Opened 4 years ago

Closed 4 years ago

#18577 closed defect (duplicate)

Not correctly copying arryas in lang.clone

Reported by: Plamen Lenkov Owned by:
Priority: undecided Milestone: tbd
Component: Core Version: 1.10.4
Keywords: Cc:
Blocked By: Blocking:


If there is undefined index/indexes in an array - the cloned array skips them and then duplicates the values of the last indexes. Example:

var a = [];
a[1] = "1";
a[2] = "2";
console.log(a); /* Result is [undefined, "1", "2"] */
var b = lang.clone(a); 
console.log(b); /* Result is ["1", "1", "2"], expected to be the same as 'a' */

It happens because there is a check in the lang.clone source - if(i in src) which skips the undefined indexes and the resulting array's length is less than original. And then at the end there is return lang._mixin(r, src, lang.clone); which makes the source and resulting array the same size but duplicates the values at the end which were already pushed in the places of the originally undefined indexes.

Maybe the fix is just to remove the check if(i in src). At least that was how I expected to work.

Change History (3)

comment:1 Changed 4 years ago by bill

Sounds like a bug to me too. I traced that code back to 3d3d17129ecd99621520e56e7354ebabf8f624b6, to fix #9763, but #9763 was about dojo.clone() and functions, not about arrays.

Alternately to removing if(i in src) check, we could do:

for(i = 0, l = src.length; i < l; ++i){
	if(i in src){
		r[i] = lang.clone(src[i]);

That avoids setting r[i] at all, rather than setting it to undefined. Might be better?

comment:2 Changed 4 years ago by David Fraser

This seems like a duplicate of #16257, which contains a patch to fix this.

comment:3 Changed 4 years ago by bill

Resolution: duplicate
Status: newclosed

Duplicate of #16257.
I agree, closing as duplicate.

Note: See TracTickets for help on using tickets.