From 0cc053b836a4e3428adf6a27728dbe6fcc792c22 Mon Sep 17 00:00:00 2001 From: ftsui Date: Mon, 7 Aug 2023 12:11:24 -0700 Subject: [PATCH] Fix use-after-free crash in glfw embedder (flutter/engine#44358) When setting up the paths to assets and ICU data, we were previously returning the underlying char* data in a temporary string returned by `std::filesystem::path::string()`. Since the return value of `string()` is never stored, there's no guarantee that the underlying data pointer is still valid later in the function. By storing the string in a local whose lifetime exceeds that of the running engine instance, we guarantee the string isn't freed until after the Flutter app terminates. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- engine/src/flutter/shell/platform/glfw/flutter_glfw.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc b/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc index d50871bd6f..55968f0e26 100644 --- a/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc +++ b/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc @@ -12,6 +12,7 @@ #include #include #include +#include #include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h" #include "flutter/shell/platform/common/incoming_message_dispatcher.h" @@ -755,10 +756,12 @@ static bool RunFlutterEngine( if (engine_state->window_controller != nullptr) { config.open_gl.gl_proc_resolver = EngineProcResolver; } + const std::string assets_path_string = assets_path.string(); + const std::string icu_path_string = icu_path.string(); FlutterProjectArgs args = {}; args.struct_size = sizeof(FlutterProjectArgs); - args.assets_path = assets_path.string().c_str(); - args.icu_data_path = icu_path.string().c_str(); + args.assets_path = assets_path_string.c_str(); + args.icu_data_path = icu_path_string.c_str(); args.command_line_argc = static_cast(argv.size()); args.command_line_argv = &argv[0]; args.platform_message_callback = EngineOnFlutterPlatformMessage;