Skip to content

Commit 4452a35

Browse files
committed
Fix assert triggering immediately in sourcekitd-repl.
PR swiftlang#41550 changed from using `SmallVector::set_size` to `resize_for_overwrite` and `truncate`, but in `sourcekitd-repl` changing from `reserve` changed the size just prior to getting the `end()` of the output array, leading to retrieving the end of the resized array, rather than the array prior to resizing. The conversion needs to begin at the original output's end, rather than the resized-to-reserve output size.
1 parent de47a77 commit 4452a35

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

tools/SourceKit/tools/sourcekitd-repl/sourcekitd-repl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ using Convert = ConvertForWcharSize<sizeof(wchar_t)>;
9090

9191
static void convertFromUTF8(llvm::StringRef utf8,
9292
llvm::SmallVectorImpl<wchar_t> &out) {
93+
size_t original_out_size = out.size();
9394
size_t reserve = out.size() + utf8.size();
9495
out.resize_for_overwrite(reserve);
9596
const char *utf8_begin = utf8.begin();
96-
wchar_t *wide_begin = out.end();
97+
wchar_t *wide_begin = out.begin() + original_out_size;
9798
auto res = Convert::ConvertFromUTF8(&utf8_begin, utf8.end(),
9899
&wide_begin, out.data() + reserve,
99100
lenientConversion);
@@ -104,10 +105,11 @@ static void convertFromUTF8(llvm::StringRef utf8,
104105

105106
static void convertToUTF8(llvm::ArrayRef<wchar_t> wide,
106107
llvm::SmallVectorImpl<char> &out) {
108+
size_t original_out_size = out.size();
107109
size_t reserve = out.size() + wide.size()*4;
108110
out.resize_for_overwrite(reserve);
109111
const wchar_t *wide_begin = wide.begin();
110-
char *utf8_begin = out.end();
112+
char *utf8_begin = out.begin() + original_out_size;
111113
auto res = Convert::ConvertToUTF8(&wide_begin, wide.end(),
112114
&utf8_begin, out.data() + reserve,
113115
lenientConversion);

0 commit comments

Comments
 (0)