-
Notifications
You must be signed in to change notification settings - Fork 23
Fix some UB in signal handling #18
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
Conversation
|
Sorry, github UI problems. I only intended to approve the first commit. |
ukleinek
left a comment
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.
Hmm, I don't particularly like this. I'd like to allow backends calling async-unsafe functions. Freeing ios in global code seems to work, but is a layer violation because the structure was allocated in the backends. I'll think about that, if you want to improve this while I think that would be welcome of course :-)
added by accident, only intended to approve first commit
We could
Is it? |
|
Could you elaborate on the type of issue this resolves? Do we have reproducible crashes, hypothetical undefined behavior or is this just about following best-practices? |
jonrebm
left a comment
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.
6b025f4 to
7efc70c
Compare
sigaction(2) reads the sa_mask and sa_flags struct members as well, but we didn't initialize them so far. We probably didn't run into problems, because we allocate it when the stack is fresh and full of zeroes. Fix this by explicitly zeroing the struct and emptying the signal set. Signed-off-by: Ahmad Fatoum <ahmad@a3f.at> Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
We use malloc elsewhere in the code with signals unmasked, but use free in a signal handler. This could result in a dead lock when terminating microcom by signal. Simply drop the free as exit is imminent. The duplication of exit(0) call is ok, as the call site in the signal handler will be replaced in a follow-up commit. Signed-off-by: Ahmad Fatoum <ahmad@a3f.at> Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
Both printf and exit aren't async signal safe, replace them with write and _Exit respectively. The access to ios and ios->exit and callees is still illegal, but at least we don't run risk reentering stdio and causing a deadlock anymore. As exit flushes stdio buffers as well, we now risk losing unflushed stdio output. This is acceptable as we aren't buffering the serial port output and the program is being terminated abnormally anyway. Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
7efc70c to
90efa26
Compare
While looking over #15, I noticed some issues with the way microcom handles signals. This fixes what was straight forward to fix. What remains is the access to
ios,ios->exitand whatever is called there. With #15 merged, this could be fixed by peppering somevolatilearound, but it's very unlikely to happen so postponing this for now.