git repos / PlatformFolders

commit 0267c838

sago007 · 2019-03-03 13:35
0267c8388fda8eabaf252a45b008d154ee75bef7 patch · browse files
parent ed746269432e092160bd31415672f712492dffac

Use getpwuid_r instead of getpwuid. This makes the library thread safe.

Changed files

M sago/platform_folders.cpp before
diff --git a/sago/platform_folders.cpp b/sago/platform_folders.cpp index c797177..0917284 100644 --- a/sago/platform_folders.cpp +++ b/sago/platform_folders.cpp
@@ -52,8 +52,16 @@ static std::string getHome() {
res = homeEnv;
return res;
}
- struct passwd* pw = getpwuid(uid);
- if (!pw) {
+ struct passwd* pw = nullptr;
+ struct passwd pwd;
+ long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (bufsize < 0) {
+ bufsize = 16384;
+ }
+ std::vector<char> buffer;
+ buffer.resize(bufsize);
+ int error_code = getpwuid_r(uid, &pwd, buffer.data(), buffer.size(), &pw);
+ if (error_code) {
throw std::runtime_error("Unable to get passwd struct.");
}
const char* tempRes = pw->pw_dir;