Skip to content

[Cherry-pick into stable/20230725] [LEB128] Don't initialize error on success #7833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ static void findKeepUniqueSections(COFFLinkerContext &ctx) {
const uint8_t *cur = contents.begin();
while (cur != contents.end()) {
unsigned size;
const char *err;
const char *err = nullptr;
uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
if (err)
fatal(toString(obj) + ": could not decode addrsig section: " + err);
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,7 @@ static void findKeepUniqueSections(opt::InputArgList &args) {
const uint8_t *cur = contents.begin();
while (cur != contents.end()) {
unsigned size;
const char *err;
const char *err = nullptr;
uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
if (err)
fatal(toString(f) + ": could not decode addrsig section: " + err);
Expand Down
33 changes: 18 additions & 15 deletions llvm/include/llvm/Support/LEB128.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,30 @@ inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
}

/// Utility function to decode a ULEB128 value.
///
/// If \p error is non-null, it will point to a static error message,
/// if an error occured. It will not be modified on success.
inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
const uint8_t *end = nullptr,
const char **error = nullptr) {
const uint8_t *orig_p = p;
uint64_t Value = 0;
unsigned Shift = 0;
if (error)
*error = nullptr;
do {
if (p == end) {
if (LLVM_UNLIKELY(p == end)) {
if (error)
*error = "malformed uleb128, extends past end";
if (n)
*n = (unsigned)(p - orig_p);
return 0;
Value = 0;
break;
}
uint64_t Slice = *p & 0x7f;
if ((Shift >= 64 && Slice != 0) || Slice << Shift >> Shift != Slice) {
if (LLVM_UNLIKELY(Shift >= 63) &&
((Shift == 63 && (Slice << Shift >> Shift) != Slice) ||
(Shift > 63 && Slice != 0))) {
if (error)
*error = "uleb128 too big for uint64";
if (n)
*n = (unsigned)(p - orig_p);
return 0;
Value = 0;
break;
}
Value += Slice << Shift;
Shift += 7;
Expand All @@ -158,17 +159,18 @@ inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
}

/// Utility function to decode a SLEB128 value.
///
/// If \p error is non-null, it will point to a static error message,
/// if an error occured. It will not be modified on success.
inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
const uint8_t *end = nullptr,
const char **error = nullptr) {
const uint8_t *orig_p = p;
int64_t Value = 0;
unsigned Shift = 0;
uint8_t Byte;
if (error)
*error = nullptr;
do {
if (p == end) {
if (LLVM_UNLIKELY(p == end)) {
if (error)
*error = "malformed sleb128, extends past end";
if (n)
Expand All @@ -177,8 +179,9 @@ inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
}
Byte = *p;
uint64_t Slice = Byte & 0x7f;
if ((Shift >= 64 && Slice != (Value < 0 ? 0x7f : 0x00)) ||
(Shift == 63 && Slice != 0 && Slice != 0x7f)) {
if (LLVM_UNLIKELY(Shift >= 63) &&
((Shift == 63 && Slice != 0 && Slice != 0x7f) ||
(Shift > 63 && Slice != (Value < 0 ? 0x7f : 0x00)))) {
if (error)
*error = "sleb128 too big for int64";
if (n)
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Object/MachOObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2992,7 +2992,7 @@ void ExportEntry::pushNode(uint64_t offset) {
ErrorAsOutParameter ErrAsOutParam(E);
const uint8_t *Ptr = Trie.begin() + offset;
NodeState State(Ptr);
const char *error;
const char *error = nullptr;
uint64_t ExportInfoSize = readULEB128(State.Current, &error);
if (error) {
*E = malformedError("export info size " + Twine(error) +
Expand Down Expand Up @@ -3127,7 +3127,7 @@ void ExportEntry::pushNode(uint64_t offset) {

void ExportEntry::pushDownUntilBottom() {
ErrorAsOutParameter ErrAsOutParam(E);
const char *error;
const char *error = nullptr;
while (Stack.back().NextChildIndex < Stack.back().ChildCount) {
NodeState &Top = Stack.back();
CumulativeString.resize(Top.ParentStringLength);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Support/DataExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ static T getLEB128(StringRef Data, uint64_t *OffsetPtr, Error *Err,
if (isError(Err))
return T();

const char *error;
const char *error = nullptr;
unsigned bytes_read;
T result =
Decoder(Bytes.data() + *OffsetPtr, &bytes_read, Bytes.end(), &error);
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-readobj/COFFDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2125,7 +2125,7 @@ void COFFDumper::printAddrsig() {
const uint8_t *End = AddrsigContents.bytes_end();
while (Cur != End) {
unsigned Size;
const char *Err;
const char *Err = nullptr;
uint64_t SymIndex = decodeULEB128(Cur, &Size, End, &Err);
if (Err)
reportError(createError(Err), Obj->getFileName());
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-readobj/ELFDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5002,7 +5002,7 @@ static Expected<std::vector<uint64_t>> toULEB128Array(ArrayRef<uint8_t> Data) {
const uint8_t *End = Data.end();
while (Cur != End) {
unsigned Size;
const char *Err;
const char *Err = nullptr;
Ret.push_back(decodeULEB128(Cur, &Size, End, &Err));
if (Err)
return createError(Err);
Expand Down