Skip to content

Commit 461e704

Browse files
author
Dale Johannesen
committed
Aligned and unaligned copies of the same string
were not hashing to the same value. Analysis and patch by Frits van Bommel! llvm-svn: 119770
1 parent 20b5ea9 commit 461e704

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

llvm/lib/Support/FoldingSet.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Support/Allocator.h"
1919
#include "llvm/Support/ErrorHandling.h"
2020
#include "llvm/Support/MathExtras.h"
21+
#include "llvm/System/Host.h"
2122
#include <cassert>
2223
#include <cstring>
2324
using namespace llvm;
@@ -110,18 +111,32 @@ void FoldingSetNodeID::AddString(StringRef String) {
110111
Pos = (Units + 1) * 4;
111112
} else {
112113
// Otherwise do it the hard way.
113-
for (Pos += 4; Pos <= Size; Pos += 4) {
114-
unsigned V = ((unsigned char)String[Pos - 4] << 24) |
115-
((unsigned char)String[Pos - 3] << 16) |
116-
((unsigned char)String[Pos - 2] << 8) |
117-
(unsigned char)String[Pos - 1];
118-
Bits.push_back(V);
114+
// To be compatible with above bulk transfer, we need to take endianness
115+
// into account.
116+
if (sys::isBigEndianHost()) {
117+
for (Pos += 4; Pos <= Size; Pos += 4) {
118+
unsigned V = ((unsigned char)String[Pos - 4] << 24) |
119+
((unsigned char)String[Pos - 3] << 16) |
120+
((unsigned char)String[Pos - 2] << 8) |
121+
(unsigned char)String[Pos - 1];
122+
Bits.push_back(V);
123+
}
124+
} else {
125+
assert(sys::isLittleEndianHost() && "Unexpected host endianness");
126+
for (Pos += 4; Pos <= Size; Pos += 4) {
127+
unsigned V = ((unsigned char)String[Pos - 1] << 24) |
128+
((unsigned char)String[Pos - 2] << 16) |
129+
((unsigned char)String[Pos - 3] << 8) |
130+
(unsigned char)String[Pos - 4];
131+
Bits.push_back(V);
132+
}
119133
}
120134
}
121135

122136
// With the leftover bits.
123137
unsigned V = 0;
124-
// Pos will have overshot size by 4 - #bytes left over.
138+
// Pos will have overshot size by 4 - #bytes left over.
139+
// No need to take endianness into account here - this is always executed.
125140
switch (Pos - Size) {
126141
case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru.
127142
case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru.

0 commit comments

Comments
 (0)