-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[LLVM][Support] check for error return from dladdr. #138369
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
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-llvm-support Author: None (jeremyd2019) ChangesIn 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. Full diff: https://github.com/llvm/llvm-project/pull/138369.diff 1 Files Affected:
diff --git a/llvm/lib/Support/Unix/Signals.inc b/llvm/lib/Support/Unix/Signals.inc
index 691e1014f18e8..b2bc76a1121d4 100644
--- a/llvm/lib/Support/Unix/Signals.inc
+++ b/llvm/lib/Support/Unix/Signals.inc
@@ -826,14 +826,17 @@ void llvm::sys::PrintStackTrace(raw_ostream &OS, int Depth) {
int width = 0;
for (int i = 0; i < depth; ++i) {
Dl_info dlinfo;
- dladdr(StackTrace[i], &dlinfo);
- const char *name = strrchr(dlinfo.dli_fname, '/');
-
int nwidth;
- if (!name)
- nwidth = strlen(dlinfo.dli_fname);
- else
- nwidth = strlen(name) - 1;
+ if (dladdr(StackTrace[i], &dlinfo) == 0) {
+ nwidth = 7; // "(error)"
+ } else {
+ const char *name = strrchr(dlinfo.dli_fname, '/');
+
+ if (!name)
+ nwidth = strlen(dlinfo.dli_fname);
+ else
+ nwidth = strlen(name) - 1;
+ }
if (nwidth > width)
width = nwidth;
@@ -841,15 +844,19 @@ void llvm::sys::PrintStackTrace(raw_ostream &OS, int Depth) {
for (int i = 0; i < depth; ++i) {
Dl_info dlinfo;
- dladdr(StackTrace[i], &dlinfo);
OS << format("%-2d", i);
- const char *name = strrchr(dlinfo.dli_fname, '/');
- if (!name)
- OS << format(" %-*s", width, static_cast<const char *>(dlinfo.dli_fname));
- else
- OS << format(" %-*s", width, name + 1);
+ if (dladdr(StackTrace[i], &dlinfo) == 0) {
+ OS << format(" %-*s", width, "(error)");
+ dlinfo.dli_sname = nullptr;
+ } else {
+ const char *name = strrchr(dlinfo.dli_fname, '/');
+ if (!name)
+ OS << format(" %-*s", width, static_cast<const char *>(dlinfo.dli_fname));
+ else
+ OS << format(" %-*s", width, name + 1);
+ }
OS << format(" %#0*lx", (int)(sizeof(void *) * 2) + 2,
(unsigned long)StackTrace[i]);
|
As promised to @mstorsjo in #138117 (comment).
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
dcd5e18
to
9fc9b53
Compare
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>
9fc9b53
to
3c25a22
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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.