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

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.

Co-authored-by: Olivier Goffart <olivier.goffart@slint-ui.com>
This commit is contained in:
Simon Hausmann 2023-04-12 16:43:37 +02:00 committed by GitHub
parent ea769cb10c
commit 004dce6c0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 0 deletions

View file

@ -75,6 +75,7 @@ fn default_config() -> cbindgen::Config {
defines: [
("target_pointer_width = 64".into(), "SLINT_TARGET_64".into()),
("target_pointer_width = 32".into(), "SLINT_TARGET_32".into()),
("target_arch = wasm32".into(), "SLINT_TARGET_WASM".into()), // Disable any wasm guarded code in C++, too - so that there are no gaps in enums.
]
.iter()
.cloned()