Skip to content

Commit 728e86e

Browse files
committed
CR
Signed-off-by: Romy <[email protected]>
1 parent 5df5a01 commit 728e86e

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/native/fs/fs_napi.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ typedef std::map<std::string, std::string> XattrMap;
168168
const char* gpfs_dl_path = std::getenv("GPFS_DL_PATH");
169169

170170
int gpfs_lib_file_exists = -1;
171+
long PASSWD_BUF_SIZE = -1;
171172

172173
static int (*dlsym_gpfs_fcntl)(gpfs_file_t file, void* arg) = 0;
173174

@@ -1399,26 +1400,22 @@ struct GetPwName : public FSWorker
13991400
std::string _user;
14001401
struct passwd pwd;
14011402
struct passwd *_getpwnam_res;
1402-
char *buf;
14031403
GetPwName(const Napi::CallbackInfo& info)
14041404
: FSWorker(info)
14051405
{
14061406
_user = info[1].As<Napi::String>();
1407+
_getpwnam_res = NULL;
14071408
Begin(XSTR() << "GetPwName " << DVAL(_user));
14081409
}
14091410
virtual void Work()
14101411
{
1411-
long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
1412-
if (bufsize == -1) {
1413-
bufsize = 16384;
1414-
}
1412+
std::unique_ptr<char[]> buf = std::unique_ptr<char[]>(new char[PASSWD_BUF_SIZE]);
14151413

1416-
buf = (char *)malloc(bufsize);
14171414
if (buf == NULL) {
14181415
SetSyscallError();
14191416
return;
14201417
}
1421-
int rc = getpwnam_r(_user.c_str(), &pwd, buf, bufsize, &_getpwnam_res);
1418+
int rc = getpwnam_r(_user.c_str(), &pwd, buf.get(), PASSWD_BUF_SIZE, &_getpwnam_res);
14221419
if (rc != 0) {
14231420
SetSyscallError();
14241421
return;
@@ -1434,12 +1431,6 @@ struct GetPwName : public FSWorker
14341431
set_getpwnam_res(env, res, *_getpwnam_res);
14351432
_deferred.Resolve(res);
14361433
}
1437-
1438-
~GetPwName()
1439-
{
1440-
free(buf);
1441-
}
1442-
14431434
};
14441435

14451436
struct FileWrap : public Napi::ObjectWrap<FileWrap>
@@ -2424,6 +2415,12 @@ fs_napi(Napi::Env env, Napi::Object exports)
24242415
exports_fs["DT_DIR"] = Napi::Number::New(env, DT_DIR);
24252416
exports_fs["DT_LNK"] = Napi::Number::New(env, DT_LNK);
24262417
exports_fs["PLATFORM_IOV_MAX"] = Napi::Number::New(env, IOV_MAX);
2418+
long passwd_bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
2419+
if (passwd_bufsize == -1) {
2420+
passwd_bufsize = 16384;
2421+
}
2422+
PASSWD_BUF_SIZE = passwd_bufsize;
2423+
24272424

24282425
#ifdef O_DIRECT
24292426
exports_fs["O_DIRECT"] = Napi::Number::New(env, O_DIRECT);
@@ -2437,6 +2434,7 @@ fs_napi(Napi::Env env, Napi::Object exports)
24372434
exports_fs["set_log_config"] = Napi::Function::New(env, set_log_config);
24382435

24392436
exports["fs"] = exports_fs;
2437+
24402438
}
24412439

24422440
} // namespace noobaa

0 commit comments

Comments
 (0)