Skip to content

Commit 42ca1c4

Browse files
authored
Avoid using strndup in TupleImpl::childMetadata (#62121)
1 parent 7d5b634 commit 42ca1c4

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

stdlib/public/runtime/ReflectionMirror.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,6 @@
4040
#include "SwiftObject.h"
4141
#endif
4242

43-
#if defined(_WIN32)
44-
#include <stdarg.h>
45-
46-
namespace {
47-
char *strndup(const char *s, size_t n) {
48-
size_t length = std::min(strlen(s), n);
49-
50-
char *buffer = reinterpret_cast<char *>(malloc(length + 1));
51-
if (buffer == nullptr)
52-
return buffer;
53-
54-
strncpy(buffer, s, length);
55-
buffer[length] = '\0';
56-
return buffer;
57-
}
58-
}
59-
#endif
60-
6143
using namespace swift;
6244

6345
namespace {
@@ -261,7 +243,12 @@ struct TupleImpl : ReflectionMirrorImpl {
261243

262244
// If we have a label, create it.
263245
if (labels && space && labels != space) {
264-
*outName = strndup(labels, space - labels);
246+
size_t labelLen = space - labels;
247+
char *label = (char *)malloc(labelLen + 1);
248+
memcpy(label, labels, labelLen);
249+
label[labelLen] = '\0'; // 0-terminate the string
250+
251+
*outName = label;
265252
hasLabel = true;
266253
}
267254
}

0 commit comments

Comments
 (0)