Skip to content

Commit 9fc9b53

Browse files
committed
[LLVM][Support] check for error return from dladdr.
In case of an error, the DL_info struct may have been left uninitialized, so it is not safe to use its members. In one error case, initialize dli_sname to nullptr explicitly, so that the later check against nullptr is guaranteed to be safe. Signed-off-by: Jeremy Drake <github@jdrake.com>
1 parent 173ec72 commit 9fc9b53

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

llvm/lib/Support/Unix/Signals.inc

+21-13
Original file line numberDiff line numberDiff line change
@@ -826,30 +826,38 @@ void llvm::sys::PrintStackTrace(raw_ostream &OS, int Depth) {
826826
int width = 0;
827827
for (int i = 0; i < depth; ++i) {
828828
Dl_info dlinfo;
829-
dladdr(StackTrace[i], &dlinfo);
830-
const char *name = strrchr(dlinfo.dli_fname, '/');
831-
832829
int nwidth;
833-
if (!name)
834-
nwidth = strlen(dlinfo.dli_fname);
835-
else
836-
nwidth = strlen(name) - 1;
830+
if (dladdr(StackTrace[i], &dlinfo) == 0) {
831+
nwidth = 7; // "(error)"
832+
} else {
833+
const char *name = strrchr(dlinfo.dli_fname, '/');
834+
835+
if (!name)
836+
nwidth = strlen(dlinfo.dli_fname);
837+
else
838+
nwidth = strlen(name) - 1;
839+
}
837840

838841
if (nwidth > width)
839842
width = nwidth;
840843
}
841844

842845
for (int i = 0; i < depth; ++i) {
843846
Dl_info dlinfo;
844-
dladdr(StackTrace[i], &dlinfo);
845847

846848
OS << format("%-2d", i);
847849

848-
const char *name = strrchr(dlinfo.dli_fname, '/');
849-
if (!name)
850-
OS << format(" %-*s", width, static_cast<const char *>(dlinfo.dli_fname));
851-
else
852-
OS << format(" %-*s", width, name + 1);
850+
if (dladdr(StackTrace[i], &dlinfo) == 0) {
851+
OS << format(" %-*s", width, "(error)");
852+
dlinfo.dli_sname = nullptr;
853+
} else {
854+
const char *name = strrchr(dlinfo.dli_fname, '/');
855+
if (!name)
856+
OS << format(" %-*s", width,
857+
static_cast<const char *>(dlinfo.dli_fname));
858+
else
859+
OS << format(" %-*s", width, name + 1);
860+
}
853861

854862
OS << format(" %#0*lx", (int)(sizeof(void *) * 2) + 2,
855863
(unsigned long)StackTrace[i]);

0 commit comments

Comments
 (0)