Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 9ad81da

Browse files
Wrap strdup to use compliant name on Windows (#16372)
A number of POSIX methods were renamed on Windows to match standards requirements, giving deprecation warnings when calling strdup on Windows. This adds a wrapper, to allow calling _strdup on Windows instead. Part of #16256
1 parent 28e6637 commit 9ad81da

File tree

9 files changed

+75
-15
lines changed

9 files changed

+75
-15
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ FILE: ../../../flutter/fml/platform/posix/file_posix.cc
201201
FILE: ../../../flutter/fml/platform/posix/mapping_posix.cc
202202
FILE: ../../../flutter/fml/platform/posix/native_library_posix.cc
203203
FILE: ../../../flutter/fml/platform/posix/paths_posix.cc
204+
FILE: ../../../flutter/fml/platform/posix/posix_wrappers_posix.cc
204205
FILE: ../../../flutter/fml/platform/posix/shared_mutex_posix.cc
205206
FILE: ../../../flutter/fml/platform/posix/shared_mutex_posix.h
206207
FILE: ../../../flutter/fml/platform/win/errors_win.cc
@@ -211,7 +212,9 @@ FILE: ../../../flutter/fml/platform/win/message_loop_win.cc
211212
FILE: ../../../flutter/fml/platform/win/message_loop_win.h
212213
FILE: ../../../flutter/fml/platform/win/native_library_win.cc
213214
FILE: ../../../flutter/fml/platform/win/paths_win.cc
215+
FILE: ../../../flutter/fml/platform/win/posix_wrappers_win.cc
214216
FILE: ../../../flutter/fml/platform/win/wstring_conversion.h
217+
FILE: ../../../flutter/fml/posix_wrappers.h
215218
FILE: ../../../flutter/fml/size.h
216219
FILE: ../../../flutter/fml/status.h
217220
FILE: ../../../flutter/fml/synchronization/atomic_object.h

fml/BUILD.gn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ source_set("fml") {
5656
"native_library.h",
5757
"paths.cc",
5858
"paths.h",
59+
"posix_wrappers.h",
5960
"size.h",
6061
"synchronization/atomic_object.h",
6162
"synchronization/count_down_latch.cc",
@@ -200,6 +201,7 @@ source_set("fml") {
200201
"platform/win/message_loop_win.h",
201202
"platform/win/native_library_win.cc",
202203
"platform/win/paths_win.cc",
204+
"platform/win/posix_wrappers_win.cc",
203205
"platform/win/wstring_conversion.h",
204206
]
205207

@@ -213,6 +215,7 @@ source_set("fml") {
213215
"platform/posix/mapping_posix.cc",
214216
"platform/posix/native_library_posix.cc",
215217
"platform/posix/paths_posix.cc",
218+
"platform/posix/posix_wrappers_posix.cc",
216219
]
217220
}
218221
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/fml/posix_wrappers.h"
6+
7+
#include <string.h>
8+
9+
namespace fml {
10+
11+
char* strdup(const char* str1) {
12+
return ::strdup(str1);
13+
}
14+
15+
} // namespace fml
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/fml/posix_wrappers.h"
6+
7+
#include <string.h>
8+
9+
namespace fml {
10+
11+
char* strdup(const char* str1) {
12+
return _strdup(str1);
13+
}
14+
15+
} // namespace fml

fml/posix_wrappers.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_FML_POSIX_WRAPPERS_H_
6+
#define FLUTTER_FML_POSIX_WRAPPERS_H_
7+
8+
#include "flutter/fml/build_config.h"
9+
10+
// Provides wrappers for POSIX functions that have been renamed on Windows.
11+
// See
12+
// https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?view=vs-2019#posix-function-names
13+
// for context.
14+
namespace fml {
15+
16+
char* strdup(const char* str1);
17+
18+
} // namespace fml
19+
20+
#endif // FLUTTER_FML_POSIX_WRAPPERS_H_

runtime/dart_isolate.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <tuple>
99

1010
#include "flutter/fml/paths.h"
11+
#include "flutter/fml/posix_wrappers.h"
1112
#include "flutter/fml/trace_event.h"
1213
#include "flutter/lib/io/dart_io.h"
1314
#include "flutter/lib/ui/dart_runtime_hooks.h"
@@ -577,7 +578,7 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate(
577578
auto vm_data = DartVMRef::GetVMData();
578579

579580
if (!vm_data) {
580-
*error = strdup(
581+
*error = fml::strdup(
581582
"Could not access VM data to initialize isolates. This may be because "
582583
"the VM has initialized shutdown on another thread already.");
583584
return nullptr;
@@ -613,7 +614,7 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate(
613614

614615
std::shared_ptr<DartIsolate> service_isolate = weak_service_isolate.lock();
615616
if (!service_isolate) {
616-
*error = strdup("Could not create the service isolate.");
617+
*error = fml::strdup("Could not create the service isolate.");
617618
FML_DLOG(ERROR) << *error;
618619
return nullptr;
619620
}
@@ -722,7 +723,7 @@ bool DartIsolate::DartIsolateInitializeCallback(void** child_callback_data,
722723
TRACE_EVENT0("flutter", "DartIsolate::DartIsolateInitializeCallback");
723724
Dart_Isolate isolate = Dart_CurrentIsolate();
724725
if (isolate == nullptr) {
725-
*error = strdup("Isolate should be available in initialize callback.");
726+
*error = fml::strdup("Isolate should be available in initialize callback.");
726727
FML_DLOG(ERROR) << *error;
727728
return false;
728729
}
@@ -799,14 +800,14 @@ bool DartIsolate::InitializeIsolate(
799800
char** error) {
800801
TRACE_EVENT0("flutter", "DartIsolate::InitializeIsolate");
801802
if (!embedder_isolate->Initialize(isolate)) {
802-
*error = strdup("Embedder could not initialize the Dart isolate.");
803+
*error = fml::strdup("Embedder could not initialize the Dart isolate.");
803804
FML_DLOG(ERROR) << *error;
804805
return false;
805806
}
806807

807808
if (!embedder_isolate->LoadLibraries()) {
808-
*error =
809-
strdup("Embedder could not load libraries in the new Dart isolate.");
809+
*error = fml::strdup(
810+
"Embedder could not load libraries in the new Dart isolate.");
810811
FML_DLOG(ERROR) << *error;
811812
return false;
812813
}
@@ -819,7 +820,7 @@ bool DartIsolate::InitializeIsolate(
819820
embedder_isolate->GetIsolateGroupData().GetChildIsolatePreparer();
820821
FML_DCHECK(child_isolate_preparer);
821822
if (!child_isolate_preparer(embedder_isolate.get())) {
822-
*error = strdup("Could not prepare the child isolate to run.");
823+
*error = fml::strdup("Could not prepare the child isolate to run.");
823824
FML_DLOG(ERROR) << *error;
824825
return false;
825826
}

runtime/dart_service_isolate.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <algorithm>
99

1010
#include "flutter/fml/logging.h"
11+
#include "flutter/fml/posix_wrappers.h"
1112
#include "flutter/runtime/embedder_resources.h"
1213
#include "third_party/dart/runtime/include/dart_api.h"
1314
#include "third_party/tonic/converter/dart_converter.h"
@@ -19,12 +20,12 @@
1920
return handle; \
2021
}
2122

22-
#define SHUTDOWN_ON_ERROR(handle) \
23-
if (Dart_IsError(handle)) { \
24-
*error = strdup(Dart_GetError(handle)); \
25-
Dart_ExitScope(); \
26-
Dart_ShutdownIsolate(); \
27-
return false; \
23+
#define SHUTDOWN_ON_ERROR(handle) \
24+
if (Dart_IsError(handle)) { \
25+
*error = fml::strdup(Dart_GetError(handle)); \
26+
Dart_ExitScope(); \
27+
Dart_ShutdownIsolate(); \
28+
return false; \
2829
}
2930

3031
namespace flutter {

runtime/service_protocol.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <utility>
1414
#include <vector>
1515

16+
#include "flutter/fml/posix_wrappers.h"
1617
#include "flutter/fml/synchronization/waitable_event.h"
1718
#include "rapidjson/stringbuffer.h"
1819
#include "rapidjson/writer.h"
@@ -123,7 +124,7 @@ bool ServiceProtocol::HandleMessage(const char* method,
123124
rapidjson::StringBuffer buffer;
124125
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
125126
document.Accept(writer);
126-
*json_object = strdup(buffer.GetString());
127+
*json_object = fml::strdup(buffer.GetString());
127128

128129
#ifndef NDEBUG
129130
FML_DLOG(INFO) << "Response: " << *json_object;

shell/common/skia_event_tracer_impl.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <vector>
99

1010
#include "flutter/fml/logging.h"
11+
#include "flutter/fml/posix_wrappers.h"
1112
#include "flutter/fml/trace_event.h"
1213
#include "third_party/dart/runtime/include/dart_tools_api.h"
1314
#include "third_party/skia/include/utils/SkEventTracer.h"
@@ -243,7 +244,7 @@ bool enableSkiaTracingCallback(const char* method,
243244
const char** json_object) {
244245
FlutterEventTracer* tracer = static_cast<FlutterEventTracer*>(user_data);
245246
tracer->enable();
246-
*json_object = strdup("{\"type\":\"Success\"}");
247+
*json_object = fml::strdup("{\"type\":\"Success\"}");
247248
return true;
248249
}
249250

0 commit comments

Comments
 (0)