Skip to content

Commit b9d35f5

Browse files
Merge pull request #7833 from adrian-prantl/cherry-pick-stable-20230725-LEB128-Don-t-initialize-error-on-success
[Cherry-pick into stable/20230725] [LEB128] Don't initialize error on success
2 parents 652a422 + c29e3d8 commit b9d35f5

File tree

7 files changed

+25
-22
lines changed

7 files changed

+25
-22
lines changed

lld/COFF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ static void findKeepUniqueSections(COFFLinkerContext &ctx) {
12491249
const uint8_t *cur = contents.begin();
12501250
while (cur != contents.end()) {
12511251
unsigned size;
1252-
const char *err;
1252+
const char *err = nullptr;
12531253
uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
12541254
if (err)
12551255
fatal(toString(obj) + ": could not decode addrsig section: " + err);

lld/ELF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,7 @@ static void findKeepUniqueSections(opt::InputArgList &args) {
22292229
const uint8_t *cur = contents.begin();
22302230
while (cur != contents.end()) {
22312231
unsigned size;
2232-
const char *err;
2232+
const char *err = nullptr;
22332233
uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
22342234
if (err)
22352235
fatal(toString(f) + ": could not decode addrsig section: " + err);

llvm/include/llvm/Support/LEB128.h

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,29 +125,30 @@ inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
125125
}
126126

127127
/// Utility function to decode a ULEB128 value.
128+
///
129+
/// If \p error is non-null, it will point to a static error message,
130+
/// if an error occured. It will not be modified on success.
128131
inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
129132
const uint8_t *end = nullptr,
130133
const char **error = nullptr) {
131134
const uint8_t *orig_p = p;
132135
uint64_t Value = 0;
133136
unsigned Shift = 0;
134-
if (error)
135-
*error = nullptr;
136137
do {
137-
if (p == end) {
138+
if (LLVM_UNLIKELY(p == end)) {
138139
if (error)
139140
*error = "malformed uleb128, extends past end";
140-
if (n)
141-
*n = (unsigned)(p - orig_p);
142-
return 0;
141+
Value = 0;
142+
break;
143143
}
144144
uint64_t Slice = *p & 0x7f;
145-
if ((Shift >= 64 && Slice != 0) || Slice << Shift >> Shift != Slice) {
145+
if (LLVM_UNLIKELY(Shift >= 63) &&
146+
((Shift == 63 && (Slice << Shift >> Shift) != Slice) ||
147+
(Shift > 63 && Slice != 0))) {
146148
if (error)
147149
*error = "uleb128 too big for uint64";
148-
if (n)
149-
*n = (unsigned)(p - orig_p);
150-
return 0;
150+
Value = 0;
151+
break;
151152
}
152153
Value += Slice << Shift;
153154
Shift += 7;
@@ -158,17 +159,18 @@ inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
158159
}
159160

160161
/// Utility function to decode a SLEB128 value.
162+
///
163+
/// If \p error is non-null, it will point to a static error message,
164+
/// if an error occured. It will not be modified on success.
161165
inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
162166
const uint8_t *end = nullptr,
163167
const char **error = nullptr) {
164168
const uint8_t *orig_p = p;
165169
int64_t Value = 0;
166170
unsigned Shift = 0;
167171
uint8_t Byte;
168-
if (error)
169-
*error = nullptr;
170172
do {
171-
if (p == end) {
173+
if (LLVM_UNLIKELY(p == end)) {
172174
if (error)
173175
*error = "malformed sleb128, extends past end";
174176
if (n)
@@ -177,8 +179,9 @@ inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
177179
}
178180
Byte = *p;
179181
uint64_t Slice = Byte & 0x7f;
180-
if ((Shift >= 64 && Slice != (Value < 0 ? 0x7f : 0x00)) ||
181-
(Shift == 63 && Slice != 0 && Slice != 0x7f)) {
182+
if (LLVM_UNLIKELY(Shift >= 63) &&
183+
((Shift == 63 && Slice != 0 && Slice != 0x7f) ||
184+
(Shift > 63 && Slice != (Value < 0 ? 0x7f : 0x00)))) {
182185
if (error)
183186
*error = "sleb128 too big for int64";
184187
if (n)

llvm/lib/Object/MachOObjectFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,7 +2992,7 @@ void ExportEntry::pushNode(uint64_t offset) {
29922992
ErrorAsOutParameter ErrAsOutParam(E);
29932993
const uint8_t *Ptr = Trie.begin() + offset;
29942994
NodeState State(Ptr);
2995-
const char *error;
2995+
const char *error = nullptr;
29962996
uint64_t ExportInfoSize = readULEB128(State.Current, &error);
29972997
if (error) {
29982998
*E = malformedError("export info size " + Twine(error) +
@@ -3127,7 +3127,7 @@ void ExportEntry::pushNode(uint64_t offset) {
31273127

31283128
void ExportEntry::pushDownUntilBottom() {
31293129
ErrorAsOutParameter ErrAsOutParam(E);
3130-
const char *error;
3130+
const char *error = nullptr;
31313131
while (Stack.back().NextChildIndex < Stack.back().ChildCount) {
31323132
NodeState &Top = Stack.back();
31333133
CumulativeString.resize(Top.ParentStringLength);

llvm/lib/Support/DataExtractor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static T getLEB128(StringRef Data, uint64_t *OffsetPtr, Error *Err,
202202
if (isError(Err))
203203
return T();
204204

205-
const char *error;
205+
const char *error = nullptr;
206206
unsigned bytes_read;
207207
T result =
208208
Decoder(Bytes.data() + *OffsetPtr, &bytes_read, Bytes.end(), &error);

llvm/tools/llvm-readobj/COFFDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2125,7 +2125,7 @@ void COFFDumper::printAddrsig() {
21252125
const uint8_t *End = AddrsigContents.bytes_end();
21262126
while (Cur != End) {
21272127
unsigned Size;
2128-
const char *Err;
2128+
const char *Err = nullptr;
21292129
uint64_t SymIndex = decodeULEB128(Cur, &Size, End, &Err);
21302130
if (Err)
21312131
reportError(createError(Err), Obj->getFileName());

llvm/tools/llvm-readobj/ELFDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5002,7 +5002,7 @@ static Expected<std::vector<uint64_t>> toULEB128Array(ArrayRef<uint8_t> Data) {
50025002
const uint8_t *End = Data.end();
50035003
while (Cur != End) {
50045004
unsigned Size;
5005-
const char *Err;
5005+
const char *Err = nullptr;
50065006
Ret.push_back(decodeULEB128(Cur, &Size, End, &Err));
50075007
if (Err)
50085008
return createError(Err);

0 commit comments

Comments
 (0)