-
Notifications
You must be signed in to change notification settings - Fork 192
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
Use getpwnam_r instead of getpwnam in multi-threaded code #274
Conversation
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.
Thanks for your contribution!
I'm 100% in favor of switching to the reentrant versions of getpwnam/uid, however the current code doesn't follow the coding style documented in the DEVELOPING.md file, and we need to add conditional code for the sysconf call (a simple #ifdef
on the _SC_GETPW_R_SIZE_MAX
macro should do) or you can just put a large char array in the _cups_globals_t
structure to have a per-thread buffer to avoid allocating on the fly.
Thank you for the PR! Ideally it would be great if the PR had config script checks for new functions and macros (getpwnam_r, getpwuid_r, sysconf, _SC_GETPW_R_SIZE_MAX) and have fallbacks if some of them aren't available on target system. |
getpwnam and getpwuid are thread-unsafe and potentially dangerous in multi-threaded code. Substitue all their occurrences in multi-threaded code with getpwnam_r and getpwuid_r, which are thread-safe.
Sorry for that. I believe it should be following all the details now.
I have gone for using a large array in
Right now using, I would assume that any unix-based system using BTW, thank you very much for the feedback and sorry for my quite not-portable PR and ignorance in some of these points. I am coming from a strict Linux background and I really do not know many details for other systems. |
@pablofsf I think 16k is more than enough - the @zdohnal At this point I think my only concern would be in using the |
Thank you so much for all the information! Should I send a similar PR for the calls under |
@pablofsf The current implementation of the scheduler is single-threaded (well, for the parts that use getpwnam anyways) so let's not worry about that. |
@michaelrsweet ok, LGTM as well. @pablofsf Thank you for the changes! |
I've checked the code style and semantics of the code, looks great, merging. |
Thank you both! Looking forward to have this in the coming 2.4 release :)) |
This pull request is an attempt to provide an upstream fix to multiple crashes seen in Alpine with cups. The crashes have in common that they happen on calls to
getpwnam
. Reading the documentation, I found thatgetpwnam
andgetpwuid
are thread-unsafe functions. If I understand it correctly, the code under thecups
folder is (potentially?) multi-threaded and could explain the crashes ingetpwnam
. Substituting all calls bygetpwnam_r
andgetpwuid_r
fix the crashes, at least in my limited testing.Two things to note:
getpwnam
andgetpwuid
are marked thread-unsafe by musl/POSIX, but also by glibc. Therefore, although I have not seen reports of crashes happening with glibc, they could potentially happen in the future if this fix is not applied in multi-threaded code.getpwnam
andgetpwuid
under different files in thescheduler
folder. However, if I understand it correctly by the documentation, the scheduler is single-threaded and usinggetpwnam
andgetpwuid
should be safe. Otherwise, should I also change the calls there?