Skip to content

Commit f57668f

Browse files
committed
[Mangler] Fix a bug in EncodeStringAsNumber.
When we encode a string into an APInt the size of the APInt needs to be large enough for the string (we got this part right), but also large enough for arithmetic with the value of character length.
1 parent 5e206f3 commit f57668f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lib/ABI/Compression.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,14 @@ std::string swift::Compress::EncodeCBCString(StringRef In) {
144144
}
145145

146146
static char DecodeFixedWidth(APInt &num) {
147-
unsigned BW = std::max(num.getBitWidth(), 32u);
147+
unsigned BW = num.getBitWidth();
148+
assert(BW > 16 &&
149+
"The APInt needs to be large enough for arithmetic on CharsetLength");
148150
APInt C = APInt(BW, Huffman::CharsetLength);
149151

150152
APInt Quotient(BW, 0), Remainder(BW, 0);
151153
APInt::udivrem(num, C, Quotient, Remainder);
152-
num = Quotient;
154+
num = Quotient.zext(BW);
153155
return Huffman::Charset[Remainder.getZExtValue()];
154156
}
155157

@@ -169,7 +171,7 @@ static void EncodeFixedWidth(APInt &num, char ch) {
169171

170172
APInt
171173
swift::Compress::EncodeStringAsNumber(StringRef In, EncodingKind Kind) {
172-
unsigned BW = std::max(1u, (unsigned) In.size() *
174+
unsigned BW = std::max(32u, (unsigned) In.size() *
173175
Huffman::LongestEncodingLength);
174176
APInt num = APInt(BW, 0);
175177

0 commit comments

Comments
 (0)