Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-1741

Application hangs if you call forEach in a dictionary

      How frequently does the bug occur?

      All the time

      Description

      I know that is a strange case, you shouldn't call forEach in a dictionary. However, I think that it should throw an error instead of hanging. I proceed to explain the problem.

      I have a schema like this one, where one of the keys is a dictionary.

        static schema = {
          name: 'TestObject',
          primaryKey: 'primaryKey',
          properties: {
            primaryKey: 'string',
            dictionary: '{}',
          },
        };
      

      Then if I retrieve the object and do a forEach or some call on it, the app completely hangs.

      const object = realm.objectForPrimaryKey('TestObject', 'current');
      
      object.dictionary.forEach();
      

      If you call a random named function, an error is thrown as expected.

      object.dictionary.randomfkjah();
      
      

      Using Flipper Hermes Debugger and pausing, I've arrived at this code:

        ["entries", "keys", "values"].forEach(function (methodName) {
          var method = function () {
            const isSet = this instanceof realmConstructor.Set;
            var self = this.type === "object" || isSet ? this.snapshot() : this;
            var index = 0;
      
            return Object.create(iteratorPrototype, {
              next: {
                value: function () {
                  if (!self || index >= self.length) {
                    self = null;
                    return { done: true, value: undefined };
                  }
      
                  var value;
                  switch (methodName) {
                    case "entries":
                      value = isSet ? [self[index], self[index]] : [index, self[index]];
                      break;
                    case "keys":
                      value = isSet ? undefined : index;
                      break;
                    default:
                      value = self[index];
                      break;
                  }
      
                  index++;
                  return { done: false, value: value };
                },
              },
            });
          };
      
          exportedFunctions[methodName] = { value: method, configurable: true, writable: true };
        });
      

      It seems that the problem is caused because self.length is null, {{ (!self || index >= self.length)}} always evals to false and therefore it loops forever.

      I hope you find this report useful.

      Best regards, Santiago

      Stacktrace & log output

      No response

      Can you reproduce the bug?

      Yes, always

      Reproduction Steps

      I've made a minimal reproducible demo here with latest react-native and realm@hermes versions: https://github.com/sdandois/RealmHermesSomeBug/.

      Clone the repo and do yarn android. The app never finishes loading because of the bug. If you comment the problematic line, it works just fine.

      Version

      10.20.0-beta.5

      What SDK flavour are you using?

      Local Database only

      Are you using encryption?

      No, not using encryption

      Platform OS and version(s)

      React Native Android

      Build environment

      No response

      Cocoapods version

      No response

            Assignee:
            jacob.gunnarsson@mongodb.com Jacob Gunnarsson (Inactive)
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: