Tencent / MMKV

An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.
Other
17.39k stars 1.91k forks source link

1.3.3版本开始,SINGLE_PROCESS 和 CRYPT下,encode一个数据后,decode的这个key的数据可能是错的 #1351

Closed XXUUNN closed 3 months ago

XXUUNN commented 3 months ago

The version of MMKV

v1.3.3 & version-latest

The platform of MMKV

Android

What's the issue?

1.3.3版本开始,SINGLE_PROCESS 和 CRYPT下,encode一个数据后,decode这个key的数据很可能出现错误,decode失败。 有错误输出 InvalidProtocolBuffer truncatedMessage

logcat output

2024-07-11 14:36:31.586  2450-2450  I  <MMKV_Android.cpp:158::mmkvWithID> prepare to load mmyu111 (id 308a9dc23d64e466418b84a8b489329a) from rootPath /storage/emulated/0/Android/data/com.tencent.mmkvdemo/files/00000
2024-07-11 14:36:31.587  2450-2450  I  <MemoryFile.cpp:97::open> open fd[0x90], /storage/emulated/0/Android/data/com.tencent.mmkvdemo/files/00000/308a9dc23d64e466418b84a8b489329a
2024-07-11 14:36:31.587  2450-2450  I  <MemoryFile.cpp:202::mmap> mmap to address [0x7982d65000], oldPtr [0x0], [/storage/emulated/0/Android/data/com.tencent.mmkvdemo/files/00000/308a9dc23d64e466418b84a8b489329a]
2024-07-11 14:36:31.587  2450-2450  I  <MemoryFile.cpp:97::open> open fd[0x9d], /storage/emulated/0/Android/data/com.tencent.mmkvdemo/files/00000/308a9dc23d64e466418b84a8b489329a.crc
2024-07-11 14:36:31.587  2450-2450  I  <MemoryFile.cpp:202::mmap> mmap to address [0x7982d64000], oldPtr [0x0], [/storage/emulated/0/Android/data/com.tencent.mmkvdemo/files/00000/308a9dc23d64e466418b84a8b489329a.crc]
2024-07-11 14:36:31.587  2450-2450  I  <MMKV_IO.cpp:210::loadMetaInfoAndCheck> meta file [308a9dc23d64e466418b84a8b489329a] has flag [0]
2024-07-11 14:36:31.587  2450-2450  I  <MMKV_IO.cpp:80::loadFromFile> loading [308a9dc23d64e466418b84a8b489329a] with 269 actual size, file size 4096, InterProcess 0, meta info version:4
2024-07-11 14:36:31.587  2450-2450  I  <MMKV_IO.cpp:85::loadFromFile> loading [308a9dc23d64e466418b84a8b489329a] with crc 4294277947 sequence 1 version 4
2024-07-11 14:36:31.587  2450-2450  I  <MMKV_IO.cpp:129::loadFromFile> loaded [308a9dc23d64e466418b84a8b489329a] with 1 key-values
2024-07-11 14:36:31.587  2450-2450  E  test12logWriteAndUpload: <<get[88888888888888877777777777777766666666666666655555555555555544444444444444433333333333333322222222222222211111111111111100000000000000099999999999999988888888888888877777777777777766666666666666655555555555555544444444444444433333333333333322222222222222211
2024-07-11 14:36:31.761  2450-2450  E  test12logWriteAndUpload: >>set[99999999999999988888888888888877777777777777766666666666666655555555555555544444444444444433333333333333322222222222222211111111111111100000000000000099999999999999988888888888888877777777777777766666666666666655555555555555544444444444444433333333333333322222222222222211
2024-07-11 14:36:31.921  2450-7127  I  Compiler allocated 4991KB to compile void android.view.ViewRootImpl.performTraversals()
2024-07-11 14:36:31.936  2450-2450  E  <MMKV.cpp:744::getBytes> InvalidProtocolBuffer truncatedMessage
2024-07-11 14:36:31.936  2450-2450  E  test12logWriteAndUpload: <<get[
2024-07-11 14:36:32.107  2450-2450  E  test12logWriteAndUpload: >>set[11
2024-07-11 14:36:32.276  2450-2450  E  test12logWriteAndUpload: <<get[11
2024-07-11 14:36:32.428  2450-2450  E  test12logWriteAndUpload: >>set[22222222222222211
2024-07-11 14:36:32.592  2450-2450  E  test12logWriteAndUpload: <<get[22222222222222211
2024-07-11 14:36:32.741  2450-2450  E  test12logWriteAndUpload: >>set[33333333333333322222222222222211
lingol commented 3 months ago

I can not reproduce this problem. Please provide the whole demo project. For example your I is not defined.

lingol commented 3 months ago

Wait I have successfully reproduced this error after retrying many times. Will look into it next week.

lingol commented 3 months ago

It's fixed in the dev branch. This bug was introduced in v1.3.3 in this optimization:

Optimizing write speed when there's only one key inside MMKV.

lingol commented 3 months ago

It's released in v1.3.9.