Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose Resource incorrectly reads string chunks from a file #4938

Closed
AChep opened this issue Jun 7, 2024 · 2 comments · Fixed by #4943
Closed

Compose Resource incorrectly reads string chunks from a file #4938

AChep opened this issue Jun 7, 2024 · 2 comments · Fixed by #4943
Assignees
Labels
bug Something isn't working reproduced resources

Comments

@AChep
Copy link

AChep commented Jun 7, 2024

Describe the bug
App crashes during base64 decoding of the resources.

image

image

If I go to the file itself and read the given range

> dd if=strings.commonMain.cvr ibs=1 skip=23401 count=83
string|feat_item_show_barcode_text|RW5jb2RlIGEgdGV4dCBhcyBkaWZmZXJlbnQgYmFyY29kZXMu
83+0 records in
0+1 records out
83 bytes copied, 7.6769e-05 s, 1.1 MB/s

we can see that the range is correct. However the record data is different to what Compose resources reads:

RW5jb2RlIGEgdGV4dCBhcyBkaWZmZXJlbnQgY
RW5jb2RlIGEgdGV4dCBhcyBkaWZmZXJlbnQgYmFyY29kZXMu

if we investigate further we see that the ResourceReader.desktop.kt has read the following sequence of bytes:

[115, 116, 114, 105, 110, 103, 124, 102, 101, 97, 116, 95, 105, 116, 101, 109, 95, 115, 104, 111, 119, 95, 98, 97, 114, 99, 111, 100, 101, 95, 116, 101, 120, 116, 124, 82, 87, 53, 106, 98, 50, 82, 108, 73, 71, 69, 103, 100, 71, 86, 52, 100, 67, 66, 104, 99, 121, 66, 107, 97, 87, 90, 109, 90, 88, 74, 108, 98, 110, 81, 103, 89, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

if I pause there and execute two different commands:

String(getResourceAsStream(path).use { it.readAllBytes() }).slice(offset.toInt()..(offset+size).toInt())
// prints string|feat_item_show_barcode_text|RW5jb2RlIGEgdGV4dCBhcyBkaWZmZXJlbnQgYmFyY29kZXMu which is correct
String(getResourceAsStream(path).use { it.skipNBytes(offset); it.readNBytes(size.toInt()) })
// prints string|feat_item_show_barcode_text|RW5jb2RlIGEgdGV4dCBhcyBkaWZmZXJlbnQgYmFyY29kZXMu which is correct
String(getResourceAsStream(path).use { it.skip(offset); it.read(result, 0, size.toInt()); result })
// prints string|feat_item_show_barcode_text|RW5jb2RlIGEgdGV4dCBhcyBkaWZmZXJlbnQgY which is not correct

The idea why it happens is because both .skip() and .read() methods that the current implementation uses can skip and read less than requested.

Affected platforms

  • Desktop (Windows, Linux, macOS)

Versions

  • Libraries:
    • Compose Multiplatform version: 1.6.11
  • Kotlin version: 2.0.0

To Reproduce

  1. Clone AChep/keyguard-app@1ceb9f5
  2. Execute :desktopApp:run Gradle task
  3. Enter any data in the app to skip the setup flow and head straight to Settings / Other / Features overview
@AChep AChep added bug Something isn't working submitted labels Jun 7, 2024
AChep added a commit to AChep/compose-multiplatform that referenced this issue Jun 7, 2024
AChep added a commit to AChep/compose-multiplatform that referenced this issue Jun 7, 2024
AChep added a commit to AChep/compose-multiplatform that referenced this issue Jun 7, 2024
@terrakok terrakok self-assigned this Jun 7, 2024
terrakok added a commit that referenced this issue Jun 7, 2024
… jvm platforms. (#4943)

In some cases the skip and read methods may handle less bytes then
expected. The PR fixes it by proper API on the JVM and manual check on
the Android.

Fixes #4938

## Testing
I manually checked it on the project from the issue.

## Release Notes
### Fixes - Resources
- Read exactly requested count of bytes from InputStream on jvm
platforms.
@okushnikov
Copy link
Collaborator

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

1 similar comment
@okushnikov
Copy link
Collaborator

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment