Skip to content

Commit

Permalink
Check for NULL returns for both getpwuid and getgrgid
Browse files Browse the repository at this point in the history
These functions return NULL when the uid or gid does not exist in the
respective database. We were not previously checking for this, so would
crash the process if the entries did not exist.

Fixes #84
  • Loading branch information
jimhester committed Mar 10, 2018
1 parent 3517cda commit 901cb50
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

## Bugfixes

* Fix crash when a files user or group id did not exist in the respective
database (#84, #58)
* Fix home expansion on systems without readline (#60).
* Fix propagation of NA values in `path_norm()` (#63).

Expand Down
18 changes: 14 additions & 4 deletions src/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,26 @@ List stat_(CharacterVector path) {
SET_STRING_ELT(VECTOR_ELT(out, 5), i, NA_STRING);
#else
passwd* pwd;
pwd = getpwuid(st.st_uid);
SET_STRING_ELT(VECTOR_ELT(out, 5), i, Rf_mkCharCE(pwd->pw_name, CE_UTF8));
if ((pwd = getpwuid(st.st_uid)) != nullptr) {
SET_STRING_ELT(VECTOR_ELT(out, 5), i, Rf_mkCharCE(pwd->pw_name, CE_UTF8));
} else {
char buf[20];
sprintf(buf, "%" PRIu64, st.st_uid);
SET_STRING_ELT(VECTOR_ELT(out, 5), i, Rf_mkCharCE(buf, CE_UTF8));
}
#endif

#ifdef __WIN32
SET_STRING_ELT(VECTOR_ELT(out, 6), i, NA_STRING);
#else
group* grp;
grp = getgrgid(st.st_gid);
SET_STRING_ELT(VECTOR_ELT(out, 6), i, Rf_mkCharCE(grp->gr_name, CE_UTF8));
if ((grp = getgrgid(st.st_gid)) != nullptr) {
SET_STRING_ELT(VECTOR_ELT(out, 6), i, Rf_mkCharCE(grp->gr_name, CE_UTF8));
} else {
char buf[20];
sprintf(buf, "%" PRIu64, st.st_gid);
SET_STRING_ELT(VECTOR_ELT(out, 6), i, Rf_mkCharCE(buf, CE_UTF8));
}
#endif

REAL(VECTOR_ELT(out, 7))[i] = st.st_rdev;
Expand Down

0 comments on commit 901cb50

Please sign in to comment.