Skip to content

const object inside of loop when --transform-object-keys true #1300

@lolovo

Description

@lolovo

When using --transform-object-keys true in some cases const object is created outside of loop that used inside of each loop iteration (but this kind of loop optimization is not valid and obvious if you want create multiple new objects with same params inside of loop). This is a bug or feature?

Expected Behavior

Create a new object with same params inside of loop for each loop iteration

Current Behavior

One object is used inside of loop and as reasult all array params point to same object in memory

Steps to Reproduce

  1. Input file (./out/test.js):
!function() {
    let o = [];
    for (let e = 0; e < 10; e++) 
        o.push({o: 0 });
    o[0].o = 1;
    console.log(o[0] === o[1]);  // this should be false
}();
  1. Obfuscate only with "--transform-object-keys true" and turn off default params to get easy readable results:

javascript-obfuscator ./out/test.js --output ./out/test_.js --transform-object-keys true --string-array false --compact false --simplify false --string-array-index-shift false --identifier-names-generator mangled --string-array-rotate false --string-array-shuffle false --string-array-wrappers-chained-calls false

  1. Got output file (./out/test_.js):
!(function () {
    let b = [];
    const c = {};
    c['o'] = 0x0;
    for (let d = 0x0; d < 0xa; d++)
        b['push'](c);
    b[0x0]['o'] = 0x1;
    console['log'](b[0x0] === b[0x1]); // this is true not false due to same object
}());

Your Environment

  • Obfuscator version used:

4.0.2_2023-02-13T06:57:01.273Z

  • Node version used:

v18.17.0

Temporary solution

Rewrite object creation as follows Object.create({...}) in case of new object should be created per each iteration

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions