@@ -1050,12 +1050,14 @@ namespace swift {
10501050 size_t *outputBufferSize,
10511051 uint32_t flags) {
10521052 if (flags > 1 ) {
1053- swift::fatalError (0 , " Only 'flags' value of '0' and '1' is currently supported." );
1054- }
1055- if (outputBuffer != nullptr && outputBufferSize == nullptr ) {
1056- swift::fatalError (0 , " 'outputBuffer' is passed but the size is 'nullptr'." );
1053+ // ignore not supported flags
1054+ return nullptr ;
10571055 }
10581056
1057+ // Very simple flags parsing, move to something more proper once we support more flags
1058+ bool shouldNullTerminateString = flags & 1 ;
1059+ bool shouldUseSimplifiedUIDemangleOptions = flags & 1 ;
1060+
10591061 llvm::StringRef name = llvm::StringRef (mangledName, mangledNameLength);
10601062
10611063 // You must provide buffer size if you're providing your own output buffer
@@ -1066,7 +1068,7 @@ namespace swift {
10661068 if (Demangle::isSwiftSymbol (name)) {
10671069 // Determine demangling/formatting options:
10681070 auto options = DemangleOptions ();
1069- if (flags == 1 ) {
1071+ if (shouldUseSimplifiedUIDemangleOptions ) {
10701072 // simplified display options, for backtraces
10711073 options = DemangleOptions::SimplifiedUIDemangleOptions ();
10721074 }
@@ -1080,12 +1082,19 @@ namespace swift {
10801082 }
10811083
10821084 if (outputBuffer == nullptr ) {
1083- outputBuffer = (char *)::malloc (result.length ());
1085+ auto totalLength = shouldNullTerminateString ? (result.length () + 1 ) : result.length ();
1086+ outputBuffer = (char *)::malloc (totalLength);
10841087 bufferSize = result.length ();
10851088 }
10861089
1087- size_t toCopy = std::min (bufferSize, result.length ());
1090+ size_t toCopy = std::min (
1091+ shouldNullTerminateString ? (bufferSize - 1 ) : bufferSize,
1092+ shouldNullTerminateString ? (result.length () - 1 ) : result.length ()
1093+ );
10881094 ::memcpy (outputBuffer, result.data(), toCopy);
1095+ if (shouldNullTerminateString) {
1096+ outputBuffer[toCopy] = ' \0 ' ;
1097+ }
10891098
10901099 return outputBuffer;
10911100 #ifndef _WIN32
@@ -1110,8 +1119,14 @@ namespace swift {
11101119 return result;
11111120 }
11121121
1113- size_t toCopy = std::min (bufferSize, resultLen);
1122+ size_t toCopy = std::min (
1123+ shouldNullTerminateString ? (bufferSize - 1 ) : bufferSize,
1124+ shouldNullTerminateString ? (resultLen - 1 ) : resultLen
1125+ );
11141126 ::memcpy (outputBuffer, result, toCopy);
1127+ if (shouldNullTerminateString) {
1128+ outputBuffer[toCopy] = ' \0 ' ;
1129+ }
11151130
11161131 free (result);
11171132
@@ -1123,7 +1138,9 @@ namespace swift {
11231138 #endif
11241139 }
11251140
1126- *outputBufferSize = 0 ; // indicate we did not write to buffer
1141+ if (outputBufferSize) {
1142+ *outputBufferSize = 0 ; // indicate we did not write to buffer
1143+ }
11271144 return nullptr ;
11281145 }
11291146 }
0 commit comments