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

Fix memory corruption when loading encoded embedded images in C++ #2542

Merged
merged 2 commits into from
Apr 12, 2023

Conversation

tronical
Copy link
Member

The ImageCacheKey enum has a gap when not targeting wasm:

pub enum ImageCacheKey {
    /// This variant indicates that no image cache key can be created for the image.
    /// For example this is the case for programmatically created images.
    Invalid,
    /// The image is identified by its path on the file system.
    Path(SharedString),
    /// The image is identified by a URL.
    #[cfg(target_arch = "wasm32")]
    URL(SharedString),
    /// The image is identified by the static address of its encoded data.
    EmbeddedData(usize),
}

In the C++ generated header, that cfg was not mapped, and thus the URL variant was always there, while in a regular slint-cpp build it's not. Consequently tag value 2 in Rust was used to represent the EmbeddedData variant, while in the C++ generated code that become variant 3 and 2 was interpreted as a URL. So the receiving code in C++ tried to interpret the cache key as URL variant, while Rust created it as EmbeddedData.

The ImageCacheKey enum has a gap when not targeting wasm:

```
pub enum ImageCacheKey {
    /// This variant indicates that no image cache key can be created for the image.
    /// For example this is the case for programmatically created images.
    Invalid,
    /// The image is identified by its path on the file system.
    Path(SharedString),
    /// The image is identified by a URL.
    #[cfg(target_arch = "wasm32")]
    URL(SharedString),
    /// The image is identified by the static address of its encoded data.
    EmbeddedData(usize),
}
```

In the C++ generated header, that cfg was not mapped, and thus the URL
variant was always there, while in a regular slint-cpp build it's not.
Consequently tag value 2 in Rust was used to represent the EmbeddedData
variant, while in the C++ generated code that become variant 3 and 2 was
interpreted as a URL. So the receiving code in C++ tried to interpret
the cache key as URL variant, while Rust created it as EmbeddedData.
api/cpp/cbindgen.rs Outdated Show resolved Hide resolved
Co-authored-by: Olivier Goffart <olivier.goffart@slint-ui.com>
@tronical tronical merged commit 004dce6 into master Apr 12, 2023
@tronical tronical deleted the simon/cpp-fix-embedded-image-data branch April 12, 2023 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants