Skip to content

Commit eac72f0

Browse files
committed
[runtime] Add implementation of _swift_strlcpy
This safer function is missing on Linux.
1 parent 4ba7e41 commit eac72f0

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

include/swift/Runtime/Portability.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===--- Portability.h -----------------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Includes stub APIs that make the portable runtime easier to write.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_RUNTIME_PORTABILITY_H
18+
#define SWIFT_RUNTIME_PORTABILITY_H
19+
#include <stddef.h>
20+
21+
size_t _swift_strlcpy(char *dst, const char *src, size_t maxlen);
22+
23+
#endif

stdlib/public/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ set(swift_runtime_sources
4747
MetadataLookup.cpp
4848
Mutex.cpp
4949
Once.cpp
50+
Portability.cpp
5051
ProtocolConformance.cpp
5152
Reflection.cpp
5253
RuntimeEntrySymbols.cpp

stdlib/public/runtime/Portability.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===--- Portability.cpp - -----------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===---------------------------------------------------------------------===//
12+
//
13+
// Implementations of the stub APIs that make portable runtime easier to write.
14+
//
15+
//===---------------------------------------------------------------------===//
16+
17+
#include "swift/Runtime/Portability.h"
18+
#include <cstring>
19+
20+
size_t _swift_strlcpy(char *dst, const char *src, size_t maxlen) {
21+
const size_t srclen = std::strlen(src);
22+
if (srclen < maxlen) {
23+
std::memmove(dst, src, srclen + 1);
24+
} else if (maxlen != 0) {
25+
std::memmove(dst, src, maxlen - 1);
26+
dst[maxlen - 1] = '\0';
27+
}
28+
return srclen;
29+
}

stdlib/public/runtime/Reflection.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/Runtime/Enum.h"
1818
#include "swift/Basic/Demangle.h"
1919
#include "swift/Runtime/Debug.h"
20+
#include "swift/Runtime/Portability.h"
2021
#include "Private.h"
2122
#include <cassert>
2223
#include <cstdio>
@@ -1247,6 +1248,6 @@ static Mirror ObjC_getMirrorForSuperclass(Class sup,
12471248
}
12481249

12491250
// Copy into the provided buffer.
1250-
strlcpy(outputBuffer, result.c_str(), *outputBufferSize);
1251+
_swift_strlcpy(outputBuffer, result.c_str(), *outputBufferSize);
12511252
return outputBuffer;
12521253
}

0 commit comments

Comments
 (0)