react-native-async-storage / async-storage

An asynchronous, persistent, key-value storage system for React Native.
https://react-native-async-storage.github.io/async-storage/
MIT License
4.71k stars 467 forks source link

Calling `mergeItem` with a json array crashes the app #699

Open zhuangdaz opened 2 years ago

zhuangdaz commented 2 years ago

What happened?

I created an array of users and then tried to update one of the user by passing a new JSON array string. Then the app crashes.

const USER_1 = {
  name: "Tom",
  age: 20,
  traits: {
    hair: "black",
    eyes: "blue",
  },
};

const USER_2 = {
  name: "Sarah",
  age: 21,
  hobby: "cars",
  traits: {
    eyes: "green",
  },
};

const USER_3 = {
  name: "Sarah",
  age: 22,
  hobby: "bike",
  traits: {
    eyes: "green",
  },
};

const USERS = [USER_1, USER_2];

const mergeUsers = async () => {
  try {
    console.log("creating users");
    await AsyncStorage.setItem("@MyApp_users", JSON.stringify(USERS));

    console.log("merging user 3");
    await AsyncStorage.mergeItem("@MyApp_users", JSON.stringify([USER_3]));

    // read merged item
    console.log("reading merged users");
    const currentUsers = await AsyncStorage.getItem("@MyApp_users");
    console.log(currentUsers);
  } catch (err) {}
};

Version

1.15.0

What platforms are you seeing this issue on?

System Information

info Fetching system and libraries information...
System:
    OS: macOS 11.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
    Memory: 473.64 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.0 - /usr/local/bin/node
    Yarn: 1.22.15 - /usr/local/bin/yarn
    npm: 8.1.0 - /usr/local/bin/npm
    Watchman: 2021.10.04.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: Not Found
  SDKs:
    iOS SDK: Not Found
    Android SDK: Not Found
  IDEs:
    Android Studio: 1.3 AI-141.2135290
    Xcode: /undefined - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.10 - /Users/zhuangda/.jenv/shims/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: ^17.0.2 => 17.0.2 
    react-native: 0.64.2 => 0.64.2 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps to Reproduce

Described above.

krizzu commented 2 years ago

Hey @zhuangdaz

Thanks for reporting that. After a quick check, it's indeed a bug. It's located here. This method is responsible for merging two objects. Those allegedly should be NSDictionaries, while in truth, they're NSArrays. The crash happens when reading a value for a key.

EddyKay101 commented 2 years ago

Hi, any update on this please as I am experiencing the same issue.

Thanks

acer1456 commented 2 years ago

any updates on this bug? got the same issue too.

thanks

gonzalopantoja commented 2 years ago

Hi I'm running into this issue, is there any update?

Here are the versions I tested: version 1.13.2 - no issue version 1.14.1 - no issue version 1.15.15 - issue version 1.16.1 (latest) - issue

krizzu commented 2 years ago

Hey

@gonzalopantoja thanks for pointing this out, this should come helpful to see where regression happened 🙏

I'm trying to find a time to address this, but in the mean time, happy to accept a PR, as always 😄

github-actions[bot] commented 2 years ago

This issue has been marked as stale due to inactivity. Please respond or otherwise resolve the issue within 7 days or it will be closed.

sabinayakc commented 1 year ago

I am experiencing this issue as well with the latest version.